Forum
I want to pass a value from an id in a collection as a layout variable
I have a collection 'Pages en' which contains an id that I want to use to set different backgrounds for different pages. In my collection template I have
<perch:content id="body_class" type="text" label="Page Class" help="This applies a class to the page to help style the background," suppress="true" />
I want to pass this body_class into my global/top layout. This is what I have in my page layout
// Get the body_class from the collection
$bodyClass = perch_collection('Pages en',[
'template'=>'body_class.html',
'filter' => 'slug',
'match' => 'eq',
'value' => perch_get('body_class'),
'count' => '1'
]);
// pages_en
perch_layout('global/top', array(
'language'=>'en',
'title'=>'Conil Pages',
'body-class'=>$bodyClass,
));
This is body_class.html
<perch:content id="body_class" type="text" />
The id body_class is showing up when I do <perch:showall/> on the page,but I can't pass the value of the id into the page layout variable. I have also tried
$bodyClass = perch_collection('Pages en',[
'skip-template'=>true,
'raw'=>true
'filter' => 'slug',
'match' => 'eq',
'value' => perch_get('body_class'),
'count' => '1'
]);
Any pointers appreciated, thanks.
When you say "I can't pass the value of the id into the page layout variable" what do you mean? Can you show me the code that isn't working?
Andy. In your 4th code example your using perch_get() which would be expecting this variable to be on the query string. Are you passing this as part of the url?
Sorry, I'm not being clear. In runway I want to allow an editor to select a theme e.g., 'dark' or 'light' from within a collection template. Then I want to assign that value to a layout variable in the master page. This should then add the 'dark' or 'light' class to the the body tag for styling. So I am trying to filter the collection by the slug and then match the value of theme.
You need to return the value into the variable:
Thanks Drew, sorry for taking so long to reply, tried that but still not working? The master page now has this
Including <perch:showall/> on the template adds this to the page:
and the debug message (repeated twice?) is
Thanks
You're using:
but you don't have an argument called 'theme' on the URL. You do have
s
which in this case is equal totrafalgarsurf
- is that what you mean to filter by?I think it's clear this has me totally baffled.
What I want to do is to allow fields that are set in the collections template to be used as layout variables when displaying a page from that collection.
What would be the best approach to achieve this?
That's what you have - I just don't understand what you're trying to filter by.