Question

I'm trying to write a very basic template engine for a project and ended up taking the approach described in this article, which write template variables like [@variable] and to simply use str_replace() to parse the templates and put in the variable values.

This is really simply and seems to work well, but despite my best efforts there are situations in which basic logic is really needed in the template itself. I don't need anything complicated, just a way to have single if/else or ternary statements. For example, something like [if@something?"text":"other text"].

What might be a good approach to parsing basic logic in templates, like shown in the above example?

UPDATE:

I'm working on an AJAX heavy website, so I have a PHP AJAX controller that receives requests and then returns a JSON encoded response. Here's an example:

AJAX handler.php:

...
else if($req == 'getContent')
{
    $template = new Template('template.tpl');
    $template->setData(array(
        'date' => time(),
        'var'  => 'foo',
        'foo'  => 'bar'
    ));

    $response = array(
        'error' => null,
        'content' => template->getOutput()
    );
}
...

echo json_encode($response);

template.tpl:

lots and lots and lots of HTML
........
variables [@date] mixed in [@foo] somewhere with HTML [@bar]
........
lots and lots and lots of HTML

If I wasn't using a template engine (which just uses str_replace() to put the variable values into the template after getting it with file_get_contents()), then I would have to do this:

AJAX handler.php:

...
else if($req == 'getContent')
{

    $output = '
        lots and lots and lots of HTML' . time() . '
        ........
        variables ' . $foo . ' mixed in ' . $bar . 'somewhere with HTML
        ........
        lots and lots and lots of HTML
    ';

    $response = array(
        'error' => null,
        'content' => $output
    );
}
...

echo json_encode($response);

The whole point of what I'm trying to accomplish here is to separate huge blocks of HTML text (in some cases, literally entire page bodies) from my AJAX handler and instead keep them in separate files, which is far more readable and keeps the AJAX handler from becoming ridiculously massive.

Was it helpful?

Solution

PHP is already a templating language. For example, this is what your example above would look like in PHP:

<? echo (something ? "text" : "other text") ?>

Think twice before adding another layer of complexity on top of this.

There are templating libraries like Smarty and they have their justification, for example caching, or when you want to separate code and design in a safe way (i.e. give designers something they can modify without breaking code). If it's just for your own use, though, then using native PHP is a very strong option.

The reason is to avoid mixing HTML with PHP and having spaghetti code

There is nothing wrong with using simple PHP inside your HTML code. (and whether you have spaghetti code in PHP or your new templating language doesn't really make a difference....)

What's bad (and leading to Spaghetti code) is mixing huge chunks of code with the HTML output, for example calculations or preparations. Those should always be separate from the HTML.

The PHP inside the HTML structure should do only simple comparisons, if/then/else checks, for/foreach loops, and any operations directly related to outputting data (e.g. htmlspecialchars or simple calculations).

If you stick to that, you can happily use native PHP for your templating needs.

OTHER TIPS

Maybe you can define another template definition for functions, like:

[*function_name][param1,param2,param3]

For example for your example:

[*defined][@something,"text","other"]

And in your template engine code you can write the logic for the "defined" template function.

Just an idea!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top