We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • hello all, I am currently trying to optimize an existing website as the loading time of some pages is not ideal. I stumbled upon something I want to double check with someone who is more experienced on these matters.

    I usually code my pages with a lot of flexibility to simplify what content uploaders have to do and avoid common problems. One common scenario is with a TV which holds an image that represents a given document.

    Consider this:

    [[*ft_img:notempty=`<div class="ft-img"><img src="[[*ft_img:phpthumbof=`&w=300&h=175&zc=1`]]" alt="[[*longtitle]]" width="300" height="175"></div>`]]


    I do this quite a lot through my templates in different situations and with different content types and template variables.

    If I understand how modx works correctly then the order in which the above gets executed is that phpthumbof will process its image, then move up to the conditional. Regardless of the conditional, the phpthumb code gets executed which is not ideal performance wise. Am I correct assuming this?

    If so there is a whole lot I can optimize by recoding some things which depend on other conditionals but are getting executed regardless.

    Another common scenario for me are context settings. I usually make custom context settings and work my website structure based on the values of these settings. This allows me to use a single template for multiple languages effectively, but again at the cost of performance when using this approach.


    I'm wondering what the right way of achieving this would be, how are people working around this subject when coding their templates?

    Thanks.
    • Currently the code sample you provided indicates that it is using cached results, thus I'd assume any slow performance would be related to another area. Are you running wayfinder or any other snippets that have processor dependencies? If you are you might review those as well and see if any optimization can be done there.

      Also you might check out this http://bit.ly/mWpXz9 by MarkH regarding cache settings.

      Cheers
        Evo Revo // Ubuntu, CentOS, Win // Apache 2x, Lighttp (Lighty)
        Visit CharlesMx.com for latest news and status updates.
      • Great read! Thank you for sharing. it clarified many things for me.

        I did try to carry along the stipulations of the article for the most part but I did learn some new stuff on the way.

        What I really want to have answered tho by someone experienced in modx is if even when caching results, calling or executing nested comparison / conditionals within resources affects the overall performance enough to consider an alternative way of achieving same results?

        That was a long question and probably does not make a lot of sense, so let me clarify:

        Lets say I do this:

        [[*introtext:empty=`[[*content:strip_tags:strip]]`:limit=`200`]]
        


        How much of a performance hit is it having nested calls? Do they all get executed regardless of the conditionals stated? or do they only get executed if the conditional is true?

        In the above example, would [[*content]] be stripped its tags regardless of [[*introtext]] being empty or not, or would it only execute if introtext is empty?

        The reasons for my question is because I rely a lot on these types of conditionals and I want to make sure that by doing so I am not forcing the server more than I should.

        Cheers,
        Jose R. Lopez
        • I got to this page by checking my analytics referal logs (I like analyzing numbers, sorry tongue), and a follow up I did on the first caching strategy post deals with nested and conditionals like the one you are posting. You can find it here: http://www.markhamstra.com/modx-blog/2011/11/nested-caching-in-modx-revolution/

          In short, it will always be parsed inside out, so in your case [[*content:strip_tags:strip]] will be executed before it checks if [[*introtext]] is empty or not. As said in the article, you could use something like this:
          [[[[*introtext:notempty=`*introtext`:empty=`*content:strip_tags:strip`]]:limit=`200`]]

          which will:
          - parse [[*introtext]]
          - check if it's empty, and if it is return "*content:strip_tags:strip"
          - if introtext was not empty, the resulting tag will be:
          [[*introtext:limit=`200`]]

          if it was empty it will be
          [[*content:strip_tags:strip:limit=`200`]]

          and both just get parsed normally.

          With resource values I wouldn't worry a bout performance too much - they're fetched anyway so they just need to be inserted.

          When dealing with snippets or output filters like phpthumbof which can have a serious impact, thinking twice is a good idea and a trick like the above one is a good alternative.
            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.
            • 3749
            • 24,544 Posts
            Mark's example will definitely be faster, but conditional modifiers are almost always slow for a variety of reasons. If performance is an issue you really should try to avoid them.

            In the case of your second example, a fairly simple replacement would be this:

            [[MyIntrotext]]


            /* MyIntrotext Snippet */.
            $it = $modx->resource->get('introtext');
            return (empty($it))? '[[*content:strip_tags:strip]]`:limit=`200`]]'  : $it;


            That way the parsing of the conditional, the extra retrieving of the content and then performing three separate processes on it won't occur unless introtext is empty.


            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODx info for everyone: http://bobsguides.com/MODx.html
              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
            • If you're going the snippet route anyway, there's little reason to use output modifiers for the content still.

              Just do something like this..

              $intro = $modx->resource->get('introtext');
              if (empty($intro)) $intro = trim(strip_tags($modx->resource->getContent()));
              return substr($intro,0,200);
              


              From my phone.. No typo-free guarantee.
                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.
                • 3749
                • 24,544 Posts
                Good point. I was trying to keep the snippet simple.

                TBH, unless a site had other users that I couldn't trust, I would skip the whole thing and just make sure the introtext field was never empty. wink


                ---------------------------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                MODx info for everyone: http://bobsguides.com/MODx.html
                  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