If statements in template system

13,628

Solution 1

Please use php. Just put in your tpl file:

<?php if ($var > 2) .... ?> 

It's a lot simpler, less code and a lot faster than parsing the file in php

Solution 2

use

<? if( condition ) :
    ....
    ....
else : 
    ....
    ....
endif; ?>

Difference between if () { } and if () : endif;

Solution 3

You already got the answer with your last question: if statements in php templates using tpl
But since you won't go away otherwise, let me quickly answer it and then mention which will be your certain next stumbling blocks.

// handle {if}...{/if} blocks
$content =
preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#s', "tmpl_if", $content);

function tmpl_if ($match) {
    list($uu, $if, $inner_content) = $match;

    // eval for the lazy!
    $if = create_function("", "extract(\$GLOBALS['tvars']); return ($if);");

    // a real templating engine would chain to other/central handlers
    if ( $if() ) {
        return $inner_content;
    }
    # else return empty content
}

Using a regular expression like this will trip over a nested if. But you didn't ask about that, so I won't mention it. And as outlined in the comment you would actually need to chain to a central function that does further replacements ({foreach} / {include} / etc.) instead of just return $content as here.

This is doable, but quickly growing cumbersome. And this is why all other templating engines (which you refuse to check out) actually convert .tpl files into .php scripts. That's much easier because PHP can already handle all those control structures that you try to mimick with your own templating class.

Solution 4

Actually it's pretty simple unless you need nested if conditions.

$template = '<b>{foo}</b>{if bar} lorem ipsum {bar}{/if}....';

$markers = array(
    'foo' => 'hello',
    'bar' => 'dolor sit amet',  
);

// 1. replace all markers 
foreach($markers as $marker => $value)
    $template = str_replace('{'. $marker .'}', $value, $template);

//2. process if conditions
$template = preg_replace_callback('#\{if\s(.+?)}(.+?)\{/if}#s', function($matches) use ($markers) {

    list($condition, $variable, $content) = $matches;

    if(isset($markers[$variable]) && $markers[$variable]) {
        // if the variable exists in the markers and is "truthy", return the content
        return $content;
    }

}, $template);
Share:
13,628
Speedy Wap
Author by

Speedy Wap

Updated on June 27, 2022

Comments

  • Speedy Wap
    Speedy Wap almost 2 years

    How can I parse, let's say, {if $var > 2} or {if $var} in a .tpl file in my own version of a templating class. I do not wanna use smarty as I don't need all their plugins. I just want include, if, for and foreach statements.

  • Speedy Wap
    Speedy Wap about 13 years
    how could that then possibly converted to php.
  • mario
    mario about 13 years
    @user381595: With regular expressions. Converting each {if ...} into <?php if(...): ?> and each {/if} into <?php endif; ?> for example.
  • mario
    mario about 13 years
    @SpeedyWap: Yes it probably would. Hencewhy many solutions are already dicoverable with the search function. stackoverflow.com/questions/3930053/…
  • girish
    girish almost 9 years
    any idea how to added nested if for your solution.