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',
));
?>
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 theperch_content_create
function.Just in case you need my diagnostics code:
If you add debug to the page, what does it output?
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:
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:
This is the page debug-output:
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.
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:
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.
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
template1
template2
Is your region now appearing in the control panel? Have you added some content?
Yes, it is appearing and I can add content.
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.
Ok, so where does the problem lie? The two
perch_content_custom()
calls should output the region.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:
But I have to use this method to get what I want
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.That would avoid unnecessary repetition.
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)?
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?