ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的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> ```