开发者

Find occurances of custom tag in HTML, perform function and replace tag with function output

This is probably a really easy question, so apologies for not being more challenging!

I am developing an in house CMS which uses a templating system.

To make this CMS as extendable as possible I want to use "modules". Hence in the template files I want to put custom HTML tags which represent modules.

This will look like:

<p><Module name="newsTicker" property="value" anotherProperty="anotherValue" /></p>

I get my template file by using:

ob_start();
include "templatefile.tpl";
开发者_运维技巧$buffer= ob_get_contents();
ob_end_clean();

Now I want to go through the contents of $buffer and for every occurance of <Module name="moduleName" /> I want to include "moduleName.class.php", create a new instance of that class, run the getOutput() function of that module and put the return value in where that <Module /> tag used to be.

I have no idea how to go about that... :/ Any ideas?

If you don't understand think about a news ticker. I may write some code that looks through the news table in my database and makes a list of the 10 latest news stories, then it outputs it to a html <ul>. I want to abstract this so that it can be used on many pages. So in my template for the home page I might have the tag <Module name="newsTicker" />. Now when I visit the home page I need to look through the home page tpl file and I'll find where it says <Module name="newsTicker" />, I'll extract the name attribute and include a class based on that name (in this case it will be newsTicker.class.php). This will have a function called getOutput() so I will be able to something similar to: $output = $current_module->getOutput(); and then I need to go back to my tpl file and change the original <Module name="newsTicker" /> for the contents of $output.

Thanks for your help. :)

Tom


  • Use DOMDocument to find Module elements (I would use lowercase only personally).
  • Store name attribute.
  • include_once the class based on name attribute (skip if using autoloading).
  • Use $class = new $name.
  • Remove original Module DOM element and appendChild of $class->getOutput().
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