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

    Just started with MODx.. and need a little help converting my existing template structure into MODx..

    I have PHP objects that setup things like resource caching etc. Quite often in my existing template, I use JS code such as:

    CLIENT_OBJECT.image_cache = <?php echo SERVER_OBJECT.getImagesArrayForCaching(); ?>;


    .. to write PHP values and objects directly into Javascript objects.

    I cannot figure out how to do this in MODx - Chunks cannot contain PHP and a snippet with the above code in simply doesn’t work.

    Where am I going wrong? Are snippets and chunks even the mechanism that I need to use to achieve this?

    TIA

    TheFoot
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can use a snippet in your javascript just as you would have used <?php ... > tags. If echo doesn’t do what you expect, try using return instead.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 696
        • 46 Posts
        You would need to create a snipit say we call it

        Caching - With the php code in it like :-
        <?php echo SERVER_OBJECT.getImagesArrayForCaching(); ?>


        Then in the page you would have the script
        CLIENT_OBJECT.image_cache = [[Caching]];


        though that is in evolution in revolution the snippet call would be different.

          • 29692
          • 3 Posts
          Thanks for your posts...

          I still cannot get it to work. The following is in a snippet called [[header]]:
          <?php
          session_start();
          	include_once('assets/include/server/thefoot.php');		
          	
          	// Page initialisation
          	$v_error_msg = "";
          	$THEFOOT->init(); // This function populates a string property with a JSON array
          ?>
          


          Here is the snippet that I use [[js-cache-images]], to get at the images array:

          <?php
          echo $THEFOOT->json_images;
          ?>
          


          In my page template, the first line contains the header snippet call [[header]], then in the <HEAD> javascript section I have:

                          // Prime the image cache 
          		THEFOOT.o_cache_images = [[js-image-cache]];
          


          Nothing is output from the snippet. I have tried return and echo. However, if I output a constant string such as:
          <?php
          echo "{\"images\": []}";
          ?>
          


          .. then it works fine.

          In the [[header]] snippet, I output (echo’d) the result of the $THEFOOT->json_images, and I know that the script executes and populates the array. It seems to get lost between the 2 snippet calls. Any ideas?

          Thanks, TheFoot
            • 4172
            • 5,888 Posts
            you can try it with modx-placeholders:

            <?php
            session_start();
            	include_once('assets/include/server/thefoot.php');		
            	
            	// Page initialisation
            	$v_error_msg = "";
            	$THEFOOT->init(); // This function populates a string property with a JSON array
            
                    $modx->setPlaceholder('jsImageCache',$THEFOOT->json_images);
            
            ?>


                            // Prime the image cache 
            		THEFOOT.o_cache_images = [+jsImageCache+];
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 29692
              • 3 Posts
              Brilliant! grin

              Works perfectly - thanks....