Snippets - Echo / Return out HTML Code

That still doesn’t make sense. So to recapitulate:

The following works:

Template (uncached getResources call directly in the template):

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

And the following doesn’t works.

Template (uncached snippet call in template):

[[!mySnippet]]

Snippet mySnippet:

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

But the same works with a different snippet than getResources!

Template (uncached snippet call in template):

[[!mySnippet]]

Snippet mySnippet:

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

Snippet myOtherSnippet:

<?php
return "some value";

No joy there either. I’m gonna put up the whole thing to see if I’m missing something.

The template I’m working on is my schedule page. I want to see the current schedule here. So if it’s Monday it’ll make the Monday schedule ACTIVE and the others will be FADE. It’s laid out like this:

  • Schedule - ID 7
    • Monday - ID 25
      • Programme 1
      • Programme 2
      • Programme 3
    • Tuesday - ID 27
      • Programme 1
      • Programme 2
      • Programme 3
    • Wednesday - ID 29
      • Programme 1
      • Programme 2
      • Programme 3
    • Thursday - ID 31
      • Programme 1
      • Programme 2
      • Programme 3
    • Friday - ID 33
      • Programme 1
      • Programme 2
      • Programme 3
    • Saturday - ID 35
      • Programme 1
      • Programme 2
      • Programme 3
    • Sunday - ID 27
      • Programme 1
      • Programme 2
      • Programme 3

This is the code inside the template. It shows 7 snippets for the Navigation Days and 7 snippets for the content.

<div class="col-lg-12 col-md-12 col-sm-12">
  <ul class="nav nav-tabs">
    [[!schedulePageNavMonday]]
    [[!schedulePageNavTuesday]]
    [[!schedulePageNavWednesday]]
    [[!schedulePageNavThursday]]
    [[!schedulePageNavFriday]]
    [[!schedulePageNavSaturday]]
    [[!schedulePageNavSunday]] 
  </ul>
</div>
        
<div class="col-xl-12 col-lg-12 col-md-12">
  <div class="tab-content">               
    [[!schedulePageContentMonday]]
    [[!schedulePageContentTuesday]]
    [[!schedulePageContentWednesday]]
    [[!schedulePageContentThursday]]
    [[!schedulePageContentFriday]]
    [[!schedulePageContentSaturday]]
    [[!schedulePageContentSunday]] 
  </div>              
</div>

The snippet code for schedulePageNavMonday-Sunday is like this. The Monday one for example checks if it’s Monday, if so it adds code and if not adds a different code. The only difference is that it adds a class called ACTIVE and the date if it is the Monday and if not, it won’t add the date and add a class called FADE.

// Create a new DateTime object
$date = new DateTime();

if(date('N') == 1){
    echo "<li class=\"nav-item\"> <a class=\"nav-link active\" data-toggle=\"tab\" href=\"#menu1\">" . $date->format("l") . " " . $date->format("d") . " " . $date->format("F") ." ". $date->format("Y") ."</a></li>";
  }
  else {
    // Modify the date it contains
    $date->modify('next monday');
    
    echo "<li class=\"nav-item\"> <a class=\"nav-link\" data-toggle=\"tab\" href=\"#menu1\">Monday</a></li>";
  }

Then the snippet for the content is similar to the navigation. It adds a class called ACTIVE if current and FADE if not.

$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',
        );
        
        $output .= $modx->runSnippet('getResources', $params); 
      
      $output .= "</div>";
    $output .= "</div>";
  $output .= "</div>";
  
  return $output;
}
  else {
    // Modify the date it contains
    $date->modify('next monday');
    
    $output = '<div id="menu1" class="tab-pane fade">';
    $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>";
}

It picks up on the output DIVS. Just not the getResources part.

Yes…this is correct. Calling the getResources inside a Snippet doesn’t seem to work

But why? When I test it on my local installation it works perfectly fine.

  • Are you sure the child-resources aren’t hidden ( “Hide From Menus” is unchecked)?
  • Are there any error messages in the error log?
  • If you add the property &debug=`1` to the getResources call, can you see the logged SQL queries in the error log (SELECT ...) . Does this SQL query returns anything if you execute it in phpMyAdmin?

I’m so so sorry. I never noticed this. I’ve been at this all day and only now noticing it’s 0. The getResources I tested earlier that this at 1. That’s why it worked and the snippet didn’t. I’m so so sorry halftrainedharry for wasting your time.

No problem. I knew it had to be something stupid like this. :wink:


Regarding your snippet:

You probably could achieve the same with a simple getResources call without the snippets schedulePageContentMonday etc.

For example:
Call to getResources with a wrapper template myWrapper and a custom property dayNr to indicate the day.

[[!getResources? 
    &parents=`25` 
    &depth=`1` 
    &limit=`0` 
    &tpl=`scheduleList_GA` 
    &includeContent=`1` 
    &includeTVs=`1` 
    &processTVs=`1` 
    &showHidden=`0` 
    &sortby=`menuindex` 
    &sortdir=`ASC`
    &tplWrapper=`myWrapper`
    &wrapIfEmpty=`1`
    &dayNr=`1`
]]

Chunk myWrapper (calls the snippet isactive):

<div id="menu1" class="tab-pane [[!isactive? &dayNr=`[[+dayNr]]`]]">
  <div class="row">
    <div class="col-12 pd1">
      [[+output]]
    </div>
  </div>
</div>

Snippet isactive (returns “active” or “fade” according to the property dayNr):

<?php
if(date('N') == $dayNr){
    return "active";
} else {
    return "fade";
}