Snippets - Echo / Return out HTML Code

Just wondering…what’s the best way to echo / return HTML code in a snippet? I’ve been using echo but was told return is better. Is there a better way to return this HTML code that also has a getResources Chunk in it. When I do it this way, the code comes out but the getResources is blank.

echo "<div id=\"menu7\" class=\"tab-pane active\">";
  echo "<div class=\"row\">";
    echo "<div class=\"col-12 pd1\">";
      echo "[[!getResources? &parents=`37` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC`]]";               
    echo "</div>";
  echo "</div>";
echo "</div>";

Any help would be appreciated as my PHP is still limited.

The best way is probably to put your html code in a chunk and then use $modx->getChunk('ChunkName'); in the snippet.

1 Like

I have a template that is returning a few snippets. One for Monday all the way up to Sunday.

<div class="col-xl-12 col-lg-12 col-md-12">
  <div class="tab-content">               
    [[!schedulePageContentMonday]]
    [[!schedulePageContentTuesday]]
    [[!schedulePageContentWednesday]]
    [[!schedulePageContentThursday]]
    [[!schedulePageContentFriday]]
    [[!schedulePageContentSaturday]]
    [[!schedulePageContentSunday]] 
  </div>              
</div>

Then inside the snippet, it will return a certain HTML code depending on if it’s the current Monday for example and if not, will set it to the next Monday;

if(date('N') == 1){        
    
    echo "<div id=\"menu1\" class=\"tab-pane active\">";
      echo "<div class=\"row\">";
        echo "<div class=\"col-12 pd1\">";
          echo "[[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC`]]"; 
          echo "It's This Monday";                 
        echo "</div>";
      echo "</div>";
    echo "</div>";
  }
  else {
    // Modify the date it contains
    $date->modify('next monday');
    
    echo "<div id=\"menu1\" class=\"tab-pane fade\">";
      echo "<div class=\"row\">";
        echo "<div class=\"col-12 pd1\">";
          echo "[[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC` ]] ";  
          echo "It's Next Monday";                
        echo "</div>";
      echo "</div>";
    echo "</div>";
  }

It’s all working for me except the echo getResources is coming up blank.

Like I said, you could use a chunk like this

<div id="menu1" class="tab-pane active">
  <div class="row">
    <div class="col-12 pd1">
      [[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC`]]
      It's This Monday                 
    </div>
  </div>
</div>

and return it in the snippet with code like this

<?php
if(date('N') == 1){          
	return $modx->getChunk('mychunk');
 } else {
	...
 }

Or maybe you can do something like this (which is much uglier):

<?php
$output = '<div id="menu1" class="tab-pane active">';
$output .= '<div class="row">';
$output .= '<div class="col-12 pd1">';
$output .= "[[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC`]]";
$output .= "It's This Monday";
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";
return $output;

Or maybe even call getResources in the snippet with runSnippet():

<?php
$getResources_result = $modx->runSnippet('getResources', [
    'parents' => 25,
    'depth' => 1,
    'limit' => 0,
    ...
]);

$output = '<div id="menu1" class="tab-pane active">';
$output .= '<div class="row">';
$output .= '<div class="col-12 pd1">';
$output .= $getResources_result;
$output .= "It's This Monday";
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";
return $output;

Or combine the two:

$getResources_result = $modx->runSnippet('getResources', [
    'parents' => 25,
    'depth' => 1,
    'limit' => 0,
    ... //add the other parameters here
]);

return $modx->getChunk('mychunk', ['resources' => $getResources_result]); //set the result from getResources as the placeholder 'resources'

Chunk mychunk:

<div id="menu1" class="tab-pane active">
  <div class="row">
    <div class="col-12 pd1">
      [[+resources]]
      It's This Monday                 
    </div>
  </div>
</div>

Thank you so much for your help. I really appreciate it. I’ve tried these and I’m still getting a blank space where the getResources should be.

$output .= "[[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC`]]";

I’ve tried this code, where I added the \ before the ` just in case but still blank

$output .= "[[!getResources? &parents=\`25\` &depth=\`1\` &limit=\`0\` &tpl=\`scheduleList_GA\` &includeContent=\`1\` &includeTVs=\`1\` &processTVs=\`1\` &showHidden=\`0\` &sortby=\`menuindex\` &sortdir=\`ASC\`]]";

It can’t seem to pick up on the code. I’ll keep trying

Does it work if you put the getResources call directly in the template and not in a snippet (just to test it)?
Maybe there is a problem with this call (like wrong parent, misspelled template name, …)

I’ve tested it there again and it does work

And when you put a snippet in the template [[!mySnippet]] that just returns the getResources call, does that work?

<?php
return "[[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC`]]";

I’ve done as is exactly above and it doesn’t work. It’s just blank. Returning a getResources in a snippet is the problem by the looks of it.

Well that is weird!

