smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.22k stars 701 forks source link

Variable as a parameter in Smarty 5 #997

Closed LPRKR closed 2 months ago

LPRKR commented 2 months ago

Can we still pass the variable into the tag parameter in Smarty 5?

In the prior versions, I used it like this and it worked: {embed type='JS' url='/js/categories.js' cdn=$CDN_URL}

Now, the "cdn" parameter, is equal to the string below instead of the $CDN_URL value: "$_smarty_tpl->getValue('CDN_URL')"

wisskid commented 2 months ago

that seems wrong. I'll investigate.

LPRKR commented 2 months ago

I have did some more analysis... The problem occur when I pass a PHP constant into my custom Tag. It works properly when I pass a string.

Compiled .tpl file result (from cache): WITH CONSTANT: <link rel="stylesheet" href="$_smarty_tpl->getValue('CSS_URL')"/> WITH STRING: <link rel="stylesheet" href="css_url_here"/>

Seems like it's missing the <?php echo ... ?> part in this situation.

wisskid commented 2 months ago

Could you share the code of your implementation for embed?

LPRKR commented 2 months ago

Sure! So, I call it like this:

1) in .php file - setting the Smarty variable from global variable in PHP $app->smarty->assign('JS_TABLEDND', JS_TABLEDND);

2) in .tpl file - calling extension like this {embed type="JS" url=$JS_TABLEDND}

3) extension code

<?php
namespace SmartyExtensions;
use Smarty\Compile\Base;

class EmbedTag extends Base {
    protected $required_attributes = ['type', 'url'];
    protected $optional_attributes = ['cdn'];

    public function compile($params, \Smarty\Compiler\Template $template, $parameter = array(), $tag = null, $function = null):string
    {
        $_attr = $this->getAttributes($template, $params);

        if (!empty($_attr['type']) && !empty($_attr['url']))
        {
            $cdn_loc = '';

            if (isset($_attr['cdn']))
                $cdn_loc = $_attr['cdn'];

            return \PageElement::embed(trim($_attr['type'], '\'"'), $cdn_loc.$_attr['url'], false, false);
        }
    }
}

If you wonder why I trim the type variable, it's because I tried to make it work with Smarty 5. For some reason the string is surrounded by " characters, that's a different behaviour from Smarty 4.

4) how it looks on final HTML page: <script src="$_smarty_tpl->getValue('JS_TABLEDND')"></script>

wisskid commented 2 months ago

Are you sure you want to compile your tag? Because then you cannot use any of the parameters the way you do, because they would no longer be dynamic. Maybe you want to write a FuncionHandler instead?

LPRKR commented 2 months ago

Oh. I tried to follow the documentation but it seems I got it all wrong. Is there some sample of FunctionHandler usage somewhere? I have few plugins used in 4.x that I need to make work again.

wisskid commented 2 months ago

Just have a look at \Smarty\Extension\DefaultExtension::getFunctionHandler and the various function handlers listed there. That should give you enough cloes on how to implement your own function handler.