We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question

Best way to reuse content?

Hi

I have a page called Global Details, I use this to collect information I intend to use around the website, things like postal address, company number, social media URLs etc.

I was wondering, what's the recommended way to access this information to output in on the page?

Using perch content custom?

<?php 
    perch_content_custom('Global Details', array(
        'template' => '_global/address-line-2.html',
        'page'=>'/global-details.php'
    )); 
?>

Or using skip-template with perch content custom?

<?php 

$html = perch_content_custom('Global Details', array(
'template' => '_global/address-line-2.html',
'page'=>'/global-details.php',
'skip-template' => true,
'return-html' => true,
));

echo $html['html'];
?>

I've created a demo page with config/debug turned on and tested each way. The Request time and Process time (is very fast) don't appear to be much different. The values change each time the page is refreshed.

Is there a preferred 'Perch way' of doing this?

Perch Runway: 3.0.10

Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago

Shared region. Then you can use anywhere with perch_content() or perch_content_custom().

Sorry, Runway:

Collection, but then perch_collection() with template and filter options.

Drew McLellan

Drew McLellan 2638 points
Perch Support

There's no real difference between the two examples you gave. You'd do the latter if you wanted the skip-template output for some other purpose. Not sure why you'd do that if you didn't.

Generally not a good idea to use a shared region for anything large.

Since this is Runway, he should just use a collection shouldn’t he? As collections are global and then using a template to output global in format required for that page...??

Rachel Andrew

Rachel Andrew 394 points
Perch Support

That would entirely depend on the content. If I have something that makes sense to exist as part of a page I'd have it as content. If was was just a collection of items to use around the site I'd use a Collection.

I definitely wouldn't use a Shared Region however. Those are only for simple bits of content such as your contact address that you want to display on several pages using the same template.

Hi all

Thanks for the replies.

It's good to know to stay away from shared regions for anything more complicated than a simple address etc.

I've created a 'Global details' page and I'm using perch_content_custom to pull out specific content as I need it.

<?php 
    perch_content_custom('Global Details', array(
        'template' => '_global/address-line-1.html',
        'page'=>'/global-details'
    )); 
?>

A collection doesn't feel like a good fit for this type particular type of content. Collections are great for templating repeating content, something like an address book or a catalogue of products. For this website there will only ever be one 'company name' or `phone number'.

Thanks again - great support!

Hi Stephen,

I am trying to do something similar but just in the standard Perch, not Runway. I was hoping you could confirm something for me. I am assuming you don't actually every display your Global Details page? It is just used to store your global content values and then you use different templates to display the content in different areas of the website?

Also, would you mind maybe attaching a copy of your Global Details page so I can see how you have is structured?

Thanks

James

Hi James,

I am assuming you don't actually every display your Global Details page?

That's right

It is just used to store your global content values and then you use different templates to display the content in different areas of the website?

Yup.

Here's part of my global template, you can add other perch tags to collect any extra information.

<!--* Company name *-->
<perch:if exists="company_name"><perch:content id="company_name" type="text" label="Company Name" html="false"  /></perch:if>
<!--* Address line 1 *-->
<perch:if exists="address_1"><perch:content divider-before="Address" id="address_1" type="text" label="Address Line 1" html="false"  /></perch:if>
<!--* Address line 2 *-->
<perch:if exists="address_2"><perch:content id="address_2" type="text" label="Address Line 2" html="false"  /></perch:if>
<!--* City *-->
<perch:if exists="city"><perch:content id="city" type="text" label="City" html="false"  /></perch:if>
<!--* Postcode *-->
<perch:if exists="postcode"><perch:content id="postcode" type="text" label="Postcode" html="false"  /></perch:if>

Say I wanted to use the ID company_name I'd just create a new HTML template (company-name.html), with just part of the global template

<perch:content id="company_name" type="text" label="Company Name" html="false"  />

I'd then use this PHP to use that template.

<?php 
perch_content_custom('Global Details', array(
'template' => 'company-name.html',
'page'=>'/global-details'
)); 
?>

Now when I update the company name in the Perch CMS the new name will be displayed on the page.

Does that help?

Hi Stephen

That is a fantastic answer - it helps heaps. Just one more question (I am fairly new to Perch). My understanding of Perch is that you need to visit the page in the browser first before the fields become available for editing in the Perch Dashboard. So, in the instance of your Global Details page, I presume you have a page called something like global-details.php which in turn is associated with the global-details.html template.

To have the template fields available in the Dashboard, did you have to visit the global-details page in your website? If so, how do you ensure visitors don't somehow stumble across this page in the future - I admit that would be extremely unlikely, but I wanted to check whether you had a strategy to serve up a 404 page or something similar. Or, is there another way to make the global-template fields accessible in the Dashboard without having to first visit the global details page in the browser?

Thanks heaps for your help.

James