3

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.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111

3 Answers3

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
  • 1
    yoda-style is ugly. especially in a case where it doesn't have *any* advantages – ThiefMaster Jul 08 '11 at 13:57
  • where would it have advantages in your opinion? – wonk0 Jul 08 '11 at 14:00
  • 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.

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.

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