Usage of if else in PHP smarty template engine

Source: Internet
Author: User

Usage:

{if} {/if}

{if} {Else} {/else} {/if}

Parsing Engine:

The code is as follows
//If


$if _operators = Array (">", "<", "= =", "= =", ">=", "<=", "!=", "!==");


$is _operator = false;


$pattern _if = "/{$left _tag}ifs+ ((. *?)) {$right _tag}/";


Preg_match_all ($pattern _if, $content, $match _if);


  


if (isset ($match _if[1]) && Is_array ($match _if[1])) {


   


foreach ($match _if[1] as $key _if=> $val _if) {


foreach ($if _operators as $if _operator) {


$items = explode ($if _operator, $val _if);


if (count ($items) = 2) {


$is _operator = true;


$shift _left = substr ($items [0], 0,1);


$pop _left = substr ($items [0], 1);


$shift _right = substr ($items [1], 0,1);


      


$pop _right = substr ($items [1], 1);


$left _var = $shift _left = = ' $ '? ' $this->var_tpl_arr['. $pop _left. ' "] ': $items [0];


$right _var = $shift _right = = ' $ '? $this->var_tpl_arr["'. $pop _right. '] ': $items [1];


$content = str_replace ($match _if[0][$key _if], "<?php if ({$left _var} {$if _operator} {$right _var}) {?>", $content);


Break


} else {


continue;


     }


    }


if (! $is _operator) {


$content = str_replace ($match _if[0][$key _if], "<?php if ({$val _if}) {?>", $content);


    }


   }


  }


  


  


//If close


$pattern _if = "/{$left _tag}/if{$right _tag}/";


$replace _if = "<?php}?>;";


if (Preg_match ($pattern _if, $content)) {


$content = preg_replace ($pattern _if, $replace _if, $content);


  }





//Else


$pattern _else = "/{$left _tag}else{$right _tag}/";


$replace _else = "<?php} else {?>";


if (Preg_match ($pattern _else, $content)) {


$content = preg_replace ($pattern _else, $replace _else, $content);


  }


  


//Else close


$pattern _else = "/{$left _tag}/else{$right _tag}/";


$replace _else = "";


if (Preg_match ($pattern _else, $content)) {


$content = preg_replace ($pattern _else, $replace _else, $content);


  }

Template Presentation method:

The code is as follows

<br/>
<br/>
$var _left = = {$var _left}, $var _right = = {$var _right}<br/><br/>
Test if: $var _left>1
<br/>
{if ($var _left>1)}
<b>if > conditions set up! </b>
{/if}
<br/>

Test if: $var _left<5
<br/>
{if ($var _left<5)}
<b>if < conditions set up! </b>
{/if}
<br/>

Test if = =: $var _left== $va _right
<br/>
{if ($var _left== $va _right)}
<b>if = = Condition Set up! </b>
{Else}
<b><font color=red>if = Condition not tenable! </font></b>
{/if}

Parsed cache file:

The code is as follows

<br/>
<br/>
$var _left = = <?php echo $this->var_tpl_arr["Var_left"];? ", $var _right = = <?php echo $this->var_tpl_arr[" Var_right "];? ><br/><br/>
Test if: $var _left>1
<br/>
<?php if ($this->var_tpl_arr["Var_left"] > 1) {?>
<b>if > conditions set up! Www.111cn.net</b>
<?php}?>;
<br/>

Test if

The code is as follows
_left<5: $var
<br/>
<?php if ($this->var_tpl_arr["Var_left"] < 5) {?>
<b>if < conditions set up! </b>
<?php}?>;
<br/>

Test if = =: $var _left== $va _right

The code is as follows
<br/>
<?php if ($this->var_tpl_arr["var_left"] = = = $this->var_tpl_arr["Va_right"]) {?>
<b>if = = Condition Set up! </b>
<?php} else {?>
<b><font Color=red>if = = condition is not tenable! </b></b>
<?php}?>;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.