We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19368
    • 40 Posts
    Hey folks,

    I an a relative noob with modX. Have done some good things with it for people but need to dive a bit deeper.

    I have a page header that varies between pages in a couple of respects. Using a standard snippet I can achieve what I want easily.

    But what I want to do is get the header into a chunk to make it easier to edit and clean up code.

    So how do I pass a variable value through a chunk to a snippet?

    e.g. my snippet is [[header-image? &name="about"]] my chunk is {{header}}

    If I have the chunk on each page, how can I pass the name variable through header chunk to the header-image snippet so my pages display the right image?

    Thanks in advance.
      • 3749
      • 24,544 Posts
      There are various ways to do what you want.

      To do it the way you describe (more or less), you’d probably use the snippet parameter to set a placeholder:

      $modx->setPlacehoder('placeholderName',$param);


      put that placeholder in the chunk, then retrieve the chunk in the snippet with:

      $output = $modx->getChunk('ChunkName');
      return $output;


        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
        • 19368
        • 40 Posts
        Thanks bobray

        Yes, I was aware of that...but how do I get the variable into the chunk?

        If I have the chunk on several pages, how do I pass the variable into the chunk so I can then toss it to the snippet.

        In your example, were/how do I set the value of $param? in the page that calls the chunk?

        Can I do something like {{chunkname? &param="about"}} I don’t think so can I?
          • 3749
          • 24,544 Posts
          If the variable is passed as a snippet parameter (let’s call it &param1), it’s available in the snippet as $param1. If you have the placeholder [+MyParam+] in the chunk and the snippet sets it:

          $modx->setPlaceholder('MyParam',$param1);


          you’ve passed the value from the page containing the snippet to the chunk.

          I’m not totally clear on what you’re trying to do and why, but I think you could also start over using TVs and forget about the snippet. Just put the path to the image in a TV, and put this in the chunk:

          <img src="[*TvName*]">


          The Input option values of the TV could be a drop-down list or radio options showing the various image paths.
            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
            • 19368
            • 40 Posts
            Thanks for the reply, but still not right.

            Here’s what I am trying to do:

            I have multiple pages...for simplicity let’s just say "about" and "contact"

            I have a standard header except that the background colour and an image in the header vary according to which page.

            Setting the colour is easy with embedded style.

            But what I want to do is change
             <img src="imagename.png">
            based on what page it is.

            Maybe I am going about it the wrong way, but I thought of making the header as a chunk and then using a snippet to change the value of the image name, so the chunk would look like:

            <img src="[[image? &name=`about`]]".png>


            Then when I call the chunk from the about page I would need to say something like {{chunk &name=`about’}} but I do understand you can’t do that.

            I get the idea of a placeholder, but how do I send the value of the current page name e.g. "about" into the chunk?

            Sorry this is going a bit in circles, I am sure we will get there.
              • 29019 ☆ A M B ☆
              • 104 Posts
              I think what Bob is suggesting is making a template variable for your "about" and "contact" pages. This template variable would be an image that you would choose while editing those pages, then in your chunk you’d simply put

              <img src="[*image-tv-name*]" />


              Which would put the image that you chose from that page into the path (assuming you don’t use any widgets).

              If you’re still looking for a way of calling a snippet from a chunk with a custom attribute, you can do that the same way:

              <img src="[[Snippet?&image=`[*image-tv-name*]`]]" />


              It sounds like the first example is all you’d need though.
                www.darkstardesign.com

                Are you in the Nashville area? Join us for our next MODX meetup!
                • 19368
                • 40 Posts
                Thanks guys,

                Your help is much appreciated.

                I think I need to start learning about TVs smiley
                  • 3749
                  • 24,544 Posts
                  Right. Swespi explained it much better than I did.

                  A TV is just another resource field (like content, pagetitle, menutitle, etc.) that you create. The TV’s value can be inserted with a tag like this [*tvName*]. The content of the TV will replace the tag. You should just need a single TV (let’s call it ImagePath).

                  Create the TV and attach it to the template that’s used for your pages.

                  If you want to make it really slick, use radio options for the input type of the TV and in the Input Options Value field of the TV, put something like this:

                  Image1==path/to/img1||Image2==path/to/img2||Image3==path/to/img3


                  (No spaces in the line above)

                  Do as swespi suggests with a tag in the chunk (or in the template). When you edit the resource, you’ll see radio buttons for Image1, Image2, Image3. The tag will be replaced with the part after the == (the path to the image).

                  <img src="[*ImagePath*]">
                    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
                    • 19368
                    • 40 Posts
                    OH MAMA!!! grin grin grin grin grin grin grin grin grin grin grin

                    HOW COOL IS THAT!

                    Thanks so much. Not only did I achieve something in a much easier way than I expected, I leapfrogged to a new level with ModX.

                    I sincerely appreciate your support - thanks!
                      • 19368
                      • 40 Posts
                      OK, so now that I am cookin’ here, is it possible to have a TV set two different values?

                      So I now have it setting the image name, can it at the same time set a background colour that is associated with that page?

                      Did I mention that I am way impressed with TVs now I see what they can do? smiley