| ||||||||||
Shopping cart software Solutions for online shops and malls | ||||||||||
|
X-Cart Home | FAQ | Forum rules | Calendar | Mark Forums Read | User manuals | Login |
Having trouble modifying a smarty variable inside javascript | ||||
|
|
Thread Tools | Search this Thread |
#1
|
|||||||
|
|||||||
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:
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.
__________________
Thanks. |
|||||||
#2
|
|||||||||
|
|||||||||
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
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
#3
|
|||||||
|
|||||||
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:
and then immediately do this Code:
__________________
Thanks. |
|||||||
#4
|
|||||||
|
|||||||
Re: Having trouble modifying a smarty variable inside javascript
I just tried your suggestion and same outcome...
Code:
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.
__________________
Thanks. |
|||||||
#5
|
|||||||||
|
|||||||||
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
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
#6
|
|||||||
|
|||||||
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
__________________
Thanks. |
|||||||
#7
|
|||||||||
|
|||||||||
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:
You can even inline the first 2 lines so it is like var test = Math.round(Math.random()) == 1 See if this works
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
#8
|
|||||||
|
|||||||
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.
__________________
Thanks. |
|||||||
#9
|
|||||||||
|
|||||||||
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?
__________________
Steve Stoyanov CFLSystems.com Web Development |
|||||||||
#10
|
|||||||
|
|||||||
Re: Having trouble modifying a smarty variable inside javascript
Im trying to randomly select between 2 files to present - sorta A/B testing.
__________________
Thanks. |
|||||||
|
Thread Tools | Search this Thread |
|
|
|
|||
X-Cart forums © 2001-2020
|