Forum

Thread tagged as: Question, Problem

Perch page attribute setting shows up in details but can't use it in a template

I placed

<perch:template path="pageoptions" />

In my default.php file and created my "pageoptions.html" file in "templates/pages/attributes" directory. The options show up in the details tab. I want to use those options on the navigation of the site (one being an image) but it won't output.

My pageoptions.html:

<perch:pages id="sidebar_img" label="Navigation Background" type="image"  />

I have a content region in a template being called using (passing values from blocks to the sidebar)

perch_content_custom('Sections',[
    'template'  =>  'sidebar.html'
  ]);

In sidebar.html I'm request the page attribute via:

<img src="<perch:pages id="sidebar_img" type="image"  />" />

According to https://docs.grabaperch.com/video/v/page-attributes/ this should work but it doesn't return anything. (I made sure the image is provided in the page details)

David Powell

David Powell 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_content_custom() won't render page attributes unless you explicitly pass them in. perch_pages_navigation() will, however.

Is it possible to pass them over via the data attribute:

perch_content_custom('name',[
data: [
  'sidebarImag' => ::perch magic goes here::
]
]);

Answer:

home.php:

<?php
  //store value of the attribute from the attribute template
  $navBg = perch_page_attribute('sidebar_img',array(
    'template'  =>  'pageoptions'
  ),true);

  //Pass data from the sections content to the sidebar.html template and hand off the stored var to the sidebar.html template
  perch_content_custom('Sections',[
    'template'  =>  'sidebar.html',
    'data' =>  [
      'sidebarimg' =>  $navBg
    ]
  ]);
?>

sidebar.html:

...
//display the passed over var. Note: the mix of quotes. Doing it any other way wouldn't out put the value inside of url()
<nav class="hidden" style='background-image:url("<perch:content id="sidebarimg" />")'>
...