Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Smarty html_选项_Php_Smarty_Undefined Index - Fatal编程技术网

Php Smarty html_选项

Php Smarty html_选项,php,smarty,undefined-index,Php,Smarty,Undefined Index,对于smarty的html_选项功能,除了不使用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} 我意识到它不会出现在模板中,但留下模板中未定义的内容似乎是一种不好的做法,它

对于smarty的html_选项功能,除了不使用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}
我意识到它不会出现在模板中,但留下模板中未定义的内容似乎是一种不好的做法,它还会用未定义索引的噪音填充我的错误日志

[编辑]

我要寻找的是这样做的一种方法,既不显示未定义的索引错误,也不减少模板文件中的smarty噪音

{html_options name=option_1 options=$options selected=$smarty.post.option_1}
我猜它更可能是一个修改过的html_选项插件

[编辑]

根据@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的预期目标

我想这是可行的:

或:

但是,如果您不能在模板中使用所有post/get/request/cookie/server/constants,而不将必须编写的代码量增加一倍,那么smarty跟踪所有post/get/request/cookie/server/constants又有什么意义呢

 {if isset($smarty.post)}
     {html_options name=option_1 optins=$options selected=$smarty.post.option_1}
 {/if}

我想回答你的问题

结果是,如果不编写一个单独的插件,我想要什么是不可能的。。。也许我会这样做,比如:

{html_options name=option_1 options=$options selected=$default.option_1 post=option_1}

刚刚注意到你在php中的做法,我的解决方案是在模板中,这是在模板中。。。您的解决方案与我的相同,需要编写的代码是我的两倍。。。这就是我认为smarty的重点。。。显然,除此之外,你不需要isset,除非你正在查看post数组中的特定项。当你输入一行int时,你会发现什么错误?当post变量为空时,我注意到:未定义索引:选项1我意识到这不是一个可怕的错误,但把它放在那里似乎有点草率。另一个问题是,如果你检查html,通知会隐藏在选择框中。这就是为什么你需要在某个地方使用isset,因为你的数组值没有设置。你可以在php中做isset并定义一个变量,作为$selected传递给smarty,然后你就不会得到我所做的错误但愿我不用用smarty。。。。
 {if isset($smarty.post)}
     {html_options name=option_1 optins=$options selected=$smarty.post.option_1}
 {/if}
{html_options name=option_1 options=$options selected=$default.option_1 post=option_1}