Having trouble modifying a smarty variable inside javascript
I have a smarty variable assigned to false. Then within Javascript I will be running a function to return whether that smarty variable should change to true or stay false. At the moment I'm always getting the else portion of the js if statement.
This is a very simple example, but it gets the point across... If the random number generated is 1 change the smarty variable to true otherwise keep it at false. Code:
{assign var= "testB" value= false } When I run console.log() inside the if statements he shows me that it iterates through the if statement correctly, but the smarty variable will always change to false, or whatever I set it to inside the else portion. So with the above code, if randNum is 1 it will console log 'here', but the smarty variable will be false - or whatever I set it to inside the else. Thank you all. |
Re: Having trouble modifying a smarty variable inside javascript
This
{$testB = true} is not a valid smarty expression. It won't do anything, it won't run. You need to repeat the assign function {assign var="testB" value=true} same for the "false" value |
Re: Having trouble modifying a smarty variable inside javascript
Steve,
Is that strictly within javascript, or in general for smarty? because if I do this: Code:
{assign var= "testB" value= 'hello'} and then immediately do this Code:
{$testB = 'hello2'} |
Re: Having trouble modifying a smarty variable inside javascript
I just tried your suggestion and same outcome...
Code:
{assign var= "testB" value= false } This code will always - still - return the assigned "testB" from inside the else. Even if I remove the initial assign - outside the js - I still get the same result. |
Re: Having trouble modifying a smarty variable inside javascript
Sorry I wasn't paying much attention.
Math.random will never return 1. It returns random number between 0 and 1. It may return 0 but 1 is not included. So your if statement will always fall into the else clause |
Re: Having trouble modifying a smarty variable inside javascript
It will cause I'm rounding it up - or down with Math.round()
Here is a quick screenshot of error thats occurring... http://simetriastudio.com/C/TST/ex.jpg |
Re: Having trouble modifying a smarty variable inside javascript
The only thing I can think of is that the smarty expression is running regardless of the js code. I think in the newer versions you don't even have to use the literal tag if they are on their own line. So basically you have the last one overwriting even though you think it shouldn't run.
There is no need to do this though. Code:
{assign var="testB" value=false } {* initial value is assigned *} You can even inline the first 2 lines so it is like var test = Math.round(Math.random()) == 1 See if this works |
Re: Having trouble modifying a smarty variable inside javascript
Thanks Steve.
But I still get the same result :? You think this is a Smarty bug? Running your code always returns "true". It seems to always run the smarty code inside the js "if" statement whether it passes or fails in respect to javascript... kinda sucks... I even went ahead and downloaded smarty and created a seperate project - thinking it may be some x-cart restrictions to smarty or something - but no dice, still the same behavior. |
Re: Having trouble modifying a smarty variable inside javascript
I don't think it is XC bug. I guess since it is smarty function always executes. I don't know.
What are you trying to do? Why use js? Can't you just use smarty function or modifier to get the value? |
Re: Having trouble modifying a smarty variable inside javascript
Im trying to randomly select between 2 files to present - sorta A/B testing.
|
All times are GMT -8. The time now is 04:05 AM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.