We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17410
    • 13 Posts
    Hey guys, new MODx user here.  Just been doing web stuff for the past month.  Started out hand-coding with coffeecup.  Moved to wordpress.  Now I’m making what’s hopefully my last jump.  I don’t understand half of what I read about MODx, but the parts I do understand, I love.  I hope this community grows into the biggest CMS comunity there is.

    On to my question...  I read in the documentation that html should be kept in chunks and php should be kept in snippets.  Snippets should only "return" values and not "echo" code.  Is that a law?  Or more of a guideline for keeping code clean?

    The reason I ask, I am trying to generate dynamic content in a chunk.  I considered using setPlaceholder in the php code, but I couldn’t figure out a way to generate a dynamic list with those placeholders back in the chunk.

    Here’s the code I’m using to give you an idea of what I’m going for.  It works as it is right now, I just want to make sure I won’t run into an error with it later down the line.

    Could I split this up into 2 snippets?  Is there a way to include a snippet inside a snippet so I’d be able to call the placeholders from the first snippet?

    <?php
    $i=0;
    $sql = 'SELECT * FROM id_links WHERE 1';
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result))
    {
    $modx -> setPlaceholder('offerurl'.$i,$row[offerurl]);
    $modx -> setPlaceholder('name'.$i,$row[name]);
    $modx -> setPlaceholder('campaignid'.$i,$row[campaignid]);
    $modx -> setPlaceholder('description'.$i,$row[description]);
    $modx -> setPlaceholder('instant'.$i,$row[instant]);
    $modx -> setPlaceholder('commission'.$i,$row[commission]);
    $modx -> setPlaceholder('countries_comma'.$i,$row[countries_comma]);
    $modx -> setPlaceholder('reporting'.$i,$row[reporting]);
    $i++;
    }
    $i = 0;
    while ($i < 10)
    {
    $login_id = $modx->getLoginUserID();
    $str = '<li><a href="[+offerurl'.$i.'+]'.$login_id.'">[+name'.$i.'+]</a></li>[+description'.$i.'+] - [+reporting'.$i.'+]';
    mysql_real_escape_string($str);
    echo ($str);
    $i++;
    }
    return;
    ?>


    Thanks for the CMS and thanks for any help on this.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Not using echo statements is by no means a law. But the echo output may not appear where you expect it to. It’s usually better to gather your dynamic HTML output into a string variable, then return the final string at the end.

      Placeholders are mostly of use for allowing different layout templates for the snippet’s output, or for allowing the placeholder’s value to be output at various locations on the page.

      As far as including another snippet, yes, you can use the API function $modx->runSnippet(’snippetName’, array(’param1’=>’value1’, ’param2’=>’value2’));
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 17410
        • 13 Posts
        Ah, thanks. smiley

        So I guess I’ll go to something like :

        $str = $str.'<li><a href="[+offerurl'.$i.'+]'.$login_id.'">[+name'.$i.'+]</a></li>[+description'.$i.'+] - [+reporting'.$i.'+]';
        return $str;
        


        Righto?
          • 22303 MODX Staff
          • 10,725 Posts
          PHP Snippets and HTML Chunks are separate to encourage separation of presentation and logic. Mixing PHP code and HTML output is a bad habit, encouraged by PHP itself. This separation is the foundation of what makes MODx different (and more desirable IMO) from other CMS systems, especially with regard to templating. It can take a little more work to isolate logic and presentation, but the benefits in the long run are well worth it.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            If you want to use a placeholder in snippet code, use $modx->getPlaceholder(’name’);
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org