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

    I am new at this and am trying to minimize creating too many templates for different page layouts. I have a whole list of chunks that I only want to show and hide depending on the actual page. What type of "if an when statement" can I use to hide and show these. This might be silly example but I am sure you understand what I mean.

    E.g.: If Pageid = 1 then
    [[$Example]]
    else
    [[$ExampleB]]
    End if

    thanks for you help!!
      • 16278
      • 928 Posts
      You can do this with snippet calls in your template such as [!mySnippet?page=`[*id*]`!], and snippets that use $modx->getChunk() or $modx->parseChunk API calls selected by if/else if/else or switch structures, and return the required chunk’s content.

      I find keeping track of more than a few page IDs confusing. If you use friendly URLs, it’s handier to give each page an ID attribute in its BODY tag equal to its alias field, i.e. the template includes <body id="[*alias*]">. Then the snippet call becomes [!mySnippet?page=`[*alias*]`!], and you have more meaningful values to switch on, with the added bonus of using the ID to include page-specific CSS in a general stylesheet for the site.

      This is just one way - as ever in MODx, you could do it in a number of ways, it’s more a question of choosing the one that suits you than finding the "right" way. I’m sure a PHx expert will be along soon to promote its virtues!
      wink KP

      Edit: Just realized your tags indicate you’re using Revolution - my notes are for Evo; same idea, different API, I expect, and yet more variations possible on how to achieve it.
      embarrassed
        • 1778
        • 659 Posts
        Hello

        If you’re using Revo you have the IF snippet to do that easily.

          • 38162
          • 97 Posts
          One option would also be using Template Variables. Depending on the case, it might even be the best solution.
            • 19369
            • 1,098 Posts
            What about this?

            <?php
            $docid = $modx->documentIdentifier;
            switch($docid) {

            case 1:
            return "{{home}}";
            break;

            case 2:
            return "{{services}}";
            break;

            default:
            return "{{default}}";
            break;
            }
            ?>
              • 6564
              • 8 Posts
              Wow guys thanks for the replies wink
              ... Which one would one would be least complicated to my template