We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36631
    • 10 Posts
    Hello,
    i am new to Modx Revolution and i try to archieve a very simple task:

    I do have a template-variable [[*newstext]] with inputtype: rich text field and outputtype: text.
    Some pages on my site may have news, others may not have.

    So i want a snippet to check wether this template-variable is empty or not and if if is not empty i want to output a message.


    Here is what i tried in my snippet:

    1. Submit the template-variable to a php variable
    $newstext = '[[*newstext]]'; 


    2. Check if the variable is empty and if it is NOT empty echo it.
    if ($newstext){
      echo"<div id='news3'>[[*newstext]]</div>
    }


    My problem is that even when [[*newstext]] is empty (no text in the rich text field)
    <div id='news3'></div>
    shows up in the source-code of the page.

    The variable$newstext contains no visible content - i tried that with echo $newstext; and it didn't show anything

    I also tried:
     if ($newstext != "")
    but still the div's were shown.

    Here is the complete code:
    <?php
    $newstext = '[[*newstext]]';
    if ($newstext != ''){
      echo"<div id='news3'>[[*newstext]]</div>";
    }
    ?>


    I'd be so glad if you could help me with that. I spent about 2 hours now on Google and the forums but couldn'd find what is wrong with my constuction.

    Thanks in advance,
    Phil [ed. note: philhippos last edited this post 15 years, 2 months ago.]
      • 4172
      • 5,888 Posts
      you can use an output-filter like that:

      [[*newstext:notempty=`<div id="news3">[[*newstext]]</div>`]]


      or:

      [[*newstext:isnot=``:then`<div id="news3">[[*newstext]]</div>`:else=`TV newstext is empty`]]


      or the If-snippet like that:

      [[!If?
         &subject=`[[*newstext]]`
         &operator=`notempty`
         &then=`<div id="news3">[[*newstext]]</div>`
      ]]
      






        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 36637
        • 2 Posts
        Hello Bruno,

        thanks you for your help. That was just what i needed!

        Just for curiosity, and because i might run over a similar problem again: Would there have been a way to solve this by using a snipped as i tried?

        Thank you!

        P.S.
        I needed to open up another account, since when i try to login with philhippos i keep getting the message: "discuss.account_deactivated". No idea why my account has been deactivated. Was there something wrong with my question?
          • 4172
          • 5,888 Posts
          you cannot use tags like [[*.......]] in a snippet, but you can send the parsed value to your snippet this way:

          [[!mysnippet? &value=`[[*newstext]]`]]


          and use it in your snippet like that:

          <?php
          if (!empty($value)){
            return '<div id="news3">'.$value.'</div>';
          }


          or

          <?php
          
          $value=$modx->getOption('value',$scriptProperties,false);
          
          if ($value)){
            return '<div id="news3">'.$value.'</div>';
          }


          the modx-api has also several methods to get a value of TVs in custom-snippets.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!