We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44090
    • 11 Posts
    Good evening,

    I am trying to figure out how to streamline my site templates. How can I or is it possible to get the current page ID and use that ID to call a specific Chunk that corresponds to that specific page ID.

    For example: If the current page (Salon with an ID of 6) then use Chunk (salonSide (23) ).

    I was trying to use an If statement but this is as far as i have gotten....
    [[!If &subject=`[[*id]]` &operator=`ID` &then=`[[

    Any help will be great.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Which version of MODX are you using?
        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
        • 30912
        • 463 Posts
        This is a common one for us, but it depends on requirements.

        for example, if you only have a few needs for it then you could use If (im guessing from your syntax your on revo)

        [[If &subject=`[[*id]]` &operator=`EQ` &operand=`6` &then=`[[$salonSide23]]` &else=`[[&standardSidebar]]` 


        however if you need to have different chunks on each individual page - so lots of different chunks per page you might be better with a TV selector for setting a chunk on a page

        see this: http://wiki.modxcms.com/index.php/Create_TV-Based_Chunks - but make sure you get the revolution version of the snippet at the bottom.

        admittedly the second version needs you to select it for every page but it gives much more flexibility unless you plan on writing If statements for every ID value.

        edit:

        you could also use the If statments to set the sidebar based on parent if the subpages to a parent need to have the same chunk

        [[If &subject=`[[*parent]]` &operator=`EQ` &operand=`6` &then=`[[$salonSide23]]` &else=`[[&standardSidebar]]` 


        you would need to set the parent id of 6 with the first call however if you neededit to display on the parent page though - i would use AndIf in that instance (https://forums.modx.com/thread/?thread=63762&i=1)
          • 6629
          • 60 Posts
          If you just want to display one single chunk for each page, you could include the page id in your chunk name:

          Chunk name for page id 1: block_1
          Chunk name for page id 2: block_2
          and so on...

          In your template, include your chunk like this:
          [[$block_[[*id]]]]
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            Note that in the examples Tyreal2012 posted, all chunks inside the If snippet (both conditions) are parsed. That's important to realise if there's any snippets in those chunks. In my opinion DasItsch's solution is better.
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • 44090
              • 11 Posts
              Modx Revolution 2.1.0–2.2.7Quote from: sottwell at Jan 20, 2014, 10:46 PM
              Which version of MODX are you using?
                • 30912
                • 463 Posts
                Thanks for the point Mark
                  • 6629
                  • 60 Posts
                  Quote from: markh at Jan 21, 2014, 01:55 PM
                  Note that in the examples Tyreal2012 posted, all chunks inside the If snippet (both conditions) are parsed. That's important to realise if there's any snippets in those chunks.

                  To avoid this, the if-snippet should only return the name of the chunk, as MODX parses nested tags from inside to outside.

                  [[[[If &subject=`[[*parent]]` &operator=`EQ` &operand=`6` &then=`$salonSide23` &else=`$standardSidebar`]]]]


                  The if-snippet will return $salonSide23 or $standardSidebar.

                  After evaluation of the if-snippet, the document parser will see a new chunk-tag (note the extra square-brackets around the if-snippet).

                  So your snippet will be evaluated to [[$salonSide23]] or [[$standardSidebar]] and therefore only the needed chunk is processed.
                    • 30912
                    • 463 Posts
                    Dasltsch - thats the best explanation ive seen for that - read the article that was posted on MODx.com about ti but didnt understand - now i see i workign example its clicked.