Forum

Thread tagged as: Runway

Dynamic Template based on Current Page?

I'm managing a grouping of pages in one Collection called "Customer". This allows me to create a groups of pages for different customer types; universities, corporates etc.

We are using different templates to output the right content. What I'm seeking to do is apply a different template based on the current URL. I'm almost there; I'm just wondering how to correctly inject the template variable into the Collection function based on the fields it is fetching.

I did try repeating the Collection function code after the IF statements to output the correct template. The template does get rendered but places it above the the header and footer.

What is the correct way to output the template in the right place?

Collection Function placed after IF Statement

  $post = perch_collection('Customer', [
    'template'      => 'customer/'.$template,
  ]);

Full Code

 # get the collection
  $post = perch_collection('Customer', [
    'filter'        => 'slug',
    'match'         => 'eq',
    'value'         => perch_get('s'),
    'template'      => 'customer/'.$template,
    'skip-template' => 'true',
    'return-html'   => 'true',
  ]);

  # set up the variables
  $title       = $post['0']['title'];
  $slug        = $post['0']['slug'];
  $description = $post['0']['desc'];
  $keywords    = $post['0']['keywords'];
  $noindex     = $post['0']['noindex'];
  $nofollow    = $post['0']['nofollow'];
  $nosnippet   = $post['0']['nosnippet'];

  # set up the sub page variables
  $page_title_01 = $post['0']['page_title_01'];
  $page_title_02 = $post['0']['page_title_02'];

  $page_slug_01 = $post['0']['page_slug_01'];
  $page_slug_02 = $post['0']['page_slug_02'];

  $page_img_01 = $post['0']['page_img_01'];
  $page_img_02 = $post['0']['page_img_02'];

  $page_body_01 = $post['0']['page_body_01'];
  $page_body_02 = $post['0']['page_body_02'];

  # Define the page template, title, active check and social image 
  # using the collection content by checking and matching the current URL
  if ($url == $domain.'/work-with-us/'.$slug.'/'.$page_slug_01) {
    $template = 'page_'.$page_slug_01.'.html';
    $titlePage = $page_title_01;
    $active = $page_slug_01;
    $socialImage = $page_img_01;
  } elseif ($url == $domain.'/work-with-us/'.$slug.'/'.$page_slug_02) {
    $template = 'page_'.$page_slug_02.'.html';
    $titlePage = $page_title_02;
    $active = $page_slug_02;
    $socialImage = $page_img_02;
  }


  PerchSystem::set_var('active', $active);


  # use the variables in the array value
  perch_page_attributes_extend(array(
      'description'    => $description,
      'keywords'       => $keywords,
      'noindex'        => $noindex,
      'nofollow'       => $nofollow,
      'nosnippet'      => $nosnippet,
      'og_description' => $description,
      'og_title'       => $title.' - '.$titlePage,
      'og_type'        => 'website',
      'og_image'       => $socialImage,
      'sharing_image'  => $socialImage,
  ));

  PerchSystem::set_var('active', $active);


  # use the variables in the array value
  perch_page_attributes_extend(array(
      'description'    => $description,
      'keywords'       => $keywords,
      'noindex'        => $noindex,
      'nofollow'       => $nofollow,
      'nosnippet'      => $nosnippet,
      'og_description' => $description,
      'og_title'       => $title.' - '.$titlePage,
      'og_type'        => 'website',
      'og_image'       => $socialImage,
      'sharing_image'  => $socialImage,
  ));


    # Include the header. You can find this in tempates/layouts/global
    perch_layout('global/header', [
      'title' => $title.' - '.$titlePage,
      'body-class' => 'overview-page',
    ]);

    # display the page content
    echo $post['html'];

    # Include the footer. You can find this in tempates/layouts/global
    perch_layout('global/footer');
Dan Lee

Dan Lee 1 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

How are you populating $template ?

Dan Lee

Dan Lee 1 points

The template value is taken from a field called page_slug_01 in the collection:

