smarty-php / smarty

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

How it's possible to modifiy tpl_vars subkey inside a plugin function #1042

Closed Zakwil closed 3 months ago

Zakwil commented 3 months ago

In smarty 4 I used a function to modify subkey of tpl_vars, but I tried to transfert this one in a method and I think my reference on $smarty is not working anymore. And the value in tpl_vars is not modified after the fetch

Previous (smarty4) :

function smarty_function_assign_subkey($params, &$smarty)
{
    $vVar       = trim($params['var'], "'");
    $vSubkey    = trim($params['subkey'], "'");
    $vValue     = trim($params['value'], "'");

    if (!isset($params['cat'])) {
        $smarty->tpl_vars[$vVar]->value[$vSubkey] = $vValue;
    } else {
        $smarty->tpl_vars[$vVar]->value[$vSubkey] .= ','.$vValue;
    }
}//smarty_function_assign_subkey

New (smarty5) :

public static function assign_subkey($params, $smarty) {
    $vVar       = trim($params['var'], "'");
    $vSubkey    = trim($params['subkey'], "'");
    $vValue     = trim($params['value'], "'");

    if (!isset($params['cat'])) {
        $smarty->getSmarty()->tpl_vars[$vVar]->value[$vSubkey] = $vValue;
    } else {
        $smarty->getSmarty()->tpl_vars[$vVar]->value[$vSubkey] .= ','.$vValue;
    }
}//assign_subkey
wisskid commented 3 months ago

Please use $smarty->assign() instead of accessing tpl_vars directly: https://smarty-php.github.io/smarty/5.x/api/variables/assigning/

Zakwil commented 3 months ago

Ok I tried $smarty->getSmarty()->append('data', array($vSubkey => $vValue), 1); But the scope of the object is related to the fetch, I do 2 fetch : one with the body of the page and the other with the main structure. And the 1st fetch doesn't modify the object and the 2nd fetch don't have the value.

Zakwil commented 3 months ago

Ok I saved the variable of the fetched object and pass it to my main object.