Use Smarty variables in JavaScript with innerHTML

1

I have Smarty variable (PrestaShop) {$cart_qties} . How can I use it in a script? For example

<script>
if ("{$cart_qties}" > 5) {
    document.getElementById("demo").innerHTML = "MAS de 5";   
}
</script>

<p id="demo">MENOS de 5</p>
    
asked by Jose 08.06.2017 в 12:52
source

3 answers

2

You could use the {literal}{/literal} keys to take into account the code Javascript and leave the variable unplaced inside these keys, as indicated in the Smarty place :

{literal} 
if ({/literal}{$cart_qties}{literal} > 5) {
    document.getElementById("demo").innerHTML = "MAS de 5";   
}
{/literal}
    
answered by 08.06.2017 в 13:07
0

Very good, you have to bear in mind that the keys that are used to indicate that they are smarty variables, are also used in the javascript language, if you want to use a variable in javascript, the easiest thing would be something like this:

   
<script> 
var numero = {$cart_qties};
{literal} 
if (numero > 5) {
   document.getElementById("demo").innerHTML = "MAS de 5"; 
}
{/literal}
</script> 

As you can see, what I do is get the values before using the javascript keys, which are included within the literal tag.

Greetings, I hope I have helped.

    
answered by 08.06.2017 в 17:27
-1

for example this code works well

{if {$cart_qties} < 5 }
<span> menos de 5 </span>
{else}
<span> mas de 5 </span>
{/if}

But without innerHTML I have to refresh the page to update what has to appear. That's why I want to put it in script

    
answered by 08.06.2017 в 13:19