Forum

Thread tagged as: Question

Including a shared region in a template

I’m putting together a site for a Parish Council and I have a Shared Region for the Parish Clerk so that if the Clerk changes then the details only need changing in a single place.

I have a template for contacts, is there a way to pull the Shared Region into a template so that I can always include the Parish Clerk details?

Here is the shared region template:

<div class="vcard">
    <perch:if exists="fn">
        <p class="fn"><perch:content type="smarttext" id="fn" label="Full Name" /></p>
    </perch:if>
    <perch:if exists="org">
        <p class="org"><perch:content type="smarttext" id="org" label="Organisation" /></p>
    </perch:if>
    <perch:if exists="role">
        <p class="role"><perch:content type="smarttext" id="role" label="Role" /></p>
    </perch:if>
    <dl>
        <perch:if exists="email">
            <dt>Email</dt>
            <dd><a href="mailto:<perch:content type="text" id="email" label="Email Address" />" class="email"><perch:content id="email" /></a></dd>
        </perch:if>
        <perch:if exists="tel">
            <dt>Telephone</dt>
            <dd class="tel"><perch:content type="text" id="tel" label="Telephone Number" /></dd>
        </perch:if>
        <perch:if exists="mobile">
            <dt>Mobile</dt>
            <dd class="mobile"><perch:content type="text" id="mobile" label="Mobile Number" /></dd>
        </perch:if>
        <perch:if exists="address">
            <dt>Address</dt>
            <dd class="address"><perch:content type="text" id="address" label="Full Address (including post code)" /></dd>
        </perch:if>
        <perch:if exists="url">
            <dt>Web site</dt>
            <dd><a href="https://<perch:content id="url" type="text" label="Website Address" replace="https://|" />" class="url"><perch:content type="smarttext" id="website" label="Website Name" /></a></dd>
        </perch:if>
    </dl>
</div>

and the template where I’d like to include the shared region:

<div class="contacts">
    <h2>Contacts</h2>
    <perch:repeater id="contact" label="Contact">
        <perch:template path="content/contact.html" />
    </perch:repeater>
</div>

Ideally I’d like to include the Shared contact before the repeater, is there a way to do that, or would I need to do something at the page template level?

Nick Bramwell

Nick Bramwell 5 points

  • 4 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

I think you would have to do that at page level - get your shared region content put pass it into a variable (using the true parameter in the function). Using PerchSystem::set_var you can pass the variable into your second template (but you would have to use perch_content_custom when calling the second template).

Simon Clay

Simon Clay 127 points

Hi Nick, your question gave me an idea, and on testing it seems to work. We can use Perch Layouts to add in a Perch Region to your template...

Firstly add the layout include in your template <perch:layout path="php_code" /> e.g.

<div class="contacts">
<h2>Contacts</h2>
<perch:layout path="php_code" />
<perch:repeater id="contact" label="Contact">
<perch:template path="content/contact.html" />
</perch:repeater> </div>

Then in perch/templates/layouts add a file called php_code.php containing your shared region e.g.

<?php perch_content('Contacts'); ?>
Duncan Revell

Duncan Revell 78 points
Registered Developer

Simon - that's genius.

Simon Clay

Simon Clay 127 points

:D It's quite exciting, now I come to think of it. It enables us to have any php in our templates. Woot!

Duncan Revell

Duncan Revell 78 points
Registered Developer

I know what you mean - my brain is having some "oh, hang on..." moments!

Waits for Drew to smother this with caution...

Drew McLellan

Drew McLellan 2638 points
Perch Support

Be cautious!

Actually, that's what it's for.

Simon Clay

Simon Clay 127 points

Yay \o/

That is a great solution Simon, thanks

Hey, I'm not looking to steal any credit here but just the other day I mentioned using layouts to solve a similar problem... but I didn't get all the high fives Sly is getting... :(

BTW Simon, good job (high five)

Simon Clay

Simon Clay 127 points

High five back at you Robert.

I will enjoy the moment, but will soon, no doubt, be back asking my usual silly questions in the forum. :)