Creating custom “html”-tags for CMS?

2019-01-23 21:43发布

问题:

I am working with a CMS for a web app in PHP, that has the needs of shortening the process for inserting (embedding) stuff, like a video from youtube or vimeo by wroting the following, which are stored in the database:

<youtube id="wfI0Z6YJhL0" />

Which would output the following after some sort of replace:

<!-- Custom formatting before object !-->
<object width="640" height="385"><param name="movie" value="http://www.youtube-nocookie.com/v/wfI0Z6YJhL0&amp;hl=sv_SE&amp;fs=1?rel=0&amp;color1=0xe1600f&amp;color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/wfI0Z6YJhL0&amp;hl=sv_SE&amp;fs=1?rel=0&amp;color1=0xe1600f&amp;color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
<!-- Custom formatting after object !-->

How could I do this in PHP?

回答1:

I've written a class that does exactly what you ask for my own cms. I've uploaded the src for you as although I've never released it the source is released under a BSD style license. Custom Tags

It basically allows you do do exactly what you ask for. In the class there are some example custom tags so I won't paste code here. Let me know how you go.

Edit 1: Example Code as requested. :-)

Edit 2: I should add it supports buried custom tags.

Edit 3: It also supports inline templating and tag substitution, ie

<ct:inline some="attribute">
    This is an in line template. <br />
    This is a #{tag} that can be accessed by the callback function
</ct:inline>

PHP/HTML: example.php

<?php

$current_dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
require_once dirname($current_dir).DIRECTORY_SEPARATOR.'customtags.php';

$ct = new CustomTags(array(
    'parse_on_shutdown'     => true,
    'tag_directory'         => $current_dir.'tags'.DIRECTORY_SEPARATOR,
    'sniff_for_buried_tags' => true
));

?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>untitled</title>
    <meta name="generator" content="TextMate http://macromates.com/">
    <meta name="author" content="Oliver Lillie">
    <!-- Date: 2010-07-10 -->
</head>
<body> 

    <ct:youtube id="wfI0Z6YJhL0" />

</body>
</html>

Custom Tag PHP Function: tags/youtube/tag.php:

function ct_youtube($tag)
{
    return '<object id="'.$tag['attributes']->id.'" value="http://www.youtube.com/v/'.$tag['attributes']->id.'" /><param ......>';
}

Output:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd"> 

<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>untitled</title> 
    <meta name="generator" content="TextMate http://macromates.com/"> 
    <meta name="author" content="Oliver Lillie"> 
    <!-- Date: 2010-07-10 --> 
</head> 
<body> 

    <object id="wfI0Z6YJhL0" value="http://www.youtube.com/v/wfI0Z6YJhL0" /><param ......> 

</body> 
</html>


回答2:

I'm not 100% sure how it will react to non-standard tags, but if it works, simpleHTMLDom will be perfect for this.

$html = str_get_html('....');

then something along the lines of ...

$element = $html->find('youtube',0 ); // Finds first element
                                      // - use "foreach" loop for final version 
$element->tag = 'object';
$element->value = "http://www.youtube.com/v/".$element->id;
$element->innertext= "<param ......>"

....

echo $html;

you get the drift.

The beauty of this approach would be that every specific extension could add its data in clean HTML notation <tagname attribute="value">, with even the possibility of adding sub-tags for structured info, instead of kludgy {placeholder}s and regexes and so on.

I have never tried this and I don't have the time to test this right now, but if you decide to give it a try, I'd be interested to know whether this way turned out to be useful.