|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Game Over! Smarty Rookie
Joined: 01 May 2019 Posts: 10
|
Posted: Wed Dec 18, 2019 9:07 am Post subject: How check if Smarty variable exists in via Java Script |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Dec 19, 2019 2:44 am Post subject: |
|
|
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 |
|
Game Over! Smarty Rookie
Joined: 01 May 2019 Posts: 10
|
Posted: Thu Dec 19, 2019 12:21 pm Post subject: |
|
|
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 |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Dec 21, 2019 12:32 pm Post subject: |
|
|
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 |
|
|
|
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
|