Hardcoding Autonav with a template
Permalink
I am trying to hardcode an autonav on the page with a custom template. I have tried two ways:
Method 1) calling a custom block
(<?php
$b = Block::getByName('test_menu');
$b->display();
?>
When I choose the template for the block in the scrapbook, it shows the template there but not actually on the page itself.
Method 2)
I hardcode it in with this code:
<?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 = '80'; // <-- 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/drop_down_menu'); // Specify your template or type "view" to use default
?>
No change.
I know the template works because I have called it for the autonav on the page and everything was fine. I just want it to be part of the default so the client doesn't have to add it to every page.
Can anyone help?
Method 1) calling a custom block
(<?php
$b = Block::getByName('test_menu');
$b->display();
?>
When I choose the template for the block in the scrapbook, it shows the template there but not actually on the page itself.
Method 2)
I hardcode it in with this code:
<?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 = '80'; // <-- 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/drop_down_menu'); // Specify your template or type "view" to use default
?>
No change.
I know the template works because I have called it for the autonav on the page and everything was fine. I just want it to be part of the default so the client doesn't have to add it to every page.
Can anyone help?
What's the file name of your custom template, and where is it located?
Sorry that codes was a mess. My file is in root/blocks/autonav/templates/drop_down_menu and I tried to call in with "templates/drop_down_menu".
This is a bit counter-intuitive, but you need to name your file with a ".php" at the end (so drop_down_menu.php in your example), but do not include the ".php" in the "render" code.
BTW, you can make the code look clean in forums by putting code tags around it (square brackets around the word "code" -- which I can't show you here though because the forum software would just think I actually wanted to output code -- but if you look just below the message window it shows you -- "Enclose code samples in ...").
BTW, you can make the code look clean in forums by putting code tags around it (square brackets around the word "code" -- which I can't show you here though because the forum software would just think I actually wanted to output code -- but if you look just below the message window it shows you -- "Enclose code samples in ...").
Maybe that is my mistake. In the hardcode I had "templates/drop_down_menu" and the "drop_down_menu" is another subfile. Should it be "templates/drop_down_menu/view.php"?
Thanks for the tip about putting code in the forum!
Thanks for the tip about putting code in the forum!
Yes, if your template is in a subfolder, then the file should be called view.php
I do have a "view.php" in the folder "drop_down_menu" but what I meant was, do I need to write "templates/drop_down_menu/view.php" to get the hardcode to call the template?
I think it's just 'templates/drop_down_menu'. But if that doesn't work, also try 'templates/drop_down_menu/view' (either way, it's without the .php at the end).
This is the code I am using to hardcode a menu with template:
It doesn't work. When I go into the page and add "autonav" and then call the template it works fine. What am I doing wrong with the hardcoding?
<?php $bt_main = BlockType::getByHandle('autonav'); $bt_main->controller->displayPages = 'top'; $bt_main->controller->orderBy = 'display_asc'; $bt_main->controller->displaySubPagesLevels = 'all'; $bt_main->controller->displaySubPageLevelsNum = 'all'; $bt_main->render('templates/drop_down_menu/view'); ?>
It doesn't work. When I go into the page and add "autonav" and then call the template it works fine. What am I doing wrong with the hardcoding?
Try changing that last line from this:
...to this:
$bt_main->render('templates/drop_down_menu/view');
...to this:
$bt_main->render('templates/drop_down_menu');
Tried both, still no luck. It should work, shouldn't it? I would like to hardcode it to save my client the trouble of adding it by hand since it is the same for every page but this has me stumped.
It should work, I have no idea why it isn't. Sorry :(
This may sound weird but try renaming your custom template to dropdown_menu.php and then call render('templates/dropdown_menu').
I recall having some problems with template names that have multiple underscores. However, I was using older version of Concrete back then and it may have caused the problem, but you can still give a try.
I recall having some problems with template names that have multiple underscores. However, I was using older version of Concrete back then and it may have caused the problem, but you can still give a try.