PHP smarty template engine foreach Loop

Source: Internet
Author: User
Tags foreach php foreach split smarty template

Template file:

The code is as follows

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>{$web _tile}</title>
<body>
{$article _title}
<br/>
--by {$author}
<br/>
{$content}
<br/>
--publish @ {$time}

<br/>
<br/>
foreach test:

{foreach (From=url key=b item=c)}
<a href= "Index.php?artcle_id={==b}" >{==c}</a>
{/foreach}

<br/>

</body>

Parsing Engine:

The code is as follows
//Var


$pattern _var = "/{$left _tag}$ ([wd]+) {$right _tag}/";


$replace _var = ' &lt;?php echo $this-&gt;var_tpl_arr["$";? &gt; ';


  


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


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


  }


  


//foreach


Preg_match_all ("/{$left _tag}foreachs+" ([^{]+?) {$right _tag}/is ", $content, $match _foreach);


if (isset ($match _foreach[1]) &amp;&amp; Is_array ($match _foreach)) {


foreach ($match _foreach[1] as $match _key =&gt; $match _value) {


$split _foreachs = Array_filter (Preg_split ('/s+/is ', $match _value));


$new _foreach_tag = Array ();


foreach ($split _foreachs as $split _foreach) {


$split = explode ("=", $split _foreach);


if (count ($split = = 2)) {


if (In_array ($split [0], array ("from", "Item", "key")) {


//Filter label does not exist filter


$new _foreach_tag[$split [0]] = $split [1];


      }


     }


    }


    


$from = $key = $item = ';


Extract ($new _foreach_tag);


$key = ($key)? ' $ '. $key. ' =&gt; ': ';


$replace _foreach = ' &lt;?php foreach ($this-&gt;var_tpl_arr['. $from. '] as '. $key. ' $ '. $item. ') {?&gt; ';


$content = str_replace ($match _foreach[0][$match _key], $replace _foreach, $content);


    


   }


  }


  


$pattern _foreach = "/{$left _tag}/foreach{$right _tag}/";


$replace _foreach = "&lt;?php}?&gt;";


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


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


  }


  


//var in statement


$pattern _var = "/{$left _tag}== ([wd]+) {$right _tag}/";


$replace _var = ' &lt;?php echo $$1;? &gt; ';


  


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


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


  }

After parsing:

  code is as follows &nbs P;

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd"
<meta http-equiv= "Content-type" content= "text/html; charset=utf-8"
<title ><?php echo $this->var_tpl_arr["Web_tile"];? ></title>
<body>
<?php echo $this->var_tpl_arr["Article_title"];?
<br/>
  --by <?php Echo $this->var_tpl_arr["author"];?
<br/>
<?php echo $this->var_tpl_arr["content"];?
<br/>
-publish @ <?php echo $this->var_tpl_arr["Time"];?

<br/>
<br/>
foreach test:

<?php foreach ($this->var_tpl_arr["url"] as $b =& Gt $c) {
 <a href= "index.php?artcle_id=<?php echo $b;? > "><?php echo $c;? ></a>
<?php}

<br/>

</body>

Use:

  code is as follows &nbs P;

<?php

require_once ' core/yatp.class.php ';

$app = new YATP ();
Date_default_timezone_set ("Asia/shanghai");
$app->is_cache = false;

$article _title = "Yet,it is a simple template engine";
$author = "sanwhiteyu@tencent.com";
$web _tile = "just test";
$content = "It is easy to write a simple template engine for Yourself,what u can does is try to do it!";
$time = Date ("Y-m-d h:i:s", Time ());

$url = Array (
   "Url1" =>http://www.111cn.net,
   url2 "=>" http://www.baidu.com ",
);

$app->assign ("Article_title", $article _title);
$app->assign ("author", $author);
$app->assign ("Web_tile", $web _tile);
$app->assign ("content", $content);
$app->assign ("Time", $time);
$app->assign ("url", $url);br> $app->display ("index.html");

End of script effect:

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.