Hello,
My question is really about implementation more than anything - I can probably come up with the right code if I have some idea what I'm doing!
For a script I'm working on, I'd like to implement a tag-based template system. Each tag could accept whatever attributes it wants, and each relates to a certain /tag/TAGNAME.tag.php file.
An example tag might be this:
<date format="MM DD YYYY" offset="-5">{$date}</date>
That tag should be replaced by its appropriate PHP equivalent (I'll compile templates and, if I come across an uncompiled template, I'll compile at runtime). For instance, something in the date_Tag class should parse that by subtracting 56060 from the timestamp ({$date}) and then spitting out a
date('PHP_DATE_FORMAT', $date - (5*60*60));
Now, how exactly would I go about replacing these tags (many of which will be more complicated; for instance, one might spit out the five latest blog entries or something)? I was thinking of having a TemplateHandler class, which would look for a compiled template and, if it's unable to find one, would call the Compiler class. That class (or perhaps a separate TagHandler) would have to parse the tags. Rather than having all the parsing work done in the specific files, I was thinking I might like to have, if possible, the TagHandler go through and, for each <tag> that exists in the template and has a counterpart in the /tags/ folder, set the class array $attributes so that attributeName => attributeValue.
If I manage to do that, how would I go about replacing tags in templates with PHP code? I was thinking I'd do a preg_match_all() and, for each set of matches, call the appropriate parsing method in the /tag/ file. That's messy, though, as I would then have to do a bunch of preg_replace('..', '...' , $template, 1)'s to replace each tag instance individually (the parseTag method of each /tag/ file would have to be called for each <tag> in the template because attributes and values could be different).
Any ideas? Some psuedo-code would be great; I could work with that and come up with the code myself.
Thanks!