Forum

Thread tagged as: Error, Suggestions

Categories with perch_template

I think perch_template($template,$data) is unable to process categories.

That's in the template:

<perch:content id="projectTitle" type="text" />
<perch:categories id="projectTheme">
  <span class="label"><perch:category id="catTitle" type="text"/></span>
</perch:categories>
<perch:showall />

This one renders Title, Categories and "Showall":

              $opts = array(
                 'template' => 'fe/project-teaser.html',
                 'sort'=>'projectTitle',
                 ),
              );

            perch_collection('Items',$opts);

While this doesn't; it only renders the title and showall:

              $opts_skip = array(
                 'skip-template' => true,
                 'sort'=>'projectTitle',
                 ),
              );

            $arr = perch_collection('Items',$opts_skip,true);

            // do something with our obtained Data
            $arr = doSomethingWithTheObtainedData($arr);

            perch_template('content/fe/project-teaser.html',$arr);

My guess is that perch_template was introduced before categories, and therefore it's not told to understand that namespace yet.

PS I'm on 2.8.29

Urs Bräm

Urs Bräm 1 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's to be gained by the above code?

It's just an example. I've added in a comment where something would happen

perch_template can be very useful if you have to do stuff that's not covered by the built in filters. If you use categories in the template, and if my theory (that categories aren't rendered there) is right, it's not usable there, though.

Luckily, I found a workaround for my case, which turned out to be a better solution than what I had before with perch_template and some prior array juggling.

I have to filter a collection by two OR and some AND conditions. AFAIK that's not possible yet. My current solution.

In a first $ids = perch_collection('Items',true) call, make the "OR" queries: give me entries that are either this or that. Use this template:

<perch:content id="_id" /><perch:if exists="perch_item_last"><perch:else />,</perch:if>

The result is a comma separated ID list.

In the second call, we can pass in the IDs from the first "query", using the 'in' match (which accepts comma separated values, thanks!):

'filter'=>array(
        array(
          'filter'=>'_id',
          'match'=>'in',
          'value'=>$ids,
        ),
),

Voilà!

This approach serves as a nice replacement for perch_template for me atm.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you looked at using the each callback for this?

I'm already using the each callback for something else (overlaying a partial translation record over the parent record), so that didn't occur to me....

let's see... no,I think I've tried it and it didn't work out

I was under the impression that

                'each' => function($item){
                  if($item['readyForPublishing']=='yes'){
                  return $item;
                  }
                },

wouldn't fully 'pop' the entry from the results before rendering them. Was that wrong?

With my workaround, I can do RAND and count => 1 for the results. I think with the 'each' variant there were empty random items picked. But I don't recall it 100%