企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
``` // 1.引入smarty include 'libs/Smarty.class.php'; // 2.实例化Smarty $smarty = new Smarty; $smarty->template_dir = 'view'; // 3.分配变量 $data = ['username'=>'zhangsan','age'=>20,'sex'=>'男']; $smarty->assign('data', $data); $smarty->assign('age', 30); // 4.编译并输出模板 $smarty->display('5.html'); // foreach ($variable as $key => $value) { // # code... // } ``` ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <!-- $v@index:索引位置(从0开始计数) $v@iteration:索引位置(从1开始计数) $v@total:获取遍历的总次数 --> {foreach $data as $k => $v} <p>{$k}===>{$v}</p> {if $v@index == 1} <h3>111</h3> {/if} {if $v@iteration == 1} <h3>222</h3> {/if} {/foreach} } <h3>{$v@total}</h3>//获取遍历总次数 {if $age > 20} <h3>过年回家妈妈给你准备了三个对象,尽情的去相亲</h3> {elseif $age<20} <h3>不着急结婚</h3> {/if} <ul> <!--for循环 step:步长 --> {for $i=0 to 10 step 3}//输出0 3 6 9 <li>{$i}</li> {/for} </ul> </body> </html> ```