[Solved] Apply a smarty modifier to JS?


I’m afraid you cannot operate on JavaScript variables to get their value. Smarty can operate only on its variables – they either come from PHP or are set in Smarty. JavaScript variable cannot be set anyway to Smarty so you cannot do it.

But in case you want to assign PHP/Smarty variable with modifier to JavaScript it should be possible:

In PHP:

$price="1";
$smarty->assign('price', $price);

Smarty modifier:

function smarty_modifier_cash($value)
{
    return '£ '.number_format($value,2);
}

Smarty template file:

{$price|cash}


<script type="text/javascript">
{literal}
    var cash = {/literal}'{$price|cash}'{literal};
    alert (cash);
{/literal}
</script>

As a result you have displayed:

£ 1.00 

on your site and get JavaScript alert with:

&pound; 1.00

solved Apply a smarty modifier to JS?