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

XF 1.5 Template Syntax inside a javascript

Discussion in 'XenForo Questions and Support' started by SmauG, May 22, 2016.

  1. SmauG

    SmauG Member


    I'm trying to implement a dynamic chart on a custom page, but i don't really have so much clues about how to do it without the <xen:foreach>.

    Basically i have an array from my model which has some integers, and i need to parse them on this way:

    <xen:foreach loop="$loops" value="$loop" i="$i">
    [{$i}, {$loop.a}, {$loop.b}, {$loop.c}]

    Of course on the template works great ([0,x,y,z], [1,x1, x2, x3], ...) but i need to build/read it inside the javascript script, and the template syntax doesn't work inside javascript.

    Basically i need to get this inside the javascript to build the chart.
    <script type="text/javascript">
    data.addRows([ [0,x,y,z], [1,x1, x2, x3], ... ]);

    How can i do it?
  2. Jeremy P

    Jeremy P Well-Known Member

    Have you considered just putting the JavaScript inside of a template instead of in an external file?
  3. SmauG

    SmauG Member

    It's everything on the template. When i said custom page, i meant XenForo custom page (template), sorry if wasn't clear.

    Basically, i need this working on the template:

    <script type="text/javascript">
    <xen:foreach loop="$loops" value="$loop" i="$i">
    [{$i}, {$loop.a}, {$loop.b}, {$loop.c}]

    Well, it just has to be a way to parse an array from a template ( {$loops} ) inside the javascript. A for on the javascript would do the trick, but again, the array from template is the problem:

    <script type="text/javascript">
    for(j =0; j < {$loops}.lenght; ++){
    data[j +1]=[[j], {$loops.x}[j], {$loops.y}[j]];

    It just doesn't simply read the $loops

    But the foreach and template variables can't be used inside the javascript.
    Last edited: May 22, 2016
  4. SmauG

    SmauG Member

    Well, i just rewrote it and now it's working. Didn't really changed anything, actually using <xen:foreach> inside the <script>. So confused but ok, at least is working.

Share This Page