我是刚学smarty的,在看视频的时候,正好学到了循环、 section和foreach 我在做section循环的时候,代码是这样的!
先说一下: 左右边界是"{{"和"}}"
这个文件是base.html 的代码: {{section name=news loop=$news}}
<li><a href="news_list.php?article_ID={{$news[news].article_ID}}">{{$news[news].article_title}}</a></li>
{{sectionelse}}
没有要显示的数据!
{{/section}} 下面是base.php的代码: <?php
include('config/conn_db.php');
include('smarty.php');
//读取数据
$sql="select * from news";
$result=mysql_query($sql);
while($rs=mysql_fetch_array($result))
{
$info[]=array("article_ID"=>$rs["article_ID"],"article_title"=>$rs["article_title"]);
}
$smarty->assign('news',$info); //解析模板
$smarty->display('loop1.html');
?>
当我浏览的时候是正常的! 但我把section换成 foreach的时候就出错:下面是loop2.html的代码:<body>{{foreach item=news from=$news}}
<li>{{$news[news].article_titles}}</li>{{foreachelse}}
没有数据!
{{/foreach}}
</body>
</html>
下面是loop2.php的代码:(下面代码的目的是先不让他显示数据,先让他显示“没有数据”)<?php
include("smarty.php");
include("config/conn_db.php");$smarty->display("loop2.html");
?>
但是这浏览loop2.php的时候他却提示下面这个错误!
Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "templates\loop2.html" on line 10 "{{foreach item=news from=$news}} " item variable 'news' may not be the same variable as at 'from'' in F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php:423 Stack trace: #0 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_compile_foreach.php(40): Smarty_Internal_TemplateCompilerBase->trigger_template_error('item variable '...', 10) #1 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php(279): Smarty_Internal_Compile_Foreach->compile(Array, Object(Smarty_Internal_SmartyTemplateCompiler), Array, NULL, NULL) #2 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php(123): Smarty_Internal_TemplateCompilerBase->callTagCompiler('foreach', Array, Array) #3 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templateparser.php(2314): Smarty_Internal_TemplateCompilerBase->compileTag( in F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php on line 423我还找不到错误的地方! 还请促进位高手帮忙解析一下! 小弟感激!
先说一下: 左右边界是"{{"和"}}"
这个文件是base.html 的代码: {{section name=news loop=$news}}
<li><a href="news_list.php?article_ID={{$news[news].article_ID}}">{{$news[news].article_title}}</a></li>
{{sectionelse}}
没有要显示的数据!
{{/section}} 下面是base.php的代码: <?php
include('config/conn_db.php');
include('smarty.php');
//读取数据
$sql="select * from news";
$result=mysql_query($sql);
while($rs=mysql_fetch_array($result))
{
$info[]=array("article_ID"=>$rs["article_ID"],"article_title"=>$rs["article_title"]);
}
$smarty->assign('news',$info); //解析模板
$smarty->display('loop1.html');
?>
当我浏览的时候是正常的! 但我把section换成 foreach的时候就出错:下面是loop2.html的代码:<body>{{foreach item=news from=$news}}
<li>{{$news[news].article_titles}}</li>{{foreachelse}}
没有数据!
{{/foreach}}
</body>
</html>
下面是loop2.php的代码:(下面代码的目的是先不让他显示数据,先让他显示“没有数据”)<?php
include("smarty.php");
include("config/conn_db.php");$smarty->display("loop2.html");
?>
但是这浏览loop2.php的时候他却提示下面这个错误!
Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "templates\loop2.html" on line 10 "{{foreach item=news from=$news}} " item variable 'news' may not be the same variable as at 'from'' in F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php:423 Stack trace: #0 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_compile_foreach.php(40): Smarty_Internal_TemplateCompilerBase->trigger_template_error('item variable '...', 10) #1 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php(279): Smarty_Internal_Compile_Foreach->compile(Array, Object(Smarty_Internal_SmartyTemplateCompiler), Array, NULL, NULL) #2 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php(123): Smarty_Internal_TemplateCompilerBase->callTagCompiler('foreach', Array, Array) #3 F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templateparser.php(2314): Smarty_Internal_TemplateCompilerBase->compileTag( in F:\AppServ\www\smarty_site\smarty\sysplugins\smarty_internal_templatecompilerbase.php on line 423我还找不到错误的地方! 还请促进位高手帮忙解析一下! 小弟感激!
$news[]=array();
$smarty->assign('news',$news);
<body>{{foreach item=item from=$news}}
<li>{{$item[article_titles]}}</li>{{foreachelse}}
没有数据!
{{/foreach}}
</body>
</html>
{foreach from=$myArray key=k item=v}
<ul><li>键:{$k}:值{$v}</li></ul>
{/foreach}
多维数组:
{section name=customer loop=$arr}
//取值
$arr[customer].属性的值
{/section}
一般情况下比较喜欢用foreach!
一般情况下比较喜欢用foreach!