Forum

Thread tagged as: Question, Problem

navigation inside content template

I am trying to include navigation inside a hero banner template file. I have the following setup:

Files system

cms/
--templates/
----content/
------hero.html
----navigation/
------menu.html
----pages/
------home.php

And these are the template files:

hero-banner.html

<div class="c-hero-banner">
        <nav class="c-primary-nav">
            <perch:template path="navigation/menu.html">
        </nav>
    </div>
...

home.php

....
<?php perch_content_create('Hero Banner', array(
    'template' => 'hero-banner.html',
    ));
?>  

<?php perch_pages_navigation(array(
  'navgroup' =>'header',
  'template' => 'menu.html',
  'levels' => 1
), true); ?>    

<?php perch_content_custom('Hero Banner'); ?>
....

When I run it, all I get is the navigation menu outside the hero banner and an empty menu inside. Making perch_pages_navigation(...) a variable doesn't work, it just removes the menu outside the hero banner.

Is there a way to make perch:template inside hero-banner.html use the perch_pages_navigation()?

Neal Hanson

Neal Hanson 0 points

  • 2 years ago

Neal, in your code this isn't going to output anything because you have not assigned the output to a variable

<?php perch_pages_navigation(array(
'navgroup' =>'header',
'template' => 'menu.html',
'levels' => 1
), true); ?>

You would need to do something like this

<?php
$navigation = perch_pages_navigation(array(
'navgroup' =>'header',
'template' => 'menu.html',
'levels' => 1
), true); ?> 

Now you would need to echo the variable $navigation or pass it into your template...

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Neal,

You're almost there.

First, like Robert mentioned, you should assign a variable. Then you need to pass the variable to the template:

// return navigation and assign it to a PHP variable
$navigation = perch_pages_navigation(array(
'navgroup' =>'header',
'template' => 'menu.html',
'levels' => 1
), true);

// pass the variable to the template engine
PerchSystem::set_var('navigation', $navigation);

Then in your content template, you can output it like so:

<perch:content id="navigation" type="hidden" html>

If you're using an older version of Perch:

<perch:content id="navigation" type="hidden" html="true" />

Thanks. That works great!