We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35176
    • 193 Posts
    Why not bring the power of dynamically generating html through chunks, snippets, templates, and variables to all the other elements of a webpage?
      jealousy<>love
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can. I frequently use a snippet or a TV to load Javascript and CSS into a page so that it won’t be cluttering up the template if it’s not being used on the page. For example, if I’m using Jot/Notes to manage a list of some sort, I’ll enable the drag/sort feature for manager users only:

      <?php
      $js = '<script type="text/javascript">
      $("#one").dragsort({ dragSelector: "li", dragEnd: saveOrder, dragBetween: false, placeHolderTemplate: "<li></li>" });
      function saveOrder() {
          var data = new Array();
          $("div#sortable li").each(function(i, elm) { data[i] = $(elm).attr("id"); });
          $.post("[~18~]", { "ids[]": data });
      };
      </script>';
      
      if($_SESSION['mgrValidated']) {
          $modx->regClientStartupScript('assets/js/jquery.dragsort-0.3.10.min.js');
          $modx->regClientScript($js);
      }
      return;
      ?>


      Or for a page with video, just have this snippet somewhere in the content to load all the javascript and CSS for the video player, as well as loading and preparing a listing of the video files themselves, generated by a desktop application that reads Vimeo and other video storage services:
      <?php
      // load up the css and js files
      $modx->regClientCSS('assets/media/engine/css/videolightbox.css');
      $modx->regClientCSS('<style type="text/css">#videogallery a#videolb{display:none}</style>');
      $modx->regClientCSS('assets/media/engine/css/overlay-minimal.css');
      $modx->regClientStartupScript('assets/media/engine/js/jquery.tools.min.js');
      $modx->regClientStartupScript('assets/media/engine/js/swfobject.js');
      $modx->regClientStartupScript('assets/media/engine/js/videolightbox.js');
      // read and parse the generated file
      $filename = $modx->documentObject['alias'];
      $file = file_get_contents("assets/media/$filename.html");
      $gallery = explode('BODY', $file);
      $div = $gallery[1];
      $div = ltrim($div, 'section -->');
      $div = rtrim($div, '<!-- End VideoLightBox.com');
      $div = str_replace('src="data', 'src="assets/media/data', $div);
      return $div;
      ?>


      http://wiki.modxcms.com/index.php/API:regClientStartupScript
      http://wiki.modxcms.com/index.php/API:regClientScript
      http://wiki.modxcms.com/index.php/API:regClientCSS
      http://wiki.modxcms.com/index.php/API:regClientStartupHTMLBlock
      (regClientHTMLBlock is not documented, but behaves the same as regClientScript)

        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
        • 35176
        • 193 Posts
        new to that syntax, but you mean i can dynamically generate css using chunks and variables?
          jealousy<>love
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Yes, if you wanted to use a chunk you would first use getChunk(’chunkname’), then use that in the regClientStartupScript (or whatever you are doing).
            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
            • 35176
            • 193 Posts
            Sorry, I meant "dynamically generate CSS" using only chunks and TV’s, not script. Can we do that?
              jealousy<>love
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Well, a TV can use @CHUNK binding to pull in the content of a chunk. As far as using a TV for dynamic CSS, there’s any number of ways to make use of TVs. Have a set of stylesheet names in a radio button TV so you can select one. Use chunks and select the chunk with a radio button TV. Have a small <style> block in your template head, and use a TV to indicate what you want; again it can be a radio button TV (in Evo ManagerManager has a color picker widget you can use). Or you can use a TV to indicate a classname for the <body> tag, for example
              <body class="[*whichpage*]">

              and your CSS file can use the body.homepage (or whichever one the TV’s content specified for that page) to override any style for any element in that particular page. This is useful for hiding a sidebar in the homepage using
              body.homepage #sidebar{display:none;}


              Learn how TVs work, then use your imagination, since that’s the only limit. And Revo has even more options with parameters and settings for just about everything, as well as a very robust phx conditional library built-in.
                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
                • 35176
                • 193 Posts
                weeeeeee!
                laugh
                kiss
                  jealousy<>love
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  That’s MODx. And that’s why I’ve been having a blast developing websites with it for the last six years (or more? I’ve lost track of the time).

                  I really have to get a grip on Evo’s new custom TVs. That will add a whole new level of "whee!" to MODx development. How about a TV with a checkbox list of optional sidebar blocks that can be drag-sorted in the resource’s TV to determine their order of appearance on that individual page? Furthermore, the TV would generate its own list based on chunks with a specific naming convention, or just about any other way of storing the list that you can imagine. I’m pretty sure Revo could do this as well.
                    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
                    • 35176
                    • 193 Posts
                    does Revo have these same custom TV’s you mention?
                    why do any new features for Evo, shouldn’t all new features be developed for Revo?
                    isn’t Evo going to be phased out as soon as Revo is up to speed?
                      jealousy<>love
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      To answer your last question first, over my dead body!

                      Revo has its own answer to the custom TV issue. I’m not familiar enough with Revo to know how it works.

                      Here’s one discussion that might be useful
                      http://modxcms.com/forums/index.php/topic,52539.60.html
                        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