We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5340
    • 1,624 Posts
    Hi,

    I still can’t figure out if I can output only documents that have a certain template(s).

    For example I have the following structure

    Events
    2010
    01
    Event 1

    I need to display all the events but the parents. I know I can use "hideContainers" in this situation but it’s not as safe as having a "filter by template" solution.

    Any help appreciated.



      • 22942
      • 3 Posts
      This is exactly what i need as well. Has this functionality been added to getResources or does anyone have suggestions on how to do so?

      Thanks in advance!

      Btw. Loving Revolution really much!
        • 5340
        • 1,624 Posts
        I don’t know if it getResources can do that but here’s the code I used (not tested it’s just a copy paste from a bigger code)


        <?php
        
        //config
        
        $templateId = 9;
        $limit = 500;
        $offset = 0;
        
        $contextResourceTbl = $modx->getTableName('modContextResource');
        $context = empty($context) ? $modx->quote($modx->context->get('key')) : $modx->quote($context);
        $criteria = $modx->newQuery('modResource', array(
            'deleted' => '0'
            ,'published' => '1'
        	,'isfolder' => '0'
        	,"`modResource`.`template` = {$templateId}"
            ///I have no clue how to use contexts.
        	,"(`modResource`.`context_key` = {$context} OR EXISTS(SELECT 1 FROM {$contextResourceTbl} `ctx` WHERE `ctx`.`resource` = `modResource`.`id` AND `ctx`.`context_key` = {$context}))"
        ));
        
        $criteria->limit($limit, $offset);
        
        if (!empty($debug)) {
            $criteria->prepare();
            $modx->log(modX::LOG_LEVEL_ERROR, $criteria->toSQL());
        }
        
        $collection = $modx->getCollection('modResource', $criteria);
        
        foreach ($collection as $resourceId => $resource) {
        	
        	//this loops through the resources
        	//$resource->get('id'); //maybe $resource->id
        	//$resource->pagetitle;
        	
        }
        
        ?>

          • 22942
          • 3 Posts
          Hi Cipa,

          Thanks for your response. I have found another, I think more efficient, way to do this by using the ’If’-plugin. Within the tpl-chunk I put an if-statement that checks the id of the template it’s using. Like so:

          Template chunk ’news’:
          [[!If?
             &subject=`[[+template]]`
             &operator=`EQ`
             &operand=`4`
             &then=`
                Output my content with template id '4'.
             `
          ]]
          


          Then, the getResources call looks like this:
          [[!getResources?
          &parents=`[[*id]]`
          &limit=`3`
          &tpl=`news`
          &includeContent=`1`
          &includeTVs=`1`
          ]]
          
            • 37329
            • 10 Posts
            Hi, PeNr.1

            I found this online :

            		[[!getResources? 
            			&tpl=`flexslider_tpl`
            			&tplFirst=`flexslider_tplFirst`
            			&tplLast=`flexslider_tplLast`
            			&includeTVs=`1`
            			&processTVs=`0`
            			&where=`{"template:=":3}`
            			&tvPrefix=``
            			&limit=`0`
            		]]


            A where clause with some JSON behind it, i found it at
            Works like a charm.

            http://rtfm.modx.com/display/ADDON/getResources