Snippet doesn't work unless i include it in all pages

I have a brand new site. I want to include a snippet on page 2 but not on page 1. If I don’t include the snippet on page 1, it doesn’t appear on page 2. If I add the snippet to both pages, it will show up on page 1 and page 2. What am I missing?

Hi @casinnova, there are many ways how to split it, probably the easiest way is to use next call on both pages:
[[*id:eq=`2`:then=`[[yourSnippet]]`:else=``]] (or uncached [[!yourSnippet]] if this makes sense)
Alternatively you may use different templates for pages, or add page ID check inside snippet directly

+1 for different templates or use a checkbox - TV or something to switch the snippet on/off
Whats the best approach depends on your individual use-case.

pages could also be build with different content - blocks by using ContentBlocks, MIGX or switch chunks on/off with TVs.

Important to note when using that approach: MODX processes tags inside out. Meaning [[yourSnippet]] is always executed before it runs the conditional check. That can in some cases be harmless, but in others it can cause unexpected behavior (eg if the snippet redirects) or degraded performance (if the goal was to skip a slow snippet on certain pages).

Turn it into a “mosquito tag” to avoid that:

[[[[*id:eq=`2`:then=`yourSnippet`:else=`- do nothing`]]]]

I am very grateful for everyone’s responses, but WHY does this happen? I don’t understand the system well enough apparently, but it seems really illogical. Is there anywhere in the documentation that explains it? I tried to do my due diligence before posting here, but obviously came up empty.

You probably have to be more specific about what exactly you are doing.

  • Where did you place the snippet call? In the content of the page? In the template?
  • Do you call the snippet cached ([[mySnippet]]) or uncached ([[!mySnippet]])?
  • What exactly is your snippet doing? Do you see the same behaviour with a very simple snippet that just returns a string?
  • Is the checkbox “Empty Cache” checked on both your pages?

Maybe, now I get your question.
You are trying to add a snippet into a page and you want it to be added to all other pages at the same time?

You would add it to your page - template. Not into the page’s content, then.