I've tried {include_php file="phpfile.php"} and {php} tags but both cause deprecated error. Can you not do this in Smarty anymore? I can't find anything in the docs.
Asked
Active
Viewed 8,372 times
3
Michael Berkowski
- 267,341
- 46
- 444
- 390
Mike Rifgin
- 10,409
- 21
- 75
- 111
-
5You should avoid including PHP in templates. – ThiefMaster Jul 08 '11 at 13:57
-
So how do I do this. I just need php logic somehow – Mike Rifgin Jul 08 '11 at 14:02
-
2Execute it in your PHP files and assign it as a template var. – ThiefMaster Jul 08 '11 at 14:03
3 Answers
2
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}
Linus Kleen
- 33,871
- 11
- 91
- 99
-
1yoda-style is ugly. especially in a case where it doesn't have *any* advantages – ThiefMaster Jul 08 '11 at 13:57
-
-
1@wonk0 [This would be an advantage.](http://stackoverflow.com/questions/4624536/which-one-will-execute-faster-if-flag-0-or-if-0-flag/4624562#4624562) – Linus Kleen Jul 08 '11 at 14:01
-
yes, poor reading; of course it makes more sense if a variable is part of the expression; but IMO you either use yoda-style or you don't; you just don't switch for every condition – wonk0 Jul 08 '11 at 14:03
2
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.
{include_php} is deprecated from Smarty, use registered plugins to properly insulate presentation from the application code.
If you include what you are trying to do in your phpfile.php, we can help you write a plugin function.
Paul DelRe
- 4,003
- 1
- 24
- 26
0
{include_php} is marked as deprecated in both Smarty2 and 3; {php} in Smarty3 only:
wonk0
- 13,402
- 1
- 21
- 15