We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19872
    • 1,078 Posts
    Is there a way to make one TV selector control two different chunks appearing in different locations on a page? Or would I need to make to selectors. Maybe some kind of IF statement. If checked place the script chunk in the footer.

    Basically trying to set up an option for some pages in my site. One option is to display the content region as standard html. The other is to display it in conjunction with read more js. My option with read more also has foundation 5 visibility classes incorporated so that on desktop, the content displays as standard html, but on touch devices, the read more functionality is brought in.

    Maybe I can make a selector for two chunks with my content display options, and just include the read more javascript right there below the content. Instructions said to put it near the footer, but I don't know how strict that is.

    Is just seems awkward to make and additional selector that would insert a js chunk in the footer. Site managers might check the one box to display text with read more functionality, forget to check the additional box to insert the script, and then become frustrated that it's not working.

    There must be other instances like this where the option to place some type of item or functionality on the page, also requires a js file, or js link or css link to be inserted as well, and I'm just not grasping how best to handle it.

    Any feedback or tips greatly appreciated.

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      If you have a chunk that requires specific JS, put a snippet in the chunk with $modx->regClientScript() and $modx->regClientCSS() as needed. The javascript and CSS will get loaded along with the chunk.

        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
        • 19872
        • 1,078 Posts
        Thanks Susan. I put the regClientsscript with my path to the js file into a snippet. Not sure I have the snippet written correctly though. Searching for a how to, or some kind of visual guide to follow.

        Does the regClientScript automatically insert the js down near the closing body tag? Or do I need a place holder on my template?

        I have the snippet called in my chunk, and the TV is checked to display the chunk on the page. This makes me think that my snippet is not written correctly.

        [ed. note: mmcgee last edited this post 11 years, 10 months ago.]
          • 19872
          • 1,078 Posts
          Wow! Got it to work. It stuck the .js file just above the closing body tag. Me like this!
            • 19872
            • 1,078 Posts
            In addition to the js file, I also have a short javascript. In my current template, I have this script below the link to the js file. Can I put javascript into a snippet. Or, should I use the regClientHTML?

            Hmmm? Perhaps I can put both in one snippet.
              • 19872
              • 1,078 Posts
              Well, adding the regClientHTMLBlock did not go so well. A parse error occurred.

              Parse error: syntax error, unexpected 'article' (T_STRING) in /public_html/core/cache/includes/elements/modsnippet/35.include.cache.php on line 16

              Perhaps I need to escape the quotes?

              This is my snippet

              <?php
              /**
               * addReadMore Snippet
               *
               * DESCRIPTION
               *
               * This Snippet add the readmore.js to the bottom of the page as well as the script that
               * targets which div to run the script on.
               */
              
              $modx->regClientScript('assets/js/readmore.min.js');
              
              $modx->regClientHTMLBlock('
              <!--BEGIN READMORE-->
                <script>
                $('article').readmore({
                  moreLink: '<a href="#">Read more ></a>',
                  speed: 500,
                  maxHeight: 200,
                  afterToggle: function(trigger, element, expanded) {
                    if(! expanded) { // The "Close" link was clicked
                      $('html, body').animate( { scrollTop: element.offset().top }, {duration: 1000 } );
                    }
                  }
                });
                </script>
              <!--END READMORE-->');
                • 19872
                • 1,078 Posts
                Escaping the quotes did the trick. Wow! This is very cool. Thank you Susan.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  No, assign your script to a variable, just as you would put it in the content. Then use regClientScript with the variable as the parameter.
                  $js ='<script>your javascript code</script>';
                  $modx->regClientScript($js);

                  Go to the core/model/modx/modx.class.php file, beginning at line 1542 to see how these various regClientxx() functions work.
                    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
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Take care of the order in which you put the regClient functions in your snippet; you need to put the main library file first, then any inline script that uses that library. MODX will load the scripts in the order in which it finds them in the snippet.
                      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
                      • 19872
                      • 1,078 Posts
                      Hey Susan: Thanks for the additional tips. I interpret "variable" as any name I want, which makes sense because I might have many, and would not want any confusion to occur.

                      These classes for me are like going to your regular lunch spot and one day realizing that all along they also serve pie. Yum!