1. 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?

Discussion in 'XenForo Development Discussions' started by marring, Dec 2, 2012.

  1. marring

    marring Member

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

    For example:

        <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)
  2. cclaerhout

    cclaerhout Well-Known Member

    In your template:
    <div id="myUniqId"
        data-variable-1 = "$myVariable_1"
        data-variable-2 = "$myVariable_2"
    In your Javascript
    var variable1 = $(#myUniqId).attr('data-variable-1'),
    variable2 = $(#myUniqId).attr('data-variable-2');
    KiF likes this.

Share This Page