We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18366
    • 14 Posts
    I want to develop a snippet that implements a very simple piece of logic. It takes a string and outputs the same string in all cases, except if the string is empty in which case it outputs another string that’s hardcoded into the php source.

    Here was my first attempt:

    <?php
    return $inputString? $inputString: ’differentstring’;
    ?>

    I also tried using an if else construct and the empty() function, but that didn’t work.

    Here’s how I’m invoking my snippet:

    [!MySnippet? &inputString=`[+tags+]`!]

    The [+tags+] placeholder is a result of me following Mark Kaplan’s video tutorial on how to do ditto tagged sorting. That placeholder definitely contains something and I can put it into my page content and it’s fine. I’ve also checked that the value it contains is being passed into the snippet code.

    I did some further checks and found that when I did strlen() on the value being passed in, the result is always 8 regardless of the length of the string contained by the placeholder. The body of the snippet code appears to be evaluating $inputString as the literal token of the placeholder. My $inputString therefore always evaluates to true even if the placeholder has an empty string.

    I tried using the api call to get a placeholder, but I couldn’t get that to work:

    $modx->getPlaceholder(’tags’);

    I’d be grateful for some guidance on how to implement such a snippet. Thanks.

      • 4041
      • 788 Posts
      Give this a try and see if it works:

      <?php
      /*
         usage:
         [!MySnippet? &inputString=`tags`!]
      */
      
      $output ='';
      $tv_name = isset($inputString) ? $inputString : '';
      
      if($tv_name !=''){
          $tv_output= $modx->getTemplateVarOutput($tv_name);
          $output = $tv_output[$tv_name];
      }
      
      return $output;
      ?>
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 18366
        • 14 Posts
        I gave that a go, but no joy.

        I have written some other snippets that look at TVs with similar code to that, but this time I’m trying to do something much simpler, but can’t do it.

        If I have the snippet like this:

        <?php
        return $inputString;
        ?>

        It will return a string with characters, or empty string in accordance with how it’s been invoked. The moment I do:

        <?php
        if (empty($inputString))
        return ’differentstring’;
        else
        return $inputString;
        ?>

        the empty() call always evaluates to false and strlen($inputString) is always 8, even if [+tags+] is evaluating to an empty string in the resource content.
          • 18366
          • 14 Posts
          I also tried creating a chunk with just [+tags+] so that the snippet doesn’t even have a take a parameter anymore, then inside the snippet I did:

          $chunkOutput = $modx->getChunk("...");

          This works in that the content contained by [+tags+] is accessible, but I can’t do any operations on it like test if it’s the null string or get it’s length.

          This has to be something to do with how placeholders are evaluated.