We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40906
    • 23 Posts
    Hi there,

    I've got this thing set up but i'm not getting the response I would like to have. Can someone please tell me what I'm doing wrong?

    [[+tv.referenceLogo:empty:then=`
                     [[!firstLetter? &input=`[[+tv.referenceName]]`]]
                `:else=`
                     <img class="ref-img" src="[[+tv.referenceLogo]]" width="224" height="126" alt="[[+pagetitle]] - [[pdoField? &id=`[[UltimateParent? &id=`[[*id]]`]]` &field=`pagetitle`]] - [[++site_name]]"/>
                `]]


    Needed outcome; if the TV "referenceLogo" is empty, output the first letters of each word in the "referenceName" TV.

    firstLetter snippet:
    <?php
    $string = "$input";
    
    $expr = '/(?<=\s|^)[a-z]/i';
    preg_match_all($expr, $string, $matches);
    
    $result = implode('', $matches[0]);
    
    $result = strtoupper($result);
    
    echo $result;


    The snippet works if I take it out of the 'then' statement. So that shouldn't be the problem?

    What i'm getting out of the code right now is the else statement. So it appears that the empty/then statement/combination doesn't work?

    If someone could advice that would be great. Thanks in advance.

    Coen
      • 3749
      • 24,544 Posts
      Putting chained output modifiers inside a Tpl chunk (as I assume these are) is always a crap shoot due to the complex caching possibilities (and they are quite slow compared to a dedicated snippet). Also, calling the firstLetter snippet uncached delays its processing, so it hasn't run yet at the time the output modifier is processed.

      One possible solution is to move everything into a dedicated snippet:

      [[!referenceCheck? &input=`[[+tv.referenceName]]` &logo=`[[+tv.referenceLogo]]` ]]


      Pseudocode for referenceCheck snippet

      // get values of input and logo with $modx->getOption
      
      function firstLetter($input) {
          $expr = '/(?<=\s|^)[a-z]/i';
          preg_match_all($expr, $input, $matches);
          $result = implode('', $matches[0]);
          $result = strtoupper($result);
          return $result;
      }
      
      if (empty($input)) {
        $output = firstLetter($logo);
      } else {
        $output = '<img class="ref-img" src="[[+tv.referenceLogo]]" width="224" height="126" alt="[[+pagetitle]] - [[pdoField? &id=`[[UltimateParent? &id=`[[*id]]`]]` &field=`pagetitle`]] - [[++site_name]]"/>';
      }
      
      return $output;
        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