对于 smarty 的 html_options 函数,有没有办法避免这样做(除了不使用 smarty 之外)?
{if $smarty.post}
{html_options name=option_1 options=$options selected=$smarty.post.option_1}
{else}
{html_options name=option_1 options=$options}
{/if}
我意识到它不会出现在模板中,但留下一些未在模板中定义的东西似乎是一种不好的做法(它也会用关于未定义索引的噪音填充我的错误日志)。
[编辑]
我正在寻找一种方法来做到这一点,而不会出现未定义的索引错误,以及减少模板文件中的聪明噪音。
{html_options name=option_1 options=$options selected=$smarty.post.option_1}
我猜它更有可能是一个修改过的 html_options 插件?
[编辑]
根据@mmcgrail 的想法:
{if isset($smarty.post.option_1)}
{assign var=selected value=$smarty.post.option_1}
{else}
{assign var=selected value=$default.option_1}
{/if}
{html_options name=option_1 options=$options selected=$selected}
我发现这更糟,因为它在模板中创建了新变量,偏离了 smarty 的假定目标。
我想这行得通:
或者:
<?php
//[... snip ...]
$option_1 = isset($_POST['option_1'])? $_POST['option_1'] : $default['option_1'];
$template->assign('option_1', $option_1);
$template->display('my_template.tpl');
在模板中:
{html_options name=option_1 options=$options selected=$option_1}
但是,如果你不能在模板中使用它们而不增加你必须编写的代码量,那么 smarty 跟踪所有的 post/get/request/cookie/server/constants 又有什么意义呢?