We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7869
    • 118 Posts
    Is it possible to call a snippet with a conditional in my site template?

    I want to use the PrevJumpNext snippet, however I only want that snippet called if the document’s parent id matches certain number. For example I don’t want my site index page nor its immediate children to display links generated by PrevJumpNext, but if the page is a child of a child (ie: a grandchild of the site index if that makes sense), then I want PrevJumpNext to be called to display the links.

    I know I can do this by creating a new template and assigning it only to those pages that I want to use PrevJumpNext on, but that doesn’t seem like practical coding.

    Any advise?
      -Dorian
      • 22851
      • 805 Posts
      I’d write a snippet to do it, called ConditionalPrevJumpNext maybe. The snippet would use the modx api to analyse the id of the current page and those of it’s parent’s pages and decide whether you wanted to call the snippet or not. Then, if you do want to call and get the output of the PrevJumpNext snippet, you can do that with the $modx->runSnippet(’PrevJumpNext’, ...) function, else don’t return anything.

      If you have never written a snippet before, then looking at the PrevJumpNext snippet code is probably a good starting point.

      Paul
        YAMS: Yet Another Multilingual Solution for MODx
        YAMS Forums | Latest: YAMS 1.1.9 | YAMS Documentation
        Please consider donating if you appreciate the time and effort spent developing and supporting YAMS.
        • 6726
        • 7,075 Posts
        My advice would rather be to use the PHx plugin, which has a powerful syntax to that effect (and many more...).

        The wiki has good PHx documentation and can give you a rough idea of what is possible here (though you really have to try it out to realize its power !).

        The benefits of PHx are limitless, so much so that I can’t live without it ! It extends the parser and once you get into its syntax, you can really do a lot without touching PHP, which (for me) is good smiley

        In your case it would be as simple as :

        [tt][+phx:if=`[*id*]`:is=`2`:then=`[[....snippet call....]`:else=``+][/tt]

        You can do nice stuff using the UltimateParent snippet in the if statement, and that’s just one example...
        The downside of conditionnals is it can hit your server a bit when you add to many of them.
        The upside is that you can reduce the number of template which is great for maintenance smiley

          .: COO - Commerce Guys - Community Driven Innovation :.


          MODx est l'outil id