If statements in the template system

How can I parse , say, {if $var > 2}or {if $var}in a .tpl file in my own version of the template. I do not want to use smarty, because I do not need all their plugins. I just want to include if, for, and foreach.

+3
source share
6 answers

Please use php. Just enter your tpl file:

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

It is much simpler, less code and much faster than parsing a file in php

+13
source

use

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

The difference between if () {} and if (): endif;

+7
source

: php- tpl
, , , .

// 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
}

, , if. , . , , , ({foreach}/{include}/ ..) return $content, .

, . ( ) .tpl .php. , PHP , .

+6

, .

$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);
+3

(.tpl).,

{if $url == 'error'}
Error message Invalid Login!
{/if} 
0

php, temaplate (php 5.3 +):

[IF {post_content}]Post content is filled![ENDIF]
[IF {post_content}]Post content is filled![ELSE]{post_content}[ENDIF]

:

$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;
0

Source: https://habr.com/ru/post/1790910/


All Articles