How to auto-include autonav in a template

Permalink 10 users found helpful
Hi there,

Is there a way to auto-include a block in a page template with certain settings? I want to include the autonav block in ever page made with template X, but with only the 1 sub-level of links showing.

Is this possible? And yes, how? :)

The reason I'm asking is because we want the end user to be able to add an X amount of subpages to a certain section, but not having to insert the autonav block each and every time.

P.S., this is the first time our company is using Concrete5 to build a site and I've only been working with it for a few days now, so please excuse me if I ask stupid questions ;)

 
griebel replied on at Permalink Reply
griebel
Try

<div class="grid_nav">
        <?php
    $bt_main = BlockType::getByHandle('autonav');
    $bt_main->controller->displayPages = 'top';
    $bt_main->controller->orderBy = 'display_asc';                    
    $bt_main->controller->displaySubPages = 'none';                    
    $bt_main->render('templates/level1');
        ?>
        </div>


In this example you load an autonav block with certain setting and a new theme
Nitrev replied on at Permalink Reply
That code and a few seconds of messing around and it does exactly what I need. Thanks alot!
nathan replied on at Permalink Reply
nathan
I just solved the exact same issue by following this tutorial:

http://www.concrete5.org/help/editing/scrapbook_defaults/...

But this solution also looks good, I'll have to look into this.
Ian replied on at Permalink Reply
Ian
Hi Griebel,

Your post has been very helpful, so first of all, thanks for that!

I am trying to do something that is very similar and i hope you can point me in the right direction???

I want to create a list of footer links (for example: Sitemap, T&C, Privacy etc) I have all these pages in a "Footer Links" folder.

I would like to output these links in an "autonav" or "page_list" im my themes default.php, but i am finding it impossible to match you the GUI options with the corrisponding controller options.

Here is what i have so far...
<?php
$bt = BlockType::getByHandle('page_list');
$bt->controller->displayPages = 'all';
$bt->controller->orderBy = 'display_asc';
$bt->controller->displaySubPages = 'footer_links';
$bt->controller->displaySubPageLevels = 'all';$bt->controller->displaySystemPages = false;
$bt->render('view');
?>


If i am a mile off, please let me know. I would rather reference by a folder name rather than by page ID, as some articles have suggested.

Any light you can shed on the topic would be very much appreciated.
griebel replied on at Permalink Reply
griebel
Hi Ian,

You need to add displayPagesID = your parent footer cID, like this:

<?php
    $bt_main = BlockType::getByHandle('autonav');
    $bt_main->controller->displayPages = 'custom';
    $bt_main->controller->orderBy = 'display_asc';                  
    $bt_main->controller->displaySubPages = 'none';
$bt_main->controller->displayPagesCID = '56';            
    $bt_main->render('view'); 
?>


Have fun building with Concrete
Ian replied on at Permalink Reply
Ian
....but where do i get the PagesID for this folder ?
griebel replied on at Permalink Reply 1 Attachment
griebel
Dashboard > Sitemap > Click on parent folder > Visit, look at the url.

Screenshot attached
Ian replied on at Permalink Reply
Ian
That worked as described.
brennaH replied on at Permalink Reply
brennaH
This is very helpful -- out of curiosity, is there any documentation about the strings you are passing through to those controller functions? You instance, in the sample code, you have controller->displayPages = 'top', controller->displayPages = 'custom', controller->displayPages = 'all'...what are all the possible strings that the function will recognize? Where are those values coming from?

Thanks for the insight,

Brenna
Remo replied on at Permalink Reply
Remo
most people (like me) probably looked at the code..

concrete/blocks/autonav/controller.php

There you find anything you need.

I might update my tutorial on codeblog.ch with these values...
brennaH replied on at Permalink Reply
brennaH
Have no idea why it didn't occur to me to simply look at the file. Better than documentation. Thanks for the (common sense) tip.
ScottC replied on at Permalink Reply
ScottC
so the path of least resistance is to simply add an autonav in the way you need it to be, check it out in the btNavigation and copy those values out to an array then create the array and load those with your BlockType::getByHandle('autonav');

be sure to watch the camelcase in the required array passed in, it caught me today :)
brennaH replied on at Permalink Reply
brennaH
Thought I would give this technique a try with another block type -- Search. I have the following in my header.php file:

<?php
$bt_search=BlockType::getByHandle('search');
$bt_search->controller->set('buttonText','GO');
$bt_search->controller->set('baseSearchPath','/icjournal');
$bt_search->controller->set('resultsURL','/icjournal/site_search');
$bt_search->render('templates/header_search');
?>


Everything is rendering as I expect, but the instead of redirecting and giving search results, I am just bumped to the home page. I've been through the files and the DB and I can't tell what I am missing here. Any help?
ddrace replied on at Permalink Reply
ddrace
I just assigned the properties directly (they're public variables) and it worked like a charm. For example:
<?php
$bt_search=BlockType::getByHandle('search');
$bt_search->controller->buttonText = "GO";
$bt_search->controller->title = "Search";
$bt_search->controller->resultsURL = "/search/search_results";
$bt_search->render('templates/toolbar');
?>


I'll be using this method quite a bit, because it's rare than anyone needs to edit the search block.
mario replied on at Permalink Reply
mario
thanks griebel, remo and scott for the pointers. :)
tolga replied on at Permalink Reply
tolga
Hi griebel,
the information you have given is a life saver for me. However, there are some problems i've seen in action.