Do you call this snippet directly in the template of the page or is this call nested much deeper inside other tags?

Do you use the normal MODx parser class?

The simple one you told me to try there is directly in the template. It’s not nested

Not sure what MODx parser class is

Do you have a system setting parser_class and what is its value?

You should be able to replace the line in your snippet containing the getResources tag with this code:

$params = array (
   'parents' => 37,     
    'depth' => 1,
    'limit' => 0,     
    'tpl' => 'scheduleList_GA',     
    'includeContent' => 1,     
    'includeTVs' => 1,
    'processTVs' => 1,    
    'showHidden' => 0,    
    'sortby' => 'menuindex',     
    'sortdir' => 'ASC',
);

echo $modx->runSnippet('getResources', $params); 

The runSnippet() call will return the results of getResources and will avoid any cache problems because it will always run the snippet uncached.

BTW, once you get things working, it should be possible to replace your seven snippets with one snippet that loops through an array of day names to produce all your output. It will be much faster than calling seven different snippets.

Is this the same as parser_recurse_uncacheable? If so it’s set to YES

Do I just change the code in the new snippet I set up to test from this -

return "[[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleList_GA` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC`]]";

to this -

$params = array (
   'parents' => 37,     
    'depth' => 1,
    'limit' => 0,     
    'tpl' => 'scheduleList_GA',     
    'includeContent' => 1,     
    'includeTVs' => 1,
    'processTVs' => 1,    
    'showHidden' => 0,    
    'sortby' => 'menuindex',     
    'sortdir' => 'ASC',
);

echo $modx->runSnippet('getResources', $params); 

Unfortunately Bob my coding is limited. I’d love to be able to replace your seven snippets with one snippet that loops through an array of day names to produce all your output.

I tired that code Bob and it’s coming up blank also.

No, but if the setting parser_class doesn’t exist, you are using the standard class.


I can’t figure out why this doesn’t work. It should work as far as I know.

Maybe to simplify it further, you could try running this test:

Directly in the template call the snippet myFirstSnippet uncached.

[[!myFirstSnippet]]

Snippet myFirstSnippet

<?php
return "[[!mySecondSnippet]]";

Snippet mySecondSnippet:

<?php
return "return value from mySecondSnippet";

Does it work if you change 37 to the correct value of 25?

I tried this and it worked

I had changed it to 25. Can I ask…does this

$params = array (
   'parents' => 25,     
    'depth' => 1,
    'limit' => 0,     
    'tpl' => 'scheduleList_GA',     
    'includeContent' => 1,     
    'includeTVs' => 1,
    'processTVs' => 1,    
    'showHidden' => 0,    
    'sortby' => 'menuindex',     
    'sortdir' => 'ASC',
);

echo $modx->runSnippet('getResources', $params); 

go into my snippet like this

$date = new DateTime();

if(date('N') == 1){        
    
    $output = '<div id="menu1" class="tab-pane active">';
      $output .= '<div class="row">';
        $output .= '<div class="col-12 pd1">';
          
          $params = array (
           'parents' => 25,     
            'depth' => 1,
            'limit' => 0,     
            'tpl' => 'scheduleList_GA',     
            'includeContent' => 1,     
            'includeTVs' => 1,
            'processTVs' => 1,    
            'showHidden' => 0,    
            'sortby' => 'menuindex',     
            'sortdir' => 'ASC',
          );
            
          echo $modx->runSnippet('getResources', $params); 
          
        $output .= "</div>";
      $output .= "</div>";
    $output .= "</div>";
    
    return $output;
  }
  else {
    // Modify the date it contains
    $date->modify('next monday');
    
    echo "<div id=\"menu1\" class=\"tab-pane fade\">";
      echo "<div class=\"row\">";
        echo "<div class=\"col-12 pd1\">";
          echo "[[!getResources? &parents=`25` &depth=`1` &limit=`0` &tpl=`scheduleListHomepage` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &showHidden=`0` &sortby=`menuindex` &sortdir=`ASC` ]] ";  
          echo "It's Next Monday";                
        echo "</div>";
      echo "</div>";
    echo "</div>";
  }

Instead of using echo, add the result of runSnippet() to the output string $output. (Otherwise it won’t be placed inside the <div> tags.)

...
$output = '<div id="menu1" class="tab-pane active">';
      $output .= '<div class="row">';
        $output .= '<div class="col-12 pd1">';
          
          $params = array (
           'parents' => 25,     
            'depth' => 1,
            'limit' => 0,     
            'tpl' => 'scheduleList_GA',     
            'includeContent' => 1,     
            'includeTVs' => 1,
            'processTVs' => 1,    
            'showHidden' => 0,    
            'sortby' => 'menuindex',     
            'sortdir' => 'ASC',
          );
            
          $output .= $modx->runSnippet('getResources', $params); 
          
        $output .= "</div>";
      $output .= "</div>";
    $output .= "</div>";
    
    return $output;
...