X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Having trouble modifying a smarty variable inside javascript (https://forum.x-cart.com/showthread.php?t=77615)

simetria 02-07-2020 07:25 AM

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.

cflsystems 02-07-2020 11:22 AM

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

simetria 02-07-2020 11:26 AM

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".

simetria 02-07-2020 11:32 AM

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.

cflsystems 02-07-2020 11:37 AM

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

simetria 02-07-2020 11:46 AM

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

cflsystems 02-07-2020 12:07 PM

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

simetria 02-07-2020 12:15 PM

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.

cflsystems 02-07-2020 12:48 PM

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?

simetria 02-07-2020 12:50 PM

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 12:25 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.