Forum

Thread tagged as: Question, Runway

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.

Andy Clark

Andy Clark 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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 clark said:

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.

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?

Drew McLellan said:

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?

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.


// Get the theme from the collection $theme = perch_collection('English Pages', [ 'filter' => 'slug', 'match' => 'eq', 'value' => perch_get('theme'), ]); // use $theme to add the dark or light class to body perch_layout('global/top', array( 'language'=>'en', 'title'=>'Conil Pages', 'body-class'=>$theme, ));
Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to return the value into the variable:

  $theme = perch_collection('English Pages', [
        'filter' => 'slug',
        'match'  => 'eq',
        'value'  => perch_get('theme'),
    ], true);

Thanks Drew, sorry for taking so long to reply, tried that but still not working? The master page now has this

  // Get the theme from the collection
  $theme = perch_collection('Conil English Pages', [
        'filter'     => 'slug',
        'match'  => 'eq',
        'value'   => perch_get('theme'),
    ], true);

  // use $theme to add the dark or light class to body
  perch_layout('global/top', array(
    'language'    => 'en',
    'title'             => 'Conil Pages',
    'body-class' => $theme,
  ));

Including <perch:showall/> on the template adds this to the page:


ID Value url_0 /en/pages/trafalgarsurf url_s trafalgarsurf url_1 trafalgarsurf perch_page_path /en/pages/page _id 199 title trafalgarsurf heading Trafalgar Surf theme dark etc etc

and the debug message (repeated twice?) is

