Forum

Thread tagged as: Question

Issue with perch_content_custom and cards-layout

So, I am aware, that this layout isn't called "cards-layout", but my english skills are obviously not advanced enough to find a name for this kind of layout:

Layoutthingy

This is what I normally do, in order to get what I want:

<?php

    $i = 0;
    $column = Array();

    foreach( $array_of_items as $item) {
        $column[$i%3] .= $item[contents];
        $i++;
    }

?>
<div class="column_1"><?=$column[0];?></div>
<div class="column_2"><?=$column[1];?></div>
<div class="column_3"><?=$column[2];?></div>

Now in Perch, I have a multiple region, where I wanna do the exact same thing. But, when I try to retrieve the contents with perch_content_custom, the html still gets displayed. I could do

$array = perch_content_custom('region', Array(
    'skip-template'=>true,
    'raw'=>true
), true);

and then iterate over the $array with foreach. But then I'd have to reuse the code I already have in my custom template. Isn't there a way to retrieve the contents of a multiple region as an array without skipping the template? I tried

<?php

    $column = Array();
    $i = 0;

    $array = perch_content_custom('region', Array(
        'each'=>function($this) {
            $column[$i%3] .= $this;
            $i ++;
            return $this;
        }
    ), true);

?>

But this doesn't work.

So, does anyone have any Idea how to solve this? I'm fine with just reusing the code from my template for this particular case and iterating over the raw data, but I was just curious, if this is also solvable without sacrificing the template.

Thomas Semmler

Thomas Semmler 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think what you want is the skip-template option.

$array = perch_content_custom('region', array(
       'skip-template' => true
    ), true);

You can then iterate through $array

Yes, but the template is lost with this method. Isn't there an option to retrieve the region as array without having to skip the template?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Even if written correctly, the each method won't help, as it's pre-template.

We have had a request for an option to be able to get the individual templated items as an array, which I think would be useful here too.

It would be very useful! Anyway - I solved this by just iterating over the array and rewriting the markup. Thanks for answering my questions.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Remember you can always use perch_template() to template an array.

Drew McLellan said:

Remember you can always use perch_template() to template an array.

That is in fact exactly, what I was looking for but was unable to find - thank you so much!