Smarty server variable inside .js file

I have the following code:
[PHP]

var data1 = {rss_source1: ‘{$myvars.myvariablename}’};
function app()
{
this.myfeed = data1.rss_source1
// this.myfeed= “data1.rss_source1”
}

[/PHP]

As we know PHP executes before the page is loaded and I have defined var to read Smarty variable. Issue is that PHP has already been parsed when JavaScript runs. Both options will not work. How to solve this issue?

Storing them as properties of an object.
[PHP]
var data1 = {rss_source1: ‘{$myvars.myvariablename}’};
[/PHP]

Is this correct as source feed is not detected.

It’s unlikely that PHP is the culprit here.

  • if this is a separate JS file, it is not run through Smarty
  • if it’s a code block inside a Smarty template there are two further possibilities:
    • the JS source is inside a {literal} block => Smarty variables are ignored
    • otherwise you get a Smarty error (unknown identifier rss_source1) => Smarty tries to read your object literal as Smarty variable, and fails

So, in your opinion there is any option to set constant value from Smarty inside .js?

function app()
{
    // omit the quotes if the value is not a string
    this.myfeed = "{$myvars.myvariablename}";
}

(obviously outside {literal} blocks)

It will not detect constant such as URL
this.feed1 = $myvars.var1
and also not
this.feed1 = ‘’$myvars.var1’’

why should it? that’s not smarty syntax.

In this moment I do not have any solution.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.