[ACCEPTED]-If statements in template system-templating-engine
Please use php. Just put in your tpl file:
<?php if ($var > 2) .... ?>
It's 2 a lot simpler, less code and a lot faster 1 than parsing the file in php
use
<? if( condition ) :
....
....
else :
....
....
endif; ?>
Difference between if () { } and if () : endif;
0
You already got the answer with your last 16 question: if statements in php templates using tpl
But since you won't go away otherwise, let 15 me quickly answer it and then mention which 14 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 13 a regular expression like this will trip 12 over a nested if
. But you didn't ask about 11 that, so I won't mention it. And as outlined 10 in the comment you would actually need to 9 chain to a central function that does further 8 replacements ({foreach}
/ {include}
/ etc.) instead of just 7 return $content
as here.
This is doable, but quickly growing 6 cumbersome. And this is why all other templating 5 engines (which you refuse to check out) actually 4 convert .tpl
files into .php
scripts. That's much 3 easier because PHP can already handle all 2 those control structures that you try to 1 mimick with your own templating class.
Actually it's pretty simple unless you need 1 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);
You can use the following format into your 1 template file(.tpl).,
{if $url == 'error'}
Error message Invalid Login!
{/if}
There is an php code example that parses 1 following temaplate (php 5.3+):
[IF {post_content}]Post content is filled![ENDIF]
[IF {post_content}]Post content is filled![ELSE]{post_content}[ENDIF]
Code:
$tags = array('post_content'=>'POST_CONTENT');
$message = '1: [IF {post_content}]Post content: {post_content}![ENDIF]
2: [IF {post_content}]Post content is filled![ELSE]Post content is empty![ENDIF]';
$matches = array();
preg_match_all('/\[IF \{([^\}]*)\}\](.[^\]]+)(?:\[ELSE\](.+?))?\[ENDIF\]/s', $message, $matches);
if ( empty($matches) ) {
return $message;
}
$math_tag = '';
foreach ( $matches[0] as $m_index => $match )
{
$math_tag = trim($matches[1][$m_index]);
if ( !empty($tags[$math_tag]) ) {
// IF value is not empty
$message = str_replace($match, $matches[2][$m_index], $message);
} elseif( empty($tags[$math_tag]) && $matches[3][$m_index] ) {
// ELSE
$message = str_replace($match, $matches[3][$m_index], $message);
} else {
// IF NO ELSE condition - REMOVE ALL
$message = str_replace($match, '', $message);
}
}
foreach($tags as $tag => $value)
$message = str_replace('{'. $tag .'}', $value, $message);
echo $message;
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.