We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36760
    • 136 Posts
    I wasn't sure which question I should be asking here, but here's where I'm at. Happy to change the title once I know exactly what I'm asking for. smiley

    I have some HTML and accompanying JavaScript that creates an "expanding box," basically a single-section accordion. Right now, I just hard-code the HTML into the RichText editor. I want to build a snippet (or something similar) so it's easier to visualize in the editor and easier for someone not familiar with coding to create a box. This worked out fine, except that I have other snippets that create links based on database information that will sometimes appear inside the "expand box" snippet. Putting the "link" snippets inside the parameters of the "expand box" snippet doesn't work, as I'm sure is expected/intended.

    This is my "expandBox" snippet.
    $modx->setPlaceholder('title', $title);
    $modx->setPlaceholder('titleTag', $titleTag);
    $modx->setPlaceholder('preContent', $preContent);
    $modx->setPlaceholder('content', $content);
    
    return $modx->getChunk('expandBoxTpl');


    This is the "expandBoxTpl" chunk.
    <div class="expand-box gray-box clear-both">
        <[[+titleTag:isnot=``:then=`[[+titleTag]]`:else=`h3`]] class="expander" style="[[+preContent:is=``:then=`margin-bottom:0`]]">[[+title]]</[[+titleTag:isnot=``:then=`[[+titleTag]]`:else=`h3`]]>
        [[+preContent]]
        <div class="expanded">[[+content]]</div>
    </div>


    And this is what the snippet might look like in action.
    [[expandBox? &title=`Box Title` &titleTag=`h4` &preContent=`
    
    Some summary content with another snippet inside it [[dbSnippet? &id=`123456`]]. And another one: [[dbSnippet? &id=`456123`]]
    
    ` &content=`
    
    The main content that's hidden until the box is expanded, also with other snippets inside it: [[dbSnippet? &id=`123456`]].
    
    `]]
    


    Everything works great until I add snippets inside the parameters.

    I guess my question is are there any other ways I should be going about this entire concept? If not, is there any way to make this work similar to what I have above? I know there's the runSnippet option that I've used inside snippets before, but that doesn't seem to apply here as the nested snippets will be numerous and random, accompanied with regular text, and so on.

    I appreciate the help!

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      Here's something to try. I'm not sure it will help:

      $fields = array (
          'title' => $title,
          'titleTag' => $titleTag,
          'preContent' => $preContent,
          'content' => $content,
       
      return $modx->getChunk('expandBoxTpl', $fields);


      Try calling the db snippets uncached:

      [[!dbSnippet? ... ]]


      Since you are calling the same dbSnippet more than once, the snippets tags would have to be called uncached in any case.

      I don't see any reason what you're doing shouldn't work, as long as the dbSnippet calls aren't used as properties in another tag -- I can't tell if they are or not.

      I would be inclined to put all the logic (including the conditionals) in the snippet. Something like this (untested):

      TPL chunk:

      <div class="expand-box gray-box clear-both">
          <[[+tag]] class="expander" [[+style]]> [[+title]] </[[+tag]]>
          Some summary Content [[!dbSnippet? &id=`123456`]] and another one: [[!dbSnippet? &id=`23456`]]
          <div class="expanded">[[+content]]</div>
      </div>


      Snippet

      
      /* Your code to set the four variables here */
      
      $fields = array(
          'tag' => empty($titleTag)? 'h3' : $titleTag,
          'style' => empty($preContent)? 'style="margin-bottom:0" ' : '',
          'preContent' => $preContent,
          'content' => $content,
          'title' => $title,
      }
      
      return $modx->getChunk('', $fields);


      If that doesn't work, I would replace the dbSnippet calls with placeholders in the Tpl chunk:

      [[+dbSnippet1]  ... [[+dbSnippet2]]
      

      Then you could pass the ids as properties in the snippet tag.

      &id1=`12345` &id2=132453`
      


      In the snippet:

      $id1 = $modx->getOption('id1', $scriptProperties);
      $id2 = $modx->getOption('id2', $scriptProperties);


      Then in the $fields array, add:

          'dbSnippet1 => $modx->runSnippet('dbSnippet', array('id' => $id1)),
          'dbSnippet2 => $modx->runSnippet('dbSnippet', array('id' => $id2)),
      



      I'm not sure if this all fits your use case, but if it does, it would be way faster. With the conditional output modifiers, MODX has to parse the conditional, then calculate the values for all cases whether they're used or not, then select which ones to actually use. That tends to be fairly slow, especially since parts of it must be called uncached.






        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
        • 36760
        • 136 Posts
        Thanks for all the help!

        My original setup works if I call the expandBox snippet uncached (along with the dbSnippets). I got too hung up looking in the wrong places!

        I took your advice on including the conditions inside the snippet, and that worked fine too.