We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34120
    • 236 Posts
    I’ve got a snippet that fetches rows from a database table, sets the values to placeholders and then parses a chunk. My problem is when there are empty fields in the database I don’t want the markup for the chunk to be displayed So if, for example, an item doesn’t have ’base’ content then don’t display the relevant <td></td> markup. I can’t think of a way to do this other than generating the markup in my snippet but I’ve read that snippets shouldn’t contain presentation layer code.

    <?php 
    foreach ($rows as $row) {  
        $values = array(
            'dwTitle' => $modx->setPlaceholder('dwTitle', $row->get('title')),
            'dwCode' => $modx->setPlaceholder('dwCode', $row->get('code')),
            'dwBase' => $modx->setPlaceholder('dwBase', $row->get('base')),
            // etc. etc.
            );
        $output .= $modx->parseChunk('productDetail', $values, '[[+', ']]');
    }
    return $output;

    My chunk looks like this
    <tr><td>[[+dwTitle]]</td><td>[[+dwCode]]</td><td>[[+dwBase]]</td></td></tr>
      • 3749
      • 24,544 Posts
      Well, most of us cheat sometimes -- it’s a guideline, not a statute. wink

      That said, I would probably create a rowTpl chunk and an item Tpl chunk and only insert the itemTpl chunk if the field was not empty.

        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
        • 36404
        • 307 Posts
        hi,

        just my two cents, i think it’s not that complicated to do (actually, i do so with complete rows in some snippets... smiley) but in your case it’s not a good idea because, if you do that, not only will you need not to display the td but, well, at least if you want to have a valid output, what i hope smiley, will you have to put a colspan="2" on the previous or the next one.

        the in your case, the only valid way i can see to do what you’re looking for would be to prepare several chunks, forecasting all the possible case you can have and then, use a switch to have modx parse the correct chunk depending on what is == "" in your db output

        have swing
          réfléchir avant d'agir
          • 34120
          • 236 Posts
          Thanks for the replies guys.
          @Bob, I’m not sure I approve wink Seriously though thanks for the reality check, I can easily get hung up on wanting to do things the ’right’ or most efficient way.

          As it happens I’m a bit stuck at the moment with or without cheating. I’ve posted in the xPDO forum though so fingers crossed.