Smarty: how to include php in template?
I've tried {include_php file="phpfile.php"}
and {php}
tags开发者_如何学Go but both cause deprecated error. Can you not do this in Smarty anymore? I can't find anything in the docs.
I circumvented this problem. Create a plugin file named block.php_code.php
with this function in it:
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();
}
In your template, you can then write:
{php_code}{literal}<?php
print "Hello, world!";
?>{/literal}{/php_code}
They are depreciated for a reason as they allow poor practices. Smarty recommends putting the included script into the PHP logic or creating a plugin (which is simple).
{php} tags are deprecated from Smarty, and should not be used. Put your PHP logic in PHP scripts or plugin functions instead.
Source
{include_php} is deprecated from Smarty, use registered plugins to properly insulate presentation from the application code.
Source
If you include what you are trying to do in your phpfile.php
, we can help you write a plugin function.
{include_php} is marked as deprecated in both Smarty2 and 3; {php} in Smarty3 only:
- http://www.smarty.net/docsv2/en/language.function.include.php.tpl
- http://www.smarty.net/docs/en/language.function.include.php.tpl
- http://www.smarty.net/docs/en/language.function.php.tpl
精彩评论