Forum

Thread tagged as: Question, Problem

Creating a region that uses two templates. What am I doing wrong?

Hi everyone,

I'm trying to create a custom region that uses two templates. The region doesn't show up in the admin section. I'm sure I'm doing something wrong, but I can't figure out what that is. I'm using the following code:

<?php perch_content_create('Regionname'); ?>
<?php
   perch_content_custom('Regionname', array(
   'template' => 'template1.html',
   ));
?>
<?php
   perch_content_custom('Regionname', array(
   'template' => 'template2.html',
   )); 
?>
Edwin Venhorst

Edwin Venhorst 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That looks like it should probably work. What appears on the page itself?

Just an empty div where the code should appear. The rest of the page works just fine. I've checked if perch_content creates a region, that works fine too. So I figured I must be doing something wrong with the perch_content_create function.

Just in case you need my diagnostics code:

Perch: 2.8.15, PHP: 5.5.25, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 15d5c781cfcad91193dceae1d2cdd127674ddb3e $, with PDO
Server OS: Linux, apache2handler
Installed apps: content (2.8.15), assets (2.8.15), categories (2.8.15)
App runtimes: <?php $apps_list = array( 'content', 'categories', );
PERCH_LOGINPATH: /perch
PERCH_PATH: /home/flexiuy104/domains/speeltuin.edwinvenhorst.nl/public_html/perch
PERCH_CORE: /home/flexiuy104/domains/speeltuin.edwinvenhorst.nl/public_html/perch/core
PERCH_RESFILEPATH: /home/flexiuy104/domains/speeltuin.edwinvenhorst.nl/public_html/perch/resources
Image manipulation: GD
PHP limits: Max upload 20M, Max POST 20M, Memory: 256M, Total max file upload: 20M
Resource folder writeable: Yes
HTTP_HOST: speeltuin.edwinvenhorst.nl
DOCUMENT_ROOT: /home/flexiuy104/domains/speeltuin.edwinvenhorst.nl/public_html
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you add debug to the page, what does it output?

Simon Clay

Simon Clay 127 points

Hi, I have been testing this out and discover:

1) Perch Content Create only seems to create a new region if you specify a template e.g. at the very least:

<?php perch_content_create('Perch Content Create needs a template', array(
    'template' => 'article.html',
    )); 
?>

I didn't think that was the case in the past, but it seems so now.

Secondly and surprisingly, I find that Perch Content Custom now actually creates a region:

<?php
    perch_content_custom('This Will Create a Region'); 
?>

This is the page debug-output:

Debug Message
SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Regionname' AND (regionPage='/index.php' OR regionPage='*')
No matching content regions found. Check region name (Regionname) and page path options.
SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE idx.regionID IS NULL AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/template1.html
No matching content regions found. Check region name (Regionname) and page path options.
SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE idx.regionID IS NULL AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/template2.html
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/index.php' OR regionPage='*' ORDER BY regionPage DESC

Simon Clay said:

Secondly and surprisingly, I find that Perch Content Custom now actually creates a region:

<?php
  perch_content_custom('This Will Create a Region'); 
?>

You're right Simon, thanks for testing. I've also tested it and Perch Content Custom does seem to create it's own region. But now I don't know if it is supposed to do that and second, I don't know if it's possible to use two templates.

Simon Clay

Simon Clay 127 points

It's definitely possible to use 2 templates. You can use Perch Content Custom to output in any number of different templates.

You'd have 1 main template for the editor to enter all the information for the region. Eg:

<?php perch_content_create('Perch Content Create needs a template', array(
    'template' => 'main_template.html',
    )); 
?>

your main_template.html template would contain all the fields you require.

Then, with perch_content_custom you can use a different template to output any of the fields from main_template.html

Thank you Simon. I've used the code below and it works. But it seems a bit complex to me. Now I have to use three templates to get the custom output I need.

                    <?php 
                        perch_content_create('Regionname', array(
                            'template' => 'main_template.html',
                            'multiple'    => true,
                        )); 
                    ?>
                    <?php
                        perch_content_custom('Regionname', array(
                            'template' => 'template1.html',
                        ));
                    ?>
                    <?php
                        perch_content_custom('Regionname', array(
                            'template' => 'template2.html',
                        )); 
                    ?>                  
Simon Clay

Simon Clay 127 points

Ok, let's have a look :) So, it's a multiple item region. Could you show your templates?

Sure, I really appreciate your help. I use these templates with the output mentioned above:

