We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30491
    • 44 Posts
    Hi according to the documentation, chaining output modifiers is perfectly acceptable. However, in practise I'm having difficulty with it.

    Take fore example, three conditional statements, in the form of chained output modifiers:


    [[+test:is=``:then=`empty`:is=`1`:then=`one`:is=`2`:then=`two`]]

    I get blank result, leading me to believe that the chaining is not actually working. Am I doing something wrong here?

    Thanks, this is doing my head in.
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      if/then statements are tricky because one is dependent on the other. Chaining is more simple when it's something like "strtoupper:ucfirst" or something.

      I know you weren't asking for a lecture, but I would advise against using if/then logic in your output filters. It's a bad habit (logic in your view layer is one of the cardinal sins of good development). At best, it's confusing, but at worst, it becomes hopelessly difficult to debug, and unlike PHP there are no helpful errors or warnings when it breaks... it just stops working. Importantly, that stuff doesn't cache the same way (or at all) because the view layer always has to do the calculation instead of doing it somewhere upstream. If there's some other way to do this (e.g. via a Snippet or a plugin or a general reorganization), I would recommend looking at the other options seriously because they nearly always result in better performance, simpler maintenance, and better scalability.
        • 3749
        • 24,544 Posts
        I agree with Everett. Aside from the maintainability and debugging issues, conditional modifiers are slow to begin with and chaining them will make them much slower. Worse yet, much of the time you end up parsing things and executing code for things that are not used in the output.

        That said, what you're trying to do might work if you include an 'else' clause for each 'then'. Many of the conditional output modifiers break without an 'else' specified.
          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
          • 30491
          • 44 Posts
          Hi thanks for the advice guys,

          My only contention is that doesn't caching take care of that speed difference?

          In my particular usage of these conditional output modifiers--I'm using them as a means of making dynamic Chunks, where I can pass particular values that result in the chunk being displayed differently. For example:

          Chunk (named titleChunk)
          [[+title
          :if=`yes`:then=`<h1 class="title">[[*pagetitle]]</h1>`
          :if=`no`:then=`No Title`
          :if=`withCrumbs`:then=`<h1 class="title">Category >> Category 1 >> [[*pagetitle]]</h1>`
          ]]
          


          So I would call this chunk in some template, and customize the output (instead of having to create a variety of chunks just for pagetitle variation)

          So for example:

          Template (named homePage)

          
          <content>
          [[$titleChunk? &title=`no`]]
          </content>
          
          


          and

          Template2 (named defaultPage)

          
          <content>
          [[$titleChunk? &title=`withCrumbs`]]
          </content>
          
          



          From my understanding, because of the way Modx caches, it would cache those templates right? and therefore, on each page-render, it won't re-evaulate those Conditional out-put modifiers, it is simply using a cached version when ever each template is invoked?

          Or am I totally off the mark?

          Thanks alot

          @BobRay

          So you suggest, that I should display it like this,to get it work correctly? (adding the else bit):

          [[+title
          :if=`yes`:then=`<h1 class="title">[[*pagetitle]]</h1>`:else=``
          :if=`no`:then=`No Title`:else=``
          :if=`withCrumbs`:then=`<h1 class="title">Category >> Category 1 >> [[*pagetitle]]</h1>`:else=``
          ]]
          
            • 38705
            • 101 Posts
            Quote from: tourshi at Jun 13, 2013, 12:38 PM

            [[+title
            :if=`yes`:then=`<h1 class="title">[[*pagetitle]]</h1>`
            :if=`no`:then=`No Title`
            :if=`withCrumbs`:then=`<h1 class="title">Category >> Category 1 >> [[*pagetitle]]</h1>`
            ]]
            


            Why don't you just use?
            [[+title:is=`yes`:then=`<h1 class="title">[[*pagetitle]]</h1>`]]
            [[+title:is=`no`:then=`No Title`]]
            [[+title:is=`withCrumbs`:then=`<h1 class="title">Category >> Category 1 >> [[*pagetitle]]</h1>`]]
            

            Since you only have three variants of [[ + title ]].... the above code should always work without any heavy mathematics...
            as a complementary addition you might add :else=`` to all statements to make it failproof....
              Addict since 2012....
              • 30491
              • 44 Posts
              Hi mintnl,

              yeah I have been using that in the meantime, but just wanted to see if I can chain it in one go instead since it appears to support that..as for the heavy mathematics, Im still trying to grasp this, as to my knowledge Modx will cache the result of the mathematics (by default, of course unless the chunk is called uncached) so then whats the harm in having it there. I hope the more experienced users here can offer their insight into this.

              Thanks smiley
                • 4172
                • 5,888 Posts
                stuff like that is doable without any conditionals for example like that:

                three chunks with

                chunk title_yes:
                <h1 class="title">[[*pagetitle]]</h1>


                chunk title_no:
                No Title


                chunk title_witchCrumbs:
                <h1 class="title">Category >> Category 1 >> [[*pagetitle]]</h1>


                used this way

                <content>
                [[$titleChunk? &title=`withCrumbs`]]
                </content>
                


                in your titleChunk without conditionals just:
                [[$title_[[+title]]]]



                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 30491
                  • 44 Posts
                  hummm, well that's very clever Bruno17 thanks for that! what do you think about using conditionals in terms of chaching and performance, doesn't modx just cache it all and hence it only does the calculating bit once (so long as the cache is intact).

                  many thanks
                    • 4172
                    • 5,888 Posts
                    once they are processed they are cached, yes.
                    But they need to be processed the first time and people wonder why there sides are going be slow.
                    So better not to use them, if there are better ways to get the same result.
                    In some situations, if you have many nested conditionals with much, probably growing content in between conditional output-filters this can break the whole output at some point.
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 9207 ☆ A M B ☆
                      • 2,475 Posts
                      I dunno -- MODX caching (and any thorough caching mechanism) is complicated and layered. I think the view gets cached, but the output filters still execute each time the view is processed, so unless I'm mistaken, having lots of output filters is bad for performance. In my experience, relying on cache for performance is a bad idea.