We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    This is probably a stupid question, but I’m working on a Snippet that’s taking Chunk names as input (finally!)

    If my simplified Snippet code has something like this:
    if ( !isset($my_chunk) ) {
    	$my_chunk = 'name_of_my_chunk';  // Default Chunk	
    }
    $output.=$modx->parseChunk( $my_chunk, $row, '[+', '+]');


    That’s great, and I can set a default Chunk name, but in order for my Snippet to be more portable, I’d rather not rely on a Chunk... I’d rather just have the default string hard-coded...

    Something like this:

    This is probably a stupid question, but I’m working on a Snippet that’s taking Chunk names as input (finally!)

    If my simplified Snippet code has something like this:
    if ( !isset($my_chunk) ) {
    	$my_chunk = '<table>[+content+]</table>';  // Default Chunk	
    }
    $output.=$modx->parseChunk( $my_chunk, $row, '[+', '+]');


    That way I can just install the Snippet and not have to worry about installing and naming Chunks.

    I’ll probably figure this out in another 30 minutes, but I thought it wouldn’t hurt to ask.

    Thanks!

      • 3749
      • 24,544 Posts
      That’s a common thing to do if the content is fairly short.

      You’ll often see it in MODx snippet code in this form:

      $myTpl = isset($my_chunk)? $my_chunk: '<table>[+content+]</table>';
      
      $output.=$modx->parseChunk( $myTpl, $row, '[+', '+]');


        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
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: BobRay at Dec 19, 2008, 12:19 AM

        That’s a common thing to do if the content is fairly short.
        Becoming even more common in Revo where you can pass content through parameters with no problems, unlike the current parser which does not allow complex HTML due to the limitation against ? & and =.
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Hmm... now that I’m sitting down to put in some default values, I can’t seem to make this work.

          Here’s what I have in a nutshell:
          $myTpl = isset($item_chunk)? $item_chunk: 'Page Title: [+pagetitle+]<br/>';
          
          $output.=$modx->parseChunk( $myTpl, $row, '[+', '+]');


          I can’t shake the feeling that the "parseChunk" function does something tricky because it’s expecting to look up a chunk by its name and THEN get the string with the placeholders... giving it the string right off the bat just doesn’t feel right to me...

          Am I missing something obvious? Thanks.
            • 3749
            • 24,544 Posts
            Quote from: Everettg_99 at Dec 30, 2008, 12:05 AM

            Hmm... now that I’m sitting down to put in some default values, I can’t seem to make this work.

            Here’s what I have in a nutshell:
            $myTpl = isset($item_chunk)? $item_chunk: 'Page Title: [+pagetitle+]<br/>';
            
            $output.=$modx->parseChunk( $myTpl, $row, '[+', '+]');


            I can’t shake the feeling that the "parseChunk" function does something tricky because it’s expecting to look up a chunk by its name and THEN get the string with the placeholders... giving it the string right off the bat just doesn’t feel right to me...

            Right. It looks like the third part of the ternary operator (where you have ’Page Title: [+pagetitle+]
            ’) should just be the name of the tpl chunk and, since it’s a default chunk, you should know its name. I’m not completely sure what you’re trying to do, but I suspect that ’Page Title: [+pagetitle+]
            ’ should go in the chunk itself.

            $myTpl = isset($item_chunk)? $item_chunk: 'name_of_default_chunk';
            $output.=$modx->parseChunk( $myTpl, $row, '[+', '+]');
            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
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              Thanks for the note, Bob. What I’m trying to do is create a Snippet that’s more portable. To achieve that, I’d like to have the default chunk contents contained within the Snippet. In my example, you can pass the name of the chunk to use for the "item_chunk", but if no name is supplied (and lets assume that the chunk doesn’t even exist), I’d like to just have the default text that would have come from the chunk if it did exist and its name had been passed. E.g. something simple like "Page Title: [+pagetitle+]". I’d like to have within the Snippet the text from that chunk so if someone hadn’t created a specialized chunk for this snippet, they could just use the default values that the Snippet had on board. Does that make sense?
                • 20413
                • 2,877 Posts
                Is this the concept ---> http://modxcms.com/forums/index.php/topic,31376.msg190240.html#msg190240  :)

                //except this is from a file.
                  @hawproductions | http://mrhaw.com/

                  Infograph: MODX Advanced Install in 7 steps:
                  http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                  Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                  http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                  • 3749
                  • 24,544 Posts
                  Quote from: Everettg_99 at Dec 30, 2008, 12:10 PM

                  Thanks for the note, Bob. What I’m trying to do is create a Snippet that’s more portable. To achieve that, I’d like to have the default chunk contents contained within the Snippet. In my example, you can pass the name of the chunk to use for the "item_chunk", but if no name is supplied (and lets assume that the chunk doesn’t even exist), I’d like to just have the default text that would have come from the chunk if it did exist and its name had been passed. E.g. something simple like "Page Title: [+pagetitle+]". I’d like to have within the Snippet the text from that chunk so if someone hadn’t created a specialized chunk for this snippet, they could just use the default values that the Snippet had on board. Does that make sense?

                  Ahh. In that case, you don’t want to use parsechunk() in the default case, since it requires the name of an actual chunk as its first argument. Ditto and Wayfinder do something similar because it’s faster than parsechunk().

                  You’ll still use parsechunk() if a tpl name is sent in the snippet call, but for the default case, you’ll want to use something like this:


                  $placeholders = array(
                       '[+pagetitle+]' =>   'Page Title:',
                       '[+something else+]' => 'Other'  
                  }
                  
                  str_replace( array_keys( $placeholders ), array_values( $placeholders ), $tpl );


                  Unless you only have one placeholder, then you can do a straight str_replace(’[+pagetitle+]’,’Page Title: ’);
                    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