When you apply a custom theme for a 'loaded block' you expect to have similar usage like applying a custom theme to a normal block. That is, it should include a view.js and a view.css. That is not the situation.

Moreover, if you create a custom theme with the c5 way, by creating a sub directory to /c5/blocks/block_name/template_name/ you should apply this theme with:

$bt_links->render('templates/template_name/view');


If the template you create is in the /c5/concrete/blocks/block_name/templates/template_name, you can use

$bt_links->render('templates/template_name');


as you've done.

As you can see this is not the way it should be, any custom template (i create) should not use concrete5 directory.

So could you please (or anyone) lead me a solution to load view.js, .css etc in a proper way when i load a block with a custom template within /c5/blocks/block_name/template_name

Thanks.
katz515 replied on at Permalink Reply
katz515
Usagi Project has been trying to develop a mobile support of concrete5.

We now have User_Agent detector, so that if concrete5 detect that you are using iPhone... it will force you to switch theme to mobile theme.

And we also have the image resizer incorporated.

If you have an iPhone, check out

http://concrete5-japan.org

It doesn really display properly in iPhone... but we are almost there.

Now we came to think... how to approach Sidebar, Header, Header Nav area contents....

We need navigation.... but Header Nav only displays top level.

We could include Sidebar... but sometime we put more than auto-nav in the sidebar = longer scroll for mobile users.

So you guys topic got me an idea that...

We could just display Main area contents only...

And we can force-display auto-nav on top and the bottom of the page....

We will work on it...

Thanks~!
Pritam replied on at Permalink Reply
Hi Griebel,

Thank you for this useful post, this works like a charm, the only thing I would like to know is that say that if I want to use a custom template for the autonav which is placed in the /blocks/auto_nav/custom_name

then what path should I define in this line
$bt_main->render('template/');

I tried using just view but it gets the default core template for the auto_nav.
cgrauer replied on at Permalink Reply
cgrauer
$bt_main->render('custom_name/yourtemplate');


This presumes that there is a custom template at
/blocks/auto_nav/custom_name/yourtemplate.php
Pritam replied on at Permalink Reply
Hi cgrauer,

Thank you for your reply, I tried this but then I get an error saying the file not found,to be more clean I have this custom auto_nav template at the following location:

\blocks\autonav\templates\drop_down_menu

Have the view.php and view.css file in place

I tried this
$bt_main->render('templates/drop_down_menu/view');


and the auto_nav comes up but without the style being applied just like when I use

$bt_main->render('templates/view');


I wonder why the template is now being applied.
Remo replied on at Permalink Reply
Remo
I think you don't need "view" if you use a "template folder"
cgrauer replied on at Permalink Reply
cgrauer
If this is your template:
/blocks/autonav/templates/drop_down_menu/view.php
you have to use
$bt_main->render('templates/drop_down_menu/view');


Just pass the path+filename of the template relative to the block folder and without the .php-extension. There is no need to call the template "view.php". Your free to place multiple custom templates in your templates folder without making a subfolder for each template!

This is what c5 does:
If render('abc/xyz') is called, it first searches for
/blocks/autonav/abc/xyz.php, if no file is found, it searches for
/blocks/autonav/view.php, if no file is found, it takes
/concrete/blocks/autonav/view.php.

If the styles are not applied, the path in the importing/linking statement might be wrong.

I'd recommend not to store the styles with the block but define them in the theme's stylesheet. This is what themes are made for.
Pritam replied on at Permalink Reply
Hi Cgrauer,

Thank you once again for your clear and concise explanation, its working perfectly and also your explanation gives me an overall clear explanation about the roles of files like view.css, view.php. view.js

Remo thank you for your wonderful tutorials on your blog, they've been of a great help.


Happy New Year to you guyz !!
sceva replied on at Permalink Reply
sceva
"I'd recommend not to store the styles with the block but define them in the theme's stylesheet. This is what themes are made for. "

I am trying to do this. I want to apply the class settings in my theme's css file to this block, but c5 keeps applying the /concrete/blocks/autonav/view.php settings.

Here is my code:
<div class="o-wrap">
        <?php
    $bt_main = BlockType::getByHandle('autonav');
    $bt_main->controller->displayPages = 'top';
    $bt_main->controller->orderBy = 'display_asc';                    
    $bt_main->controller->displaySubPages = 'all';                    
        ?>
        </div>

where o-wrap is the class in the css file I want to apply. (I am learning css along with c5, so the problem may be there also...)