$page_slug_01 = $post['0']['page_slug_01'];

And then assigned to $template

if ($url == $domain.'/work-with-us/'.$slug.'/'.$page_slug_01) {

$template = 'page_'.$page_slug_01.'.html';

}
Drew McLellan

Drew McLellan 2638 points
Perch Support

For that first example, just be sure to return the value:

$post = perch_collection('Customer', [
    'template'      => 'customer/'.$template,
  ], true);
Dan Lee

Dan Lee 1 points

Sorry Drew,

A little confused. Where is that code meant to go? I pasted it below the IF statement thinking it might go there but the template didn't render.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't know - where did it come from for your example?

Dan Lee

Dan Lee 1 points

The Collection function placed after IF Statement in my original message was an experiment to see if the template would render - it rendered in the wrong place - above the header and footer. You then provided code: the Collection function with a returned value - I placed that after the IF statement again (see code below). That didn't render anything.

The full code now looks like the following - using the code you provided, what am I doing wrong?

  # get the collection
  $post = perch_collection('Customer', [
    'filter'        => 'slug',
    'match'         => 'eq',
    'value'         => perch_get('s'),
    'template'      => 'customer/'.$template,
    'skip-template' => 'true',
    'return-html'   => 'true',
  ]);

  # set up the variables
  $title       = $post['0']['title'];
  $slug        = $post['0']['slug'];
  $description = $post['0']['desc'];
  $keywords    = $post['0']['keywords'];
  $noindex     = $post['0']['noindex'];
  $nofollow    = $post['0']['nofollow'];
  $nosnippet   = $post['0']['nosnippet'];

  # set up the sub page variables
  $page_title_01 = $post['0']['page_title_01'];
  $page_title_02 = $post['0']['page_title_02'];

  $page_slug_01 = $post['0']['page_slug_01'];
  $page_slug_02 = $post['0']['page_slug_02'];

  $page_img_01 = $post['0']['page_img_01'];
  $page_img_02 = $post['0']['page_img_02'];

  $page_body_01 = $post['0']['page_body_01'];
  $page_body_02 = $post['0']['page_body_02'];


  # Define the page template, title, active check and social image
  # using the collection content by checking and matching the current URL
  if ($url == $domain.'/work-with-us/'.$slug.'/'.$page_slug_01) {
    $template = 'page_'.$page_slug_01.'.html';
    $titlePage = $page_title_01;
    $active = $page_slug_01;
    $socialImage = $page_img_01;
  } elseif ($url == $domain.'/work-with-us/'.$slug.'/'.$page_slug_02) {
    $template = 'page_'.$page_slug_02.'.html';
    $titlePage = $page_title_02;
    $active = $page_slug_02;
    $socialImage = $page_img_02;
  } 


  PerchSystem::set_var('active', $active);

  $post = perch_collection('Customer', [
      'template'      => 'customer/'.$template,
    ], true);


  # use the variables in the array value
  perch_page_attributes_extend(array(
      'description'    => $description,
      'keywords'       => $keywords,
      'noindex'        => $noindex,
      'nofollow'       => $nofollow,
      'nosnippet'      => $nosnippet,
      'og_description' => $description,
      'og_title'       => $title.' - '.$titlePage,
      'og_type'        => 'website',
      'og_image'       => $socialImage,
      'sharing_image'  => $socialImage,
  ));


    # Include the header. You can find this in tempates/layouts/global
    perch_layout('global/header', [
      'title' => $title.' - '.$titlePage,
      'body-class' => 'overview-page',
    ]);

    # display the page content
    echo $post['html'];

    # Include the footer. You can find this in tempates/layouts/global
    perch_layout('global/footer');
Drew McLellan

Drew McLellan 2638 points
Perch Support

You're overwriting the $post variable. First it's a skip-template array, then you're overwriting it with a templated string.

Dan Lee

Dan Lee 1 points

Thanks Drew

I am now using two separate Collection Functions - one to extract the Collection data and pass it to the template logic and the second to display the template for the data.