• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to pass template variable to javascript?

#1
I need to use template variable in javascript, but I can not search the example.

For example:

Code:
<div>
    <xen:set var="$myServerTime">{$serverTime}</xen:set>
    <script type="text/javascript">
    <!--
    var currentTime = new Date($myServerTime)
    var month = currentTime.getMonth()
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write(day + "/" + month + "/" + year)
    //-->
    </script>
</div>
Thanks!
 

cclaerhout

Well-known member
#2
In your template:
HTML:
<div id="myUniqId"
    data-variable-1 = "$myVariable_1"
    data-variable-2 = "$myVariable_2"
    <xen:comment>myUniqId</xen:comment>
</div>
In your Javascript
Code:
var variable1 = $(#myUniqId).attr('data-variable-1'),
variable2 = $(#myUniqId).attr('data-variable-2');