We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    On my pages I want to display the name of the department. This is held in a TV in the top parent resource of the department.

    I have a snippet called departmentTitle that finds the top department parent, looks for the TV, and returns either the TV or (if TV is empty) the page title:
    $deptID = $modx->runSnippet('wwwGetDepartment', array('pid'=>$pid)); 
    $dept   = $modx->getObject('modResource', $deptID);
    
    $tv = $modx->getObject('modTemplateVar', array('name'=>'departmentTitle'));
    $title = ($tv->getValue($deptID)) ? $tv->getValue($deptID) : $dept->get('pagetitle');


    So far so good. But, rather than just calling this directly in my template like

    <h1>[[departmentTitle]]</h1>

    I decided to get cute and call it like this:

    <h1>[[*departmentTitle:default=`[[wwwDepartmentTitle]]`]]</h1>

    My thought was that this would speed things up for requests to the actual top department resource, because it would just access the TV directly and not have to run the snippet. And for other resources, I figured getting to the snippet via the :default filter of a TV would not make a noticeable difference in load time.

    I’m not positive, because I haven’t figured out a completely accurate way to measure and compare... but I think it is making a noticeable difference in load time. Calling the snippet directly seems to result in a much faster load time than the tv:default method.

    OK, as I’m writing this I think I see a problem. I guess I was considering the output filter to work like an else clause - to only be invoked when the TV was empty. But the parser doesn’t look at it that way right? It just sees a snippet call waiting to be parsed and parses it... so it gets parsed every time.

    So that’s a problem with my logic... but I don’t think it really explains the perceived difference in load time. Should I expect the TV:default syntax add a lot of processing time?

    Or is this all in my head?
      • 3749
      • 24,544 Posts
      Conditional modifiers are always slow and I think you’re right about the snippet tag being parsed whether needed or not.

      I think you’d gain a lot of speed by just using a snippet tag and putting all the logic in the snippet.

      There are also a number of ways to optimize the snippet code.
        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