有没有办法告诉 Smarty 不要打印表达式?
我想将 Smarty 与 Zend Framework 结合使用,特别是其中的一些视图助手。 现在我说到了重点,我实现了一个使用 Smarty 来显示模板的 Zend_View。我可以像往常一样分配值。到目前为止,一切都很好。
现在我真的很想在 Smarty 中使用 Zend View Helpers。我将 Zend_View 对象指定为“this”,并在模板中尝试了以下操作:
{$this->layout()->setLayout('default')}
由于这将打印 setLayout() 方法(这是一个 Zend_Layout)的返回值,因此出现错误:
Catchable fatal error: Object of类 Zend_Layout 无法转换为字符串/path/to/templates_c/089c3d67082722c7cabc028fa92a077f8d8b4af5.file.default.tpl.cache.php on line 27
这对我来说很清楚,所以我进入 Smarty 的核心来修复这个问题:
生成的代码确实看起来像这样
<?php
echo $_smarty_tpl->tpl_vars['this']
->value->layout()
->setLayout('default');
?>
:现在它显示:
<?php
$hack = $_smarty_tpl->tpl_vars['this']
->value
->layout()
->setLayout('default');
if( is_string($hack) ||
( is_object($hack) && method_exists($hack, '__toString') ) )
echo $hack;
?>
现在这可能是我能想到的最糟糕的修复,有几个原因(Smarty 兼容性丢失, 表现)。遗憾的是,这是唯一的一个。有没有办法阻止 Smarty 尝试打印表达式的输出?另外,我希望语法尽可能保持直观,并且我不想为所有助手编写 Smarty 函数,因为我想将此代码与可能添加新助手的第三方应用程序 (Pimcore) 一起使用。
预先感谢您的任何建议!
I'd like to use Smarty in conjuction with the Zend Framework, especially some of it's View Helpers.
Now i got to the point, where i implemented a Zend_View that uses Smarty to display templates. I can assign values as usual. So far so good.
Now I would really like to use Zend View Helpers in Smarty. I asssigned the Zend_View object as "this" and tried this in the template:
{$this->layout()->setLayout('default')}
As this will print the return value of the setLayout() method (which is a Zend_Layout), there is an error:
Catchable fatal error: Object of class Zend_Layout could not be converted to string in /path/to/templates_c/089c3d67082722c7cabc028fa92a077f8d8b4af5.file.default.tpl.cache.php on line 27
This is clear to me, so I went into Smarty's core to fix this:
The generated code did look like this:
<?php
echo $_smarty_tpl->tpl_vars['this']
->value->layout()
->setLayout('default');
?>
And now it reads:
<?php
$hack = $_smarty_tpl->tpl_vars['this']
->value
->layout()
->setLayout('default');
if( is_string($hack) ||
( is_object($hack) && method_exists($hack, '__toString') ) )
echo $hack;
?>
Now this is probably the worst fix i can think of, for several reasons (Smarty compatibility loss, performance). Sadly, it's the only one. Is there a way to stop Smarty from trying to print the output of the expression? Also, i want the syntax to stay as intuitive as possible, and i don't want to write Smarty functions for all the Helpers, because I want to use this code with a third-party application (Pimcore) that might add new helpers.
Thanks in advance for any suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些建议(只是想法,没什么好):
在 Zend_Layout 中创建一个返回 null 或空字符串的 __toString() (大/丑/更糟糕的解决方法)。
创建一个返回 null 或空字符串的变量修饰符/过滤器,因此您的调用将类似于
{$this->layout()->setLayout('default')|noreturn}
(您也可以将它与其他东西一起使用,并且可以使用诸如definition
或define
之类的友好名称来调用noreturn
来指示该对象的用途指令,但解决方法太)使用分配到构建一个表达式,将所有这些内容设置为另一个变量(也是解决方法)。
也许这可以给你一些好主意=)
Some suggestions (only ideas, nothing so good):
Create a __toString() in Zend_Layout who returns null or empty string (big/ugly/worse workaround).
Create a variable modifier/filter who return null or empty string, so your call will be something like
{$this->layout()->setLayout('default')|noreturn}
(you can use it with other things too andnoreturn
can be called with an friendly name likedefinition
ordefine
to indicate the purpose of the instruction, but a workaround too)Using the assign to build a expression who set all this thing to another var (workaround too).
Maybe this can give you some good ideas =)