Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

How check if Smarty variable exists in via Java Script

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Game Over!
Smarty Rookie


Joined: 01 May 2019
Posts: 10

PostPosted: Wed Dec 18, 2019 9:07 am    Post subject: How check if Smarty variable exists in via Java Script Reply with quote

Hello guys.

I have an array in my PHP and I pass this variable to Smarty TPL file.

Code:

 $smarty->assign('outsourceJson', json_encode($outsourceTemp , JSON_UNESCAPED_UNICODE));


The question is how can I check if this variable exists with JS?
I wrote this code :

Code:

var phaseOutsource = JSON.parse('{$outsourceJson}');


but if the variable doesn't exists it will give me error :

Code:

VM174:1 Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at


I even checked if variable exists via this code :

Code:

if( {$outsourceJson} ){
 alert("It works");
}


If $outsourceJson exists , it works fine but if doesn't it will give me error :

Code:

(index):1596 Uncaught SyntaxError: Unexpected token ')'

and here is source of my HTML :

Code:

 if(   ){
 alert("It works");
}

as you can see {$outsourceJson} converted to nothing.

So would you please suggest me a way to check if variable exists?

Thanks a bunch
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 265
Location: West Coast

PostPosted: Thu Dec 19, 2019 2:44 am    Post subject: Reply with quote

It will be revealed that {$outsourceJson} exists in a Smarty template - and when Smarty compiles that template, the code {$outsourceJson} gets replaced with the intended value.

The finalized HTML then gets sent out to the visitor's browser where any javascript execution happens.

You can load up the HTML with a Smarty statement like:
Code:
{if $outsourceJson}{$outsourceJson}{else}<!-- Sorry, nothing was assigned to the Smarty variable $outsourceJson -->{/if}
Back to top
View user's profile Send private message
Game Over!
Smarty Rookie


Joined: 01 May 2019
Posts: 10

PostPosted: Thu Dec 19, 2019 12:21 pm    Post subject: Reply with quote

bsmither wrote:
It will be revealed that {$outsourceJson} exists in a Smarty template - and when Smarty compiles that template, the code {$outsourceJson} gets replaced with the intended value.

The finalized HTML then gets sent out to the visitor's browser where any javascript execution happens.

You can load up the HTML with a Smarty statement like:
Code:

{if $outsourceJson}
        {$outsourceJson}
{else}
        <!-- Sorry, nothing was assigned to the Smarty variable $outsourceJson -->
{/if}


Thank you a billion dear bsmither . You have helped a lot. Best wishes for you Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1615

PostPosted: Sat Dec 21, 2019 12:32 pm    Post subject: Reply with quote

Since JSON is a valid JS object, you don't need JSON.parse.
Just
Code:
var phaseOutsource = {$outsourceJson};

is enough.
And if for some reason the needed variable is not assigned to the template, that's a bug in your code.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP