我试过{include_php file="phpfile.php"}
和{php}
标签,但都导致不推荐的错误。你不能在Smarty中做到这一点吗?我无法在文档中找到任何内容。Smarty:如何在模板中包含php?
回答
我绕过了这个问题。它创建具有该功能名为block.php_code.php
插件文件:
function smarty_block_php_code($params, $content, &$smarty)
{
if (is_null($content))
{
return;
}
if ('<?php' == substr($content,0,5) && '?>' == substr($content, -2))
$content = substr($content,5,-2);
ob_start();
eval($content);
return ob_get_clean();
}
在您的模板,然后你可以写:
{php_code}{literal}<?php
print "Hello, world!";
?>{/literal}{/php_code}
尤达风格是丑陋的。特别是在没有*任何*优势 – ThiefMaster
的情况下,您认为哪里有优势? – wonk0
@ wonk0 [这将是一个优点。](http://stackoverflow.com/questions/4624536/which-one-will-execute-faster-if-flag-0-or-if-0-flag/4624562# 4624562) –
{include_php}在这两个SMARTY2和3弃用标记; {} PHP只Smarty3:
- 1. Smarty在php和javascript中都包含模板(使用Smarty和JSmart)
- 2. 如何在smarty中显示包含html模板的变量?
- 3. Smarty + Prestashop:包含模板(如果存在的话)
- 4. 在Smarty 2模板中包含或导入(@import)样式表
- 5. 如何使用Smarty模板中的Smarty模板?
- 6. Backbone.js与生态模板:如何在模板中包含模板?
- 7. 如何在WordPress的php模板中包含PHP文件?
- 8. 在Wordpress模板中包含PHP文件
- 9. Smarty动态模板包括
- 10. 在Smarty模板中使用php函数
- 11. PHP在SmartyBC中的Foreach。 Smarty模板
- 12. 在Smarty模板中引用PHP常量
- 13. 如何在类中包含HTML模板?
- 14. 在SMARTY模板
- 15. PHP:包含模板系统
- 16. 如何在smarty模板中构建url?
- 17. 如何在smarty模板中使用js?
- 18. 将jquery包含在smarty中
- 19. php smarty模板结构
- 20. 如何在php/smarty中包含静态html
- 21. 如何在PHP Smarty模板中访问Oracle中的查询?
- 22. PHP-Smarty模板 - 如何创建上下文无关模板
- 23. 在.php模板中包含一个.twig基本模板
- 24. 如何在PHP电子邮件模板中包含简码
- 25. 我如何将我的php代码包含在Html模板中?
- 26. 如何在Joomla模板中包含PHP函数的文件
- 27. 如何在Dojo Widget的另一个模板中包含模板?
- 28. 如何在通用模板中包含通用模板
- 29. 如何在Puppet模板中包含一个子模板
- 30. 如何在smarty模板文件中运行php代码?
您应该避免在模板中包含PHP。 – ThiefMaster
那么我该如何做到这一点。我只是需要PHP逻辑以某种方式 –
在您的PHP文件中执行它,并将其分配为模板var。 – ThiefMaster