Passing a variable to a layout template from within a content template...

  • Again, this may not be the best way to go about this but in any case it's working and I wanted to check it's supported behaviour:


    I have a global.header.php template with a couple of variables which I was calling in the page with


    PHP
    1. <?php perch_layout ('global.header', array(
    2.     'page' => 'about',
    3.     'background' => 'some-image.jpg',
    4.     )); ?>
    5.     

    but anticipating my users wanting to be able to edit a couple of things (the background image for instance) in that header on a page by page basis I thought I would try calling it in a template like this:


    Code
    1. <perch:layout path="global.header" page="courses" background="<perch:content id="header_image" type="image" required label="Header Image">">


    which works absolutely fine... I just wondered if that's a sensible way to do it and if there's any advantages / disadvantages to this approach vs a content template include.


    ...an to check it was supported behaviour as I said as I couldn't find it as an example usage in the docs.


    Owain

  • drewm

    Approved the thread.
  • I use layouts includes in templates occasionally. It's a quick way to "bring" PHP into a template. As you pointed out it's easy to pass editable content to a layout like this.


    on a page by page basis


    For customising things on the page level, you'd normally use Page Attributes which you can get in PHP in your global.header.php:


    PHP
    1. $header_image = perch_page_get_attribute('header_image');
  • When I first built a site for someone using perch pages weren't a thing and I've never used them as I usually build my own site structure then use perch as a "drop in" CMS...


    I should definitely explore the functionality more.


    Just to understand how this is working... the variable from the template is stored when the editor clicks "save"... but the "perch:layout" function runs at runtime when perch:content is called in the page?