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

    I got this little problem:
    in my pages i show a chunk which uses a template-variable (see here).

    but i want this chunk only to be shown when a template variable for this entry exists. it the TV doesn’t exist i don’t want the chunk to be shown. is this possible?

    thanks for any suggestion in advance!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can use a snippet or PHx.

      The snippet:
      <?php
      $output = '';
      $TV = $modx->getTemplateVar('file_name','value', $modx->documentIdentifier);
      if(!empty($TV)) {
          $output = $modx->getChunk('chunk_name');
      }
      return $output;
      ?>
      

      Chunk using PHx
      [*file_name:ifempty=``:else=`<a href="[*file_name*]">Get File</a>`*]
      

      although I’m sure somebody better with PHx will be able to give a better usage for it.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3688
        • 43 Posts
        sottwell, thank you for your suggestions!

        i have the following problems:
        with phx i am not familiar, but the code you posted is for evolution, right? i don’t know how i would use it with revolution (and i don’t find a documentation?!); perhaps anybody has any idea how to translate it into "revolution"-phx-language? it seems to me this would be the most simple solution.

        the snippet-solution you posted doesn’t work for me. wouldn’t it be possible to work with the snippet WITHOUT using the chunk, means that i use the getTemplateVar to retrieve the value of the variable?
        it seems to me slightly complex calling the snippet which calls the chunk which calls the variable. i tried things like the following, but i do not fully understand the concept of the getTemplateVar-API:

        <?php
        $output = '';
        $tv = $modx->getTemplateVar('pdf_praesentation');
        // maybe have to enter something like $tv_content = $tv['value']; but this doesn't work
        $output = "<a href='xy/" . $tv . "'>download</a>";
        
        return $output;
        ?>

        so perhaps somebody could help me out with this ... thanks in advance!
          • 33337
          • 3,975 Posts
            Zaigham R - MODX Professional | Skype | Email | Twitter

            Digging the interwebs for #MODX gems and bringing it to you. modx.link
            • 3688
            • 43 Posts
            thank you zi. i didn’t know that PHx doesn’t exist in Revolution anymore.

            for me i would not understand how to use the output-filters to make an if-else-construction. i see there is an modifier "ifempty", but i would not understand how to use it in my case. perhaps you could give an example for the others reading this thread.

            i now generated a solution with getTemplateVarOutput, which doesn’t need a chunk anymore, based on sottwell’s suggestion:
            <?php
            $output = '';
            $tv_value = $modx->getTemplateVarOutput(array('name_of_templatevariable'));
            if(!empty($tv_value['name_of_templatevariable']))
            {
            $output = "<iframe src = 'xy/" . $tv_value['pdf_praesentation'] . "#pagemode=none&toolbar=0' width='550px' height='375px'  frameborder='0'></iframe>";
            }
            
            return $output;
            ?>


            thanks again for all your fast and useful help!

            (my case closed)
              • 33337
              • 3,975 Posts
              For if-else stuff, there is IF snippet, please see: http://svn.modxcms.com/docs/display/ADDON/If
                Zaigham R - MODX Professional | Skype | Email | Twitter

                Digging the interwebs for #MODX gems and bringing it to you. modx.link
                • 12110
                • 122 Posts
                Hello,

                I found that the phx example code from above

                [*file_name:ifempty=``:else=`<a href="[*file_name*]">Get File</a>`*]


                does not work, my TV is an image with display type image.

                This is my code:

                [*motivGross1:ifempty=``:else=`<li>[*motivGross1*]</li>`*]
                [*motivGross2:ifempty=``:else=`<li>[*motivGross2*]</li>`*]
                [*motivGross3:ifempty=``:else=`<li>[*motivGross3*]</li>`*]


                It shows the else part even if motivGrossX TV isn’t filled. Any suggestions?

                I’m using Evo 1.0.4 and Phx 2.1.4.

                P.S.: It seems to work this way:
                [*motivGross1:is=``:else=`<li>[*motivGross1*]</li>`*]
                [*motivGross2:is=``:else=`<li>[*motivGross2*]</li>`*]
                [*motivGross3:is=``:else=`<li>[*motivGross3*]</li>`*]


                Not sure why ifempty doesn’t work.