Error tagged with php in Smarty

Asked

Viewed 117 times

0

I have a question related to the Smarty 3.1 template engine, I’m trying to use the {php}{/php} tags in the templates and I’m getting the following error:

Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:/var/www/html/Caf/view/index.tpl" on line 67 "{php}" {php}{/php} tags not allowed. Use Smartybc to enable them <-- thrown in /var/www/html/Caf/lib/Smarty/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php on line 67

I know that if I use the Smartybc class instead of Smarty it will work normally, but I saw that this is a deprecated method in version 3.1 and that the Smartybc class is an adaptation class.

My question is: Since using the {php}{/php} tags is deprecated, what would be the correct way to execute php code in the templates in this new version?

Would be using the plugins Register https://www.smarty.net/docs/en/api.register.plugin.tpl ?

I tried to use the plugins Register with the getPage method of my routes class and it didn’t work

class

class Rotas {

    public static $pag;
    private static $dir_controller = 'controller';
    private static $dir_view = 'view';

    static function getPage() {
        if (isset($_GET['pag'])) {

            $pagina = $_GET['pag'];

            self::$pag = explode("/", $pagina);

            $pagina = 'controller/' . self::$pag[0] . '.php';

            if (file_exists($pagina)) {
                include $pagina;
            } else {
                include 'error.php';
            }
        }
    }

    static function getUrlBase() {
        return $_SERVER['DOCUMENT_ROOT'] . '/' . Config::APP_DIR;
    }

    static function getUrlHome() {
        return Config::APP_URL . '/' . Config::APP_DIR;
    }

    static function getTemplate() {
        return self::getUrlHome () . '/' . self::$dir_view;
    }

}

I’m trying to use it like this:

$smarty->registerPlugin("function", "getPage", Rotas::getPage());

1 answer

1

I found my problem, in case anyone has this doubt in the future the solution I found in my case was to use the Marty’s registerPlugin method which according to the documentation is recommended, and in my case described above the solution was like this:

$smarty->registerPlugin("function", "get_page", array("Rotas", "getPage"));

To call the plugin in the template just use the name you gave it, inside keys:

{get_template}

Note: the name of the plugin can not have the same name of your method/function because of the error (it took me to discover this rsrsrs).

registerPlugin documentation link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.