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, Problem, Configuration

Can a Master Page template itself contain links to automatically pull in content...

Hi all,

Okay, so, assistance in a previous thread of mine means that I now have a working Master Page... Its actual contents is currently a thread-bear minimum and I wish to add more content to it, but not directly within in.

For example, if I knew that the footer of ALL my website pages was going to be identical, then I would have an urge to have the separate footer HTML as being in its own "content" HTML template. And then, my Master Page template would have some sorta Perch Content PHP code to go fetch that footer HTML and add it into the Master Page and thus also add it into all the actual pages using that Master Page.

I'm not after that footer content being actually editable by the Admin user creating a new page... I just want footer's HTML being in a separate template so that if (in a month's time) I need to amend the footer, I'm only amending it in one single location.

m.

Martin Lowe

Martin Lowe 0 points

  • 2 years ago

Maybe I'm supposed to be using Perch Layouts?

If a "layout" can be pulled into a page when being called within a template, does that include a Master Template too?

m.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Martin,

Yes, Layouts is a good approach for what you are trying to do.

You can use Layouts in your master pages: https://docs.grabaperch.com/functions/layouts/

perch_layout('global.footer'); 

Yes, Layouts is a good approach for what you are trying to do.

Thanks... I am now building up my "layout" elements and adding them into my pre-existing Master File.

My next question (following on) is this:

Can I have a "layout" file that within it, contains an element that is Admin editable (for individual pages).

Let's say, for example, I have a layout element that was purely just a pair of H2 tags and the header's text in between them:

 <h2>This section is on Education</h2>

Now, I want the actual text "This section is on Education" to be editable within Perch Admin, for any/all of the Perch pages that use that specific layout element.

Remember, the layout isn't just directly within a PHP page, the layout is also referenced within a Master Page template.

I currently have the below inside of my layout template:

 <h2 class="text-white"><perch:content ID="pageTitle" type="smarttext" label="pageTitle" help="This is the text title that sits on top of the BIG header photo" required="true" /></h2>

...But the page(s) that run off of the Master Page, which references that layout, aren't giving me an option to place content within the H2 tags. I'm literally getting the above Perch Content tag itself added to the end-result page.

m.

So, now I'm reading from here - https://docs.grabaperch.com/perch/pages-and-nav/layouts/variables/ and I can see that I can add this into my actual layout file:

 <h2 class="text-white"><?php perch_layout_var('pageTitle'); ?></h2>

...and equally, this inside my Master Page:

<?php perch_layout('pmsa.template01.header', array(
    'pageTitle'=>'cheese',
)); ?>

...and my actual front-end page that's been made from that Master Page will display the title "cheese". Problem is, with "cheese" being hard-coded into the array, then EVERY front-end page using that Master Page template will also have the H2 tag title of "cheese".

So, how do I make that "pageTitle" become an editable element of the page(s) coming off of the Master Page?

m.

Equally I see that this page - https://docs.grabaperch.com/perch/pages-and-nav/layouts/layout-variables/ - suggests that you can get the variable's value "from elsewhere" and shows this code as an example:

<?php
    perch_layout('global.header', array(
        'title'=>perch_pages_title(true),
    ));
?>

...which is fine and dandy if your variable wants to take the value of the page's title. But, again, I want the page creator to have the ability to type in a bespoke piece of text for each and every page that uses the Master Page > that uses the Layout.

Can I put:

<?php
    perch_layout('global.header', array(
        'title'=>perch_content('pageTitleContent'),
    ));
?>

Will that work?

m.

Answer = almost, but not quite. It does work, in the sense that you get to Admin add content to the new region. But the content does not actually display in the correct location of the front-end page. Instead it just sticks it in the first available spot of the Layout file's content... as opposed to specifically applying it to inside the pair of H2 tags.

Hmmmm.... trying this too:

        <?php $pageTitle = perch_content('pageTitleContent');
            perch_layout('pmsa.template01.header', array(
            'pageTitle'=>$pageTitle
        )); ?>

...based upon the blog post title example, also on https://docs.grabaperch.com/perch/pages-and-nav/layouts/layout-variables/ but this too seems to just dump the content at the very top of the Layout's contents, as opposed to between the H2 tags.

To add to my confusion, I'm also hoping that wherever/however it finally turns out to be, that I'm giving the page's creator the ability to amend that text inside the H2 tags... that I'm able to pre-define the type of template that content uses.

What I mean is that (obviously) all I require between the H2 tags is text, no markup at all. So we'd be using the "text" template, rather than the "text block" template. And I'd like it that the user doesn't need to actually choose a template type, instead they're just automatically given a "text" template to fill in.

I believe code like this:

 <perch:content id="title" type="text" label="Product title" required title>

...has that ability, since its type is defined as "text". Can I use code like this somewhere within what I'm trying to achieve?

But then I also read about - https://docs.grabaperch.com/functions/content/perch-content-custom/ - which seems to be a fancy version of :

 <?php perch_content('pageTitleContent'); ?>

...with the ability to predefine a specific template.

Basically, I want Admin editable (at a per page-level) content, within a Layout Template, that's itself within a Master Page template.

So that this H2 header of mine is held inside the Layout Template, which is inside the Master Page template, but each page made from that Master Page template has individual, user editable, text inside those H2 tags.

m.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Now, I want the actual text "This section is on Education" to be editable within Perch Admin, for any/all of the Perch pages that use that specific layout element.

You can add an editable content region to the Layout with perch_content(). Each page will have an editable region with same name.

So:

perch_layout('global.header');

And then inside layouts/global.header.php:

<h2><?php perch_content('Heading'); ?></h2>

And when you visit the page in the browser, Perch will register the content region and you will be able to enter the content in the control panel.


Layout variable solves a solution that you may not have come across yet.

Layouts are great because you can have something like global header layout that you use on all pages. But in some pages, you may want to have things set up slightly differently.

For example, let's say you have an e-commerce site that displays a list of product categories as the main site navigation menu. This navigation menu is displayed on every page. However, you want to hide it in the checkout pages so the user can focus on completing the payment.

On all pages you would have:

perch_layout('global.header');

On checkout pages you would add a variable to change the behaviour inside the layout:

perch_layout('global.header', [
'hide_menu' => true
]);

So in your case, do you want the editable region to be on all Master Pages that uses the layout? If so, you can add perch_content() directly to the layout.

If not, you can add it in the Master Page and then pass it as a layout variable. You need to return the value of the region See: https://docs.grabaperch.com/functions/content/perch-content/#return

// return the value of the content region:
$pageTitle = perch_content('pageTitleContent', true);

// pass it as a layout variable
perch_layout('pmsa.template01.header', [
'pageTitle' => $pageTitle
]);

Thanks for the reply!

With your guidance I have been able to get:

 <h2 class="text-white"><?php perch_content('Heading'); ?></h2>

...working within my Layout and appearing on the page. Yay!

But if I wanted more control over which template is used (i.e. templates > content > text.html), is there an obvious reason why the below fails to work?

<h2 class="text-white">
<?php perch_content_create('pageHeading', array(
'template' => 'text.html'
));
?>
</h2>

It works in principle, that Admin shows the region within the page and I can fill that region with the text I want... But once saving the changes, the page's front-end does not display the H2 tag content. The H2s are blank.

m.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

perch_content_create() creates the region, but it doesn't output the content: https://docs.grabaperch.com/functions/content/perch-content-create/

You need to use perch_content() or perch_content_custom() to output it.

Thanks again... but doesn't seem to be working.

I have now replaced:

 <h2 class="text-white"><?php perch_content('Heading'); ?></h2>

...in my Layout file, with the below instead.

<h2 class="text-white">
<?php perch_content_custom('anotherTest', array(
'template' => 'text.html'
));
?>
</h2>

I can't seem to get the "anotherTest" region to appear in the relevant page within Admin.

So, perch_content_create() does give me a region inside admin, perch_content_custom() doesn't.

Is there any way I can use:

 <perch:content type="text" id="heading" label="Heading" help="Give the article a descriptive title">

...within a Layout file? I'm guessing not directly.

m.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

  • perch_content_create() only creates a region.
  • perch_content() creates a region and outputs its (cached) content
  • perch_content_custom() only outputs the content of a region at runtime (not cached)

So if you want to have the template pre-selected for the region, you are right in using perch_content_create(). But to output the content you also need to use either perch_content() or perch_content_custom():

// create the region first
perch_content_create('pageHeading', array(
'template' => 'text.html'
));

// output the region
perch_content('pageHeading');

Oh!!! It needs two stages... Creation and then output!

Working now! Many Thanks Hussein.

p.s. Hussein, do you do freelance work? If i wanted some proper paid assistance on a Perch/Runway website?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Great!

p.s. Hussein, do you do freelance work? If i wanted some proper paid assistance on a Perch/Runway website?

Yes, I do. I'm also listed as a Registered Developer: https://grabaperch.com/developers

Feel free to contact me on contact@hussein-alhammad.com

Hussein, email sent