We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33238
    • 388 Posts
    Hello.

    I have 2 placeholders: [[+placeholder1]] and [[+placeholder2]]

    I need create a conditional that check the placeholders, if at least one of them is empty display a chunk with code, if both are not empty then display another chunk:

    if [[+placeholder1]] or [[+placeholder2]] are empty then [[$chunk_empty]] else [[$chunk_full]]

    any help?
    thank you!

    This question has been answered by Ysanmiguel. See the first response.

      --
      ysanmiguel.com
      • 4172
      • 5,888 Posts
      try this:

      $delimiter = $modx->getOption('delimiter',$scriptProperties,',');
      $values = explode($delimiter,$modx->getOption('values',$scriptProperties,''));
      $emptyChunk = $modx->getOption('emptyChunk',$scriptProperties,'');
      $fullChunk = $modx->getOption('fullChunk',$scriptProperties,'');
      
      $tpl = $fullChunk;
      if (is_array($values)){
          foreach ($values as $value){
              if (empty($value)){
                  $tpl = $emptyChunk;
                  break;
              }
          }
      }
      
      return $modx->getChunk($tpl,$scriptProperties);
      


      [[isOneEmpty? &values=`[[+placeholder1]],[[+placeholder2]]` &emptyChunk=`emptyChunk` &fullChunk=`fullChunk`]]


      if you have commas in your placeholders, use another delimter
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 33238
        • 388 Posts
        Thank you for your answer Bruno.

        Because there are comas inside the placeholders I modified the delimiter:

        $delimiter = $modx->getOption('delimiter',$scriptProperties,'~');


        then I tried to use the snippet

        [[isOneEmpty? &values=`[[+placeholder1]]~[[+placeholder2]]` &emptyChunk=`emptyChunk` &fullChunk=`fullChunk`]]
        


        But it's always displaying the &FullChunk content no matter what.

        Then I checked and confirmed the placeholders were really empty and at least one of them is empty, so the rule is true, but I don't really understand why it's still showing the &FullChunk all the time.

        Both placeholders came from MigX tables:

        [[getImageList?
            &tvname=`migxTv`
            &tpl=`tpl.here`
            &toPlaceholder=`placeholder1`
        ]]


        So, not sure if there is something that I can't see but the snippet is processing as content?

        Thank you Bruno.
          --
          ysanmiguel.com
          • 3749
          • 24,544 Posts
          Try adding &delimiter=`~` to the snippet tag.

          Using ~ as a delimiter might be confusing the parser, since it's a MODX token for link tags. Try &delimiter=`xxx` and put xxx between the placeholders (with no spaces before of after xxx).
            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
            • 33238
            • 388 Posts
            Quote from: BobRay at Sep 26, 2017, 05:36 AM
            Try adding &delimiter=`~` to the snippet tag.

            Using ~ as a delimiter might be confusing the parser, since it's a MODX token for link tags. Try &delimiter=`xxx` and put xxx between the placeholders (with no spaces before of after xxx).

            Hello Bob.

            OK, I changed the line in the snippet:

            $delimiter = $modx->getOption('delimiter',$scriptProperties,'xxx');


            then I called this way:

            [[isOneEmpty? &values=`[[+placeholder1]]xxx[[+placeholder2]]` &emptyChunk=`emptyChunk` &fullChunk=`fullChunk` &delimiter=`xxx`]]


            but nothing, it still displaying full chunk all the time.

            As I said before, the placeholders come from MigX tables, one has content and the other hasn't, one placeholder is displaying the content, and the other one is showing nothing (front and backend) so I think it's really empty.

            to be sure I called like this:
            [[getImageList?
                &tvname=`migxTv`
                &tpl=`tpl.here`
                &toPlaceholder=`placeholder1`
            ]]
            [[+placeholder1]]


            and like this:

            [[getImageList?
                &tvname=`migxTv`
            ]]


            This call is inside another MigX [[getImageList? call (nested), do you think that might change something?

            thank you very much for your help, cheers!
              --
              ysanmiguel.com
              • 3749
              • 24,544 Posts
              Yes, the nesting could be an issue if the isOneEmpty snippet is getting the placeholder tags themselves rather than the processed tags' values.

              Try this, then look at the error log:

              $delimiter = $modx->getOption('delimiter',$scriptProperties,',');
              $values = explode($delimiter,$modx->getOption('values',$scriptProperties,''));
              $emptyChunk = $modx->getOption('emptyChunk',$scriptProperties,'');
              $fullChunk = $modx->getOption('fullChunk',$scriptProperties,'');
               
              $tpl = $fullChunk;
              if (is_array($values)){
                  foreach ($values as $value){
                      $modx->log(modX::LOG_LEVEL_ERROR, 'XXX' . $value . 'XXX'); // Added
                      if (empty($value)){
                          $tpl = $emptyChunk;
                          break;
                      }
                  }
              }
               
              return $modx->getChunk($tpl,$scriptProperties);
                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
                • 33238
                • 388 Posts
                Quote from: BobRay at Sep 26, 2017, 06:35 PM
                Yes, the nesting could be an issue if the isOneEmpty snippet is getting the placeholder tags themselves rather than the processed tags' values.

                Try this, then look at the error log:

                $delimiter = $modx->getOption('delimiter',$scriptProperties,',');
                $values = explode($delimiter,$modx->getOption('values',$scriptProperties,''));
                $emptyChunk = $modx->getOption('emptyChunk',$scriptProperties,'');
                $fullChunk = $modx->getOption('fullChunk',$scriptProperties,'');
                 
                $tpl = $fullChunk;
                if (is_array($values)){
                    foreach ($values as $value){
                        $modx->log(modX::LOG_LEVEL_ERROR, 'XXX' . $value . 'XXX'); // Added
                        if (empty($value)){
                            $tpl = $emptyChunk;
                            break;
                        }
                    }
                }
                 
                return $modx->getChunk($tpl,$scriptProperties);


                Hello Bob.

                here is the error log:

                [2017-10-02 12:11:58] (ERROR @ /home/user/modx/core/cache/includes/elements/modsnippet/115.include.cache.php : 10) XXX
                <div class="small-12">
                    <div class="row">
                        <HTML code....>
                    </div>
                </div>
                XXX
                [2017-10-02 12:11:58] (ERROR @ /home/montours/modx/core/cache/includes/elements/modsnippet/115.include.cache.php : 10) XXX[[+placeholder2]]XXX


                Do you think it's not showing as empty because the XXX[[+placeholder2]]XXX?

                Thank you!
                  --
                  ysanmiguel.com
                  • 3749
                  • 24,544 Posts
                  Yes, definitely.

                  You might try calling the placeholder uncached.

                  [[!+placeholder]]


                  That should delay the processing of the placeholder, hopefully until after the time it's set.
                    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
                    • 33238
                    • 388 Posts
                    Quote from: BobRay at Oct 02, 2017, 09:55 PM
                    Yes, definitely.

                    You might try calling the placeholder uncached.

                    [[!+placeholder]]


                    That should delay the processing of the placeholder, hopefully until after the time it's set.

                    Well, now it looks like this:

                    [[isOneEmpty? &values=`[[!+placeholder1]]xxx[[!+placeholder2]]` &emptyChunk=`emptyChunk` &fullChunk=`fullChunk` &delimiter=`xxx`]]
                    

                    [2017-10-03 08:05:21] (ERROR @ /home/user/modx/core/cache/includes/elements/modsnippet/115.include.cache.php : 10) XXX[[!+placeholder1]]XXX
                    [2017-10-03 08:05:21] (ERROR @ /home/user/modx/core/cache/includes/elements/modsnippet/115.include.cache.php : 10) XXX[[!+placeholder2]]XXX
                    


                    If I uncached the snippet, didn't work at all.
                    [[!isOneEmpty? &values=`[[+placeholder1]]xxx[[+placeholder2]]` &emptyChunk=`emptyChunk` &fullChunk=`fullChunk` &delimiter=`xxx`]]


                    Any other idea Bob?
                    Thank you very much for your help!
                      --
                      ysanmiguel.com
                      • 3749
                      • 24,544 Posts
                      Sorry that didn't work. Is the getImageList tag above the tag with the placeholders? It needs to come first.

                        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