【发布时间】:2014-12-15 18:04:06
【问题描述】:
问题
说我有
{php}
function hello(){
{/php}
<div class="hello">{$smarty.foreach.panellist.iteration}</div>
{php}
}
{/php}
然后我在下面调用:
{foreach from=$channelObj->get_panellist_primary('','','pan_ptyid,pan_label1,pan_label2') item=panelObj name=panellist}
{php}hello(){/php}
{/foreach}
我收到此错误:
PHP Fatal error: Using $this when not in object context in /var/www.app1/theURL/otherthings/channel.tpl.php on line 197
为什么这不起作用?如何从函数内部访问此 foreach 的迭代,最好不传递参数?
更多详情
因为我知道你会告诉我{php} 已被弃用...我使用它的原因是因为我被要求制作一个非常复杂的模板,所以我需要复杂的函数来构建它而不让它变成一团糟,我以前从未使用过smarty。我在最后期限,我已经迟到了,所以我无法学习 smarty 的正确方法。我知道如何管理此代码的唯一方法是使用常规 PHP,但我的老板坚持我用 smarty 完成整个事情......不允许使用 PHP 或 Javascript。
谢谢!
【问题讨论】:
-
如果“不允许 PHP ...”,那么呃...
{php}是不允许的,对吧? :) 说真的,你能are you allowed a plugin 吗?如果没有,你的生活可能会更难成为 SOL... -
这看起来像是经典的X/Y problem 对我来说:你偶然发现了一些你不知道如何在 Smarty 中做的事情(与编写可重用的代码块有关),开始了一条路线你认为这很容易,结果却得到了一件你不知道如何在 Smarty 中做的不同的事情(以非标准方式混合 PHP 和 Smarty)。你可能会发现回到最初的问题比在错误的方法上苦苦挣扎更好地利用你的时间。
-
@bishop...naw 我可以在 .tpl 文件中使用我必须工作的任何内容...我无权访问该课程
-
@IMSoP 我已经走了很多路,我只需要有关如何执行此操作的帮助。我可以用一堆意大利面条聪明的代码来做到这一点,但我想避免这种情况。我希望 smarty 代码看起来像,例如,
{wrapperIfNecessary()}{content}{closeWrapperIfNecessary()} -
替代方案是
{if \\thing || \\thing and \\thing eq thing}<div>divdivdivdivdivdivdivdiv....etc{/if>{content}{if really long if statement that makes it not even worth using smarty etc}</div></div></div></div></div><div>hello</div></div></div>{/if}