main-template

                    <perch:before>
                    <!-- Start van de Bootstrap carousel -->
                    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <!-- Indicators -->
                        <ol class="carousel-indicators">
                        </perch:before>
                            <li data-target="#carousel-example-generic" data-slide-to="<perch:content id="perch_item_zero_index" type="hidden" />" <perch:if id="perch_item_index" match="eq" value="1">class="active"</perch:if>></li>
                        <perch:after>
                        </ol>
                        </perch:after>
                        <perch:before>
                        <!-- Wrapper for slides -->
                        <div class="carousel-inner" role="listbox">
                        </perch:before>
                            <div class="item<perch:if id="perch_item_index" match="eq" value="1"> active</perch:if>">
                                <img src="<perch:content id="slide" type="image" label="Foto" width="720px" height="350px" crop="true" />" alt="<perch:content id="logoalt" type="text" label="Omschrijving van de foto" required="true" help="Vul deze tekst altijd in. Het helpt voor een betere positie in Google." />">
                                <div class="carousel-caption">
                                    <perch:content id="logoalt" type="text" />
                                </div>
                            </div>
                            <perch:after>
                        </div>
                        <!-- Controls -->
                        <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>
                        </div>
                    <!-- Einde van de Bootstrap carousel -->  
                    </perch:after>

template1

                    <perch:before>
                    <!-- Start van de Bootstrap carousel -->
                    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <!-- Indicators -->
                        <ol class="carousel-indicators">
                        </perch:before>
                            <li data-target="#carousel-example-generic" data-slide-to="<perch:content id="perch_item_zero_index" type="hidden" />" <perch:if id="perch_item_index" match="eq" value="1">class="active"</perch:if>></li>
                        <perch:after>
                        </ol>
                        </perch:after>

template2

                        <perch:before>
                        <!-- Wrapper for slides -->
                        <div class="carousel-inner" role="listbox">
                        </perch:before>
                            <div class="item<perch:if id="perch_item_index" match="eq" value="1"> active</perch:if>">
                                <img src="<perch:content id="slide" type="image" label="Foto" width="720px" height="350px" crop="true" />" alt="<perch:content id="logoalt" type="text" label="Omschrijving van de foto" required="true" help="Vul deze tekst altijd in. Het helpt voor een betere positie in Google." />">
                                <div class="carousel-caption">
                                    <perch:content id="logoalt" type="text" />
                                </div>
                            </div>
                            <perch:after>
                            ...
                        </div>
                        <!-- Controls -->
                        <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>
                        </div>
                    <!-- Einde van de Bootstrap carousel -->  
                    </perch:after>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is your region now appearing in the control panel? Have you added some content?

Yes, it is appearing and I can add content.

Simon Clay

Simon Clay 127 points

I will have a look at the templates as soon as pos, to see if there's a better way to do it. It's the <ol> Indicators that make it slightly tricky.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so where does the problem lie? The two perch_content_custom() calls should output the region.

Simon Clay

Simon Clay 127 points

They do, but Edwin was wondering if there was a way to do it with less templates.

Yep, that's right. When I use perch_content_create I have to assign a main template to it, otherwise it doesn't create a region. To get the output I want I need to use two templates, which are in fact the same as the main template only cut in two. So I need a total of three templates to get the correct output and I think that it can be done with two.

I thought it would be possible to create a region using perch_content_create and the assign two templates to that region in the following way:

<?php perch_content_create('Regionname'); ?>
<?php
   perch_content_custom('Regionname', array(
   'template' => 'template1.html',
   ));
?>
<?php
   perch_content_custom('Regionname', array(
   'template' => 'template2.html',
   )); 
?>

But I have to use this method to get what I want

 <?php 
  perch_content_create('Regionname', array(
  'template' => 'main_template.html',
  'multiple' => true,
  )); 
 ?>
<?php
  perch_content_custom('Regionname', array(
  'template' => 'template1.html',
  ));
?>
<?php
  perch_content_custom('Regionname', array(
  'template' => 'template2.html',
  )); 
?> 
Drew McLellan

Drew McLellan 2638 points
Perch Support

The template that is assigned to the region becomes the master template for editing. As such, that needs to include all of the fields you want to add content for.

In this case, I would make main_template.html just a shell that includes the other two.

<perch:template path="content/template1.html" />
<perch:template path="content/template2.html" />

That would avoid unnecessary repetition.

Simon Clay

Simon Clay 127 points

I agree with Drew :) that's the most efficient way I can see.

On the other points raised here Drew, should we be able to create a region without needing to specify a template (we might want the client to be able to choose it from the list)?

<?php perch_content_create('Regionname'); ?> <!-- doesn't create a region -->
<?php perch_content_custom('Regionname'); ?> <!-- does create a region, surprisingly -->

Thanks guys, this looks like a nice solution. I'll clean up my code and will share the solution on this forum, so other newbie-users can benefit from it.

@Simon: Isn’t what you’re suggesting what the standard perch_content() function does? It adds the region, but lets the editor choose the template?