Edit: Maybe I should make this part clear. A user is writing the template. So I want the syntax to be simple, and I can’t trust them with powerful template engines.
So, I need a template engine that will be used to write emails, using tokens (easy) with conditional logic (less easy).
Example: Hello{if first_name} dear {first_name}{endif}, blah blah blah.
If first_name is not available, it should read: Hello, blah blah blah.
I was able to get it working using eval… but we all know eval is evil.
$body = preg_replace('/{if ([^{\|}]+)}/i', '<?php if(isset(\$tokens[\'$1\'])):?>', $body);
$body = preg_replace('/{endif}/i', '<?php endif;?>', $body);
Can anyone point me towards a tutorial on this one? I can’t seem to find anything beyond simple token replacement.
Using the comments from everyone, I was able to figure out how to quickly make my own secure template engine with custom syntax.
Let’s say I want to make
{first_name|buddy}be the first_name variable if it’s set, but if it’s not set use the string “buddy” as default.Use a powerful template engine, like Smarty. Smarty has support for this, but I don’t like the syntax
{$first_name|default:"buddy"}Replace all occurrences of your custom syntax with the correct syntax
Then just run it through Smarty. This is essentially the same as using eval, but secure. Thanks guys.