Debug Message
SELECT * FROM perch2_pages WHERE pagePath='/perch/core/runway/start.php' LIMIT 1
SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
Matched route: en/pages/[slug:s]
Using master page: /templates/pages/en/conil_pages/conil_page_en.php
Page arguments:
Array
(
    [0] => /en/pages/trafalgarsurf
    [s] => trafalgarsurf
    [1] => trafalgarsurf
)
SELECT collectionID, collectionTemplate FROM perch2_collections WHERE collectionKey='Conil English Pages'
SELECT * FROM ( SELECT idx.itemID, ci.collectionID, ci.itemJSON, idx2.indexValue as sortval FROM perch2_collection_index idx JOIN perch2_collection_items ci ON idx.itemID=ci.itemID AND idx.itemRev=ci.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_revisions cr ON idx.itemID=cr.itemID AND idx.itemRev=cr.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE (idx.collectionID=29) AND ((idx.indexKey='slug' AND idx.indexValue='')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/collections/pages_coll_en.html
Using sub-template: /templates/content/blocks/contact_details.html
Using sub-template: /templates/content/blocks/cta.html
Using sub-template: /templates/content/blocks/lede.html
Using sub-template: /templates/content/blocks/resp_img_block.html
Using sub-template: /templates/content/blocks/modal_text.html
Using sub-template: /templates/content/blocks/modal_video.html
SELECT * FROM perch2_pages WHERE pagePath='/en/pages/page' LIMIT 1
Using template: /templates/pages/attributes/seo.html
Using template: /templates/search/search-form.html
SELECT * FROM ( SELECT idx.itemID, ci.collectionID, ci.itemJSON, idx2.indexValue as sortval FROM perch2_collection_index idx JOIN perch2_collection_items ci ON idx.itemID=ci.itemID AND idx.itemRev=ci.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_revisions cr ON idx.itemID=cr.itemID AND idx.itemRev=cr.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE (idx.collectionID=29) AND ((idx.indexKey='slug' AND idx.indexValue='trafalgarsurf')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/conil_pages/title.html
SELECT * FROM ( SELECT idx.itemID, ci.collectionID, ci.itemJSON, idx2.indexValue as sortval FROM perch2_collection_index idx JOIN perch2_collection_items ci ON idx.itemID=ci.itemID AND idx.itemRev=ci.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_revisions cr ON idx.itemID=cr.itemID AND idx.itemRev=cr.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE (idx.collectionID=29) AND ((idx.indexKey='slug' AND idx.indexValue='trafalgarsurf')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/conil_pages/pages_coll_en.html
Using sub-template: /templates/content/blocks/contact_details.html
Using sub-template: /templates/content/blocks/cta.html
Using sub-template: /templates/content/blocks/lede.html
Using sub-template: /templates/content/blocks/resp_img_block.html
Using sub-template: /templates/content/blocks/modal_text.html
Using sub-template: /templates/content/blocks/modal_video.html
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/en/pages/page' OR regionPage='*' ORDER BY regionPage DESC
Debug Message
SELECT * FROM perch2_pages WHERE pagePath='/perch/core/runway/start.php' LIMIT 1
SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC
Matched route: en/pages/[slug:s]
Using master page: /templates/pages/en/conil_pages/conil_page_en.php
Page arguments:
Array
(
    [0] => /en/pages/trafalgarsurf
    [s] => trafalgarsurf
    [1] => trafalgarsurf
)
SELECT collectionID, collectionTemplate FROM perch2_collections WHERE collectionKey='Conil English Pages'
SELECT * FROM ( SELECT idx.itemID, ci.collectionID, ci.itemJSON, idx2.indexValue as sortval FROM perch2_collection_index idx JOIN perch2_collection_items ci ON idx.itemID=ci.itemID AND idx.itemRev=ci.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_revisions cr ON idx.itemID=cr.itemID AND idx.itemRev=cr.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE (idx.collectionID=29) AND ((idx.indexKey='slug' AND idx.indexValue='')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/collections/pages_coll_en.html
Using sub-template: /templates/content/blocks/contact_details.html
Using sub-template: /templates/content/blocks/cta.html
Using sub-template: /templates/content/blocks/lede.html
Using sub-template: /templates/content/blocks/resp_img_block.html
Using sub-template: /templates/content/blocks/modal_text.html
Using sub-template: /templates/content/blocks/modal_video.html
SELECT * FROM perch2_pages WHERE pagePath='/en/pages/page' LIMIT 1
Using template: /templates/pages/attributes/seo.html
Using template: /templates/search/search-form.html
SELECT * FROM ( SELECT idx.itemID, ci.collectionID, ci.itemJSON, idx2.indexValue as sortval FROM perch2_collection_index idx JOIN perch2_collection_items ci ON idx.itemID=ci.itemID AND idx.itemRev=ci.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_revisions cr ON idx.itemID=cr.itemID AND idx.itemRev=cr.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE (idx.collectionID=29) AND ((idx.indexKey='slug' AND idx.indexValue='trafalgarsurf')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/conil_pages/title.html
SELECT * FROM ( SELECT idx.itemID, ci.collectionID, ci.itemJSON, idx2.indexValue as sortval FROM perch2_collection_index idx JOIN perch2_collection_items ci ON idx.itemID=ci.itemID AND idx.itemRev=ci.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_revisions cr ON idx.itemID=cr.itemID AND idx.itemRev=cr.itemRev AND idx.collectionID=ci.collectionID JOIN perch2_collection_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE (idx.collectionID=29) AND ((idx.indexKey='slug' AND idx.indexValue='trafalgarsurf')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID ORDER BY sortval ASC
Using template: /templates/content/conil_pages/pages_coll_en.html
Using sub-template: /templates/content/blocks/contact_details.html
Using sub-template: /templates/content/blocks/cta.html
Using sub-template: /templates/content/blocks/lede.html
Using sub-template: /templates/content/blocks/resp_img_block.html
Using sub-template: /templates/content/blocks/modal_text.html
Using sub-template: /templates/content/blocks/modal_video.html
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/en/pages/page' OR regionPage='*' ORDER BY regionPage DESC
Time: 0.0781
Memory: 5.2323

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

You're using:

perch_get('theme')

but you don't have an argument called 'theme' on the URL. You do have s which in this case is equal to trafalgarsurf - 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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's what you have - I just don't understand what you're trying to filter by.