We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4188
    • 7 Posts
    Hi all,

    I have searched for this pretty much everywhere, and I know the answer should be obvious but I can’t find it.

    I have a PHP snippet that outputs a piece of javascript code.

    When I ran the snippet as is, everything worked, except for where the multi-dimensional array had [[ .... ]] That would get replaced by nothing (I am assuming that the template parser was doing it’s job there.)

    I tried loading it in to a TV, and outputing it that way. Same thing happened. Is there any way that I can output this information so that it works properly? (this is dynamically produced information so I can’t just take the output and stick it in a .js file)

    Output from Modx:
    var data = {"zoom":15,"garages":[{"id":"1","name":"Akatiff Lot","address":"N. Autumn Street North of Julian Street","phone":"(408) 295-7275","hours":"Open for arena events","rates":"Rates vary by event","discounts":"","directions_link":"http://maps.google.com/maps?q=N.+Autumn+Street+and+Julian+Street,+san+jose,+ca&spn=0.025608369289529875,0.028457164764404297&hl=en","points":}

    It is the points location that is the issue.

    What it is supposed to look like:

    var data = {"zoom":15,"garages":[{"id":"1","name":"Akatiff Lot","address":"N. Autumn Street North of Julian Street","phone":"(408) 794-1090","hours":"Open for arena events","rates":"Rates vary by event ($10-$20)","discounts":"","directions_link":"http://maps.google.com/maps?q=N.+Autumn+Street+and+Julian+Street,+san+jose,+ca&spn=0.025608369289529875,0.028457164764404297&hl=en","points":[[37.33830,-121.90116],[37.33867,-121.90134],[37.33853,-121.90189],[37.33826,-121.90262],[37.33775,-121.90408],[37.33632,-121.90245],[37.33693,-121.90151],[37.33676,-121.90129],[37.33656,-121.90108],[37.33693,-121.90048],[37.33785,-121.90134],[37.33802,-121.90099]]}]};

    The code that produces the above:

    ob_start();
    ?>
    <script language="JavaScript">
    <!--
    var data = <?php print_data($modx,$zoom); ?>;
    var poiLocations = [ null // 1-indexed.
    {foreach from=$points_of_interest item=p}
    , new GLatLng({$p->lat}, {$p->lng})
    {/foreach}
    ];
    //-->
    </script>
    <?php
    $output=ob_get_contents();
    ob_end_clean();
    $modx->setPlaceholder(’parking_data’,$output);

    I have tried simply echoing $output as well and still get the same thing.

    Template call:

    [[parking_display? &zoom=`15`]]
    [+parking_data+]

    Any suggestions?

    Thanks,
    Bryan



      • 23491 ☆ A M B ☆
      • 1,056 Posts
      Bryan,

      Your arrays are conflicting with the MODx Parser.
      [[snippets_go_here]]


      That said, see if you can introduce a SPACE before/after each opening/closing bracket to prevent the parser from thinking your JavaScript is a snippet smiley

      Example:
         var data = {"zoom":15,"garages":[{"id":"1","name":"Akatiff Lot","address":"N. Autumn Street North of Julian Street","phone":"(408) 794-1090","hours":"Open for arena events","rates":"Rates vary by event ($10-$20)","discounts":"","directions_link":"http://maps.google.com/maps?q=N.+Autumn+Street+and+Julian+Street,+san+jose,+ca&spn=0.025608369289529875,0.028457164764404297&hl=en","points":[[37.33830,-121.90116],[37.33867,-121.90134],[37.33853,-121.90189],[37.33826,-121.90262],[37.33775,-121.90408],[37.33632,-121.90245],[37.33693,-121.90151],[37.33676,-121.90129],[37.33656,-121.90108],[37.33693,-121.90048],[37.33785,-121.90134],[37.33802,-121.90099]]}]};
      


      ...would become...
         var data = {"zoom":15,"garages": [ {"id":"1","name":"Akatiff Lot","address":"N. Autumn Street North of Julian Street","phone":"(408) 794-1090","hours":"Open for arena events","rates":"Rates vary by event ($10-$20)","discounts":"","directions_link":"http://maps.google.com/maps?q=N.+Autumn+Street+and+Julian+Street,+san+jose,+ca&spn=0.025608369289529875,0.028457164764404297&hl=en","points": [ [ 37.33830,-121.90116 ] , [ 37.33867,-121.90134 ] , [ 37.33853,-121.90189 ] , [ 37.33826,-121.90262 ] , [ 37.33775,-121.90408 ] , [ 37.33632,-121.90245 ] , [ 37.33693,-121.90151 ] , [ 37.33676,-121.90129 ] , [ 37.33656,-121.90108 ] , [ 37.33693,-121.90048 ] , [ 37.33785,-121.90134 ] , [ 37.33802,-121.90099 ] ] } ] };
      
        Mike Reid - www.pixelchutes.com
        MODx Ambassador / Contributor
        [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
        ________________________________
        Where every pixel matters.
        • 4188
        • 7 Posts
        Thank you very much! It works!
          • 3749
          • 24,544 Posts
          BTW, you may already know this, but you can inject JS code into the header with


          $modx->regClientStartupScript()

          http://wiki.modxcms.com/index.php/API:regClientStartupScript


          or just before the </body> tag with

          $modx->regClientScript()

          http://wiki.modxcms.com/index.php/API:regClientScript
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting