We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33657
    • 128 Posts
    so for example sake.

    i have a file "custom.js"

    and in custom.js I want to say the name of my Page I access for example.

    I'd have to embed the script in the Master Page.

    <script>
    console.log("[[*pagetitle]]");
    </script>


    would it be possible to make a resource somehow and have it output a javascript? like I make a chunk or resource or something and it outputs as .js and it does ModX magic inside it so the file itself could be dynamic? The only way I currenty can do this is by embedding ALL my scripts in the master page rather than loading them remote.

    Alternatively, Is there a way to "Minify" javacsript thats been embedded? maybe thats a easier way to do it but still keep the ability for ModX to compile?
      • 3749
      • 24,544 Posts
      I'm not completely clear on what you're trying to do, but this might be what you want:

      Put the JS in a chunk, let's call it 'MyJsChunk' with placeholders for the dynamic stuff.

      Then put a snippet at the top of the page with code like this (the values on the left are the names of the placeholders, the ones on the right are the values you want to replace them with):

      <?php
      $fields = array(
          'pagetitle' = $modx->resource->get('pagetitle'),
          'longtitle' = $modx->resource->get('longtitle'),
          /* etc. */
      );
      
      $tpl = $modx->getChunk('MyJsChunk', $fields);
      $modx->regClientStartupScript($tpl);
      return '';


      If all the fields are resource fields from the current resource (No TVs), you can replace the $fields array with this:

      $fields = $modx->resource->toArray();
        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