Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Having trouble modifying a smarty variable inside javascript

 
Reply
   X-Cart forums > X-Cart 4 > Changing design
 
Thread Tools Search this Thread
  #1  
Old 02-07-2020, 07:25 AM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 77
 

Default 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 } <script language="JavaScript" type="text/JavaScript"> {literal} let randNum = Math.round(Math.random()); if(randNum == 1){ {/literal} "{$testB = true}" {literal} console.log('here'); } else { {/literal} "{$testB = false}" {literal} console.log('here2'); } console.log( randNum ); console.log( {/literal} "{$testB}" {literal} ); {/literal} </script>

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.
Reply With Quote
  #2  
Old 02-07-2020, 11:22 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,881
 

Default 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
Reply With Quote
  #3  
Old 02-07-2020, 11:26 AM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 77
 

Default 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'}
my output will be "hello2".
__________________
Thanks.
Reply With Quote
  #4  
Old 02-07-2020, 11:32 AM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 77
 

Default Re: Having trouble modifying a smarty variable inside javascript

I just tried your suggestion and same outcome...

Code:
{assign var= "testB" value= false } <script language="JavaScript" type="text/JavaScript"> {literal} let randNum = Math.round(Math.random()); if(randNum == 1){ {/literal} "{assign var='testB' value=true}" {literal} } else { {/literal} "{assign var='testB' value=false}" {literal} } console.log( randNum ); console.log( {/literal} "{$testB}" {literal} ); {/literal} </script>

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.
Reply With Quote
  #5  
Old 02-07-2020, 11:37 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,881
 

Default 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
Reply With Quote
  #6  
Old 02-07-2020, 11:46 AM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 77
 

Default 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.
Reply With Quote
  #7  
Old 02-07-2020, 12:07 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,881
 

Default 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 *} <script> var test; test = Math.round(Math.random()) == 1; if (test) {ldelim} {assign var="testB" value=true} {rdelim} </script>


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
Reply With Quote
  #8  
Old 02-07-2020, 12:15 PM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 77
 

Default 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.
Reply With Quote
  #9  
Old 02-07-2020, 12:48 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 13,881
 

Default 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
Reply With Quote
  #10  
Old 02-07-2020, 12:50 PM
 
simetria simetria is offline
 

Advanced Member
  
Join Date: Jul 2013
Posts: 77
 

Default Re: Having trouble modifying a smarty variable inside javascript

Im trying to randomly select between 2 files to present - sorta A/B testing.
__________________
Thanks.
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Changing design


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 01:32 AM.

   

 
X-Cart forums © 2001-2020