This is a very helpful post! Also the codeblog.ch tutorials have helped me greatly.
brothersjef replied on at Permalink Reply
brothersjef
Thanks for this, worked like a charm
snebold replied on at Permalink Reply
snebold
Is there a place where all the options are documented?
12345j replied on at Permalink Reply
12345j
there in the autonav controller.php
imran replied on at Permalink Reply
Hi, i want to display a fandrop in my header menu. I use this code in my theme to display those menu.

$bt_main = BlockType::getByHandle('autonav');
            $bt_main->controller->displayPages = 'top';
            $bt_main->controller->orderBy = 'display_asc';                    
            $bt_main->controller->displaySubPages = 'all';
            $bt_main->controller->displaySubPageLevels = 'all';
            $bt_main->render('templates/level1');


Which will display all the levels of page. but actually i want to display a short description and an image with each sub-menu, Is this possible if yes so how i do this...

Any idea's will be appreciated. Thanx.
DKMPK replied on at Permalink Reply
This thread is fantastic! Very much appreciated, did indeed save me some gravelling!

Alas, I seem to have stranded anyways - I am trying to load autonav(and standard sidebar content) into ALL newly created pages, as the user will not need to have alternative navigation on her site.

I placed the code between my other 'standard' content in the default.php of the modified default template, and everything loads perfectly, except for the autonav itself. It even loads the div I placed it in, and I get no errors. Made me think I made a mistake with the settings, but they match the ones I placed in the manually placed autonav. I have been checking, doublechecking, and triplechecking for the past two hours after finding this post, but I just can't figure out what I've done wrong :-(

<div class="code_autonav">
  <?php
      $sidebarNav = BlockType::getByHandle('autonav');
      $sidebarNav->controller->displayPages = 'top';
      $sidebarNav->controller->orderBy = 'display_asc';                    
      $sidebarNav->controller->displaySubPages = 'all';
      $sidebarNav->render = ('view');
  ?>
</div>
melat0nin replied on at Permalink Reply
melat0nin
Hi guys

I can't get this to work. I'm trying to embed a secondary navigation in my template using autonav. I've put all the relevant pages within a parent page called 'topnav' with cID 71.

Here's my code:

<?php
    $bt_main = BlockType::getByHandle('autonav');
    $bt_main->controller->displayPages = 'custom';
    $bt_main->controller->orderBy = 'display_asc';                    
    $bt_main->controller->displaySubPages = 'all';
$bt_main->controller->displayPagesCID = '71';
    $bt_main->render('view'); 
        ?>


Any thoughts? nothing appears at all :(
melat0nin replied on at Permalink Reply
melat0nin
Instead of using Autonav, how do I hardcode a link into the template? Do i just use the standard URL:

www.website.com/concrete5/contact-us


or is there a better way, which prevents it breaking if e.g. the name or location of the Contact Us page is altered?
okhayat replied on at Permalink Reply
okhayat
This is great! Thank you all for your input.
carriesmaha replied on at Permalink Reply
I found this great example -- which shows all of the variables you can use.

<?php 
   $bt_main = BlockType::getByHandle('autonav'); 
   $bt_main->controller->displayPages = 'custom'; // top, above, below, second_level, third_level, custom (Specify the displayPagesCID below)
   $bt_main->controller->displayPagesCID = '62'; // <-- Specify the CID of the page named: REPERTOIRE ( Gets the first level of pages under that section )
   $bt_main->controller->orderBy = 'display_asc';  // display_asc, display_desc, chrono_asc, chrono_desc, alpha_desc 
   $bt_main->controller->displaySubPages = 'relevant';  // none,  relevant, relevant_breadcrumb, all
   $bt_main->controller->displaySubPageLevels = 'custom'; //custom, none
   $bt_main->controller->displaySubPageLevelsNum = '0'; // Specify how deep level 
   $bt_main->render('templates/sub_nav'); // Specify your template or type "view" to use default
?>
jamesleech replied on at Permalink Reply
jamesleech
Very handy - I wish all code was so well referenced!
adajad replied on at Permalink Reply
adajad
JordanLev rewrote the autonav code for the 5.6+ release of c5 and he has supplied this extensive overview: http://c5blog.jordanlev.com/blog/2012/04/hard-coded-autonav-options...

Can't get better than this
baumanator replied on at Permalink Reply
Great thread! I had the idea of putting a block into my template as well. Thanks for all the contributions.
MainostoimistoExx replied on at Permalink Reply
This is awesome guys, but I cant seem to get autonav to show children pages, what I have right now is:

$bt_main = BlockType::getByHandle('autonav');
$bt_main->controller->displayPages = 'second_level';
$bt_main->controller->orderBy = 'display_asc';
$bt_main->controller->displaySubPages = 'all';
$bt_main->controller->displaySubPageLevelsNum = 'all';
$bt_main->render('view');

it should obviously show child pages and I got four levels of pages, these are the exact same setting as I have configured to the block when I add it. Still it wont show sub levels at all. Anyone before me had that? And how did you fix it? :)
leinteractive replied on at Permalink Reply
leinteractive
PetroOmg...instead of displaySubPageLevelsNum = 'all'; you need to use displaySubPageLevels = 'all';

The one you have in there now is looking for a number.