Hard coding autonav wont pick up custom template
Permalink 2 users found helpfulI have a custom template for the menu, however when I specify the template to be rendered, it doesn't pick it up.
This is the code I have:
$bt = BlockType::getByHandle('autonav'); $bt->controller->displayPages = 'top'; $bt->controller->orderBy = 'display_asc'; $bt->controller->displaySubPages = 'all'; $bt->controller->displaySubPageLevels = 'custom'; $bt->controller->displaySubPageLevelsNum = '1'; $bt->render('templates/drop_down_menu/view');
I have set up my custom template in the path 'templates/drop_down_menu', but for some reason the above code is not displaying my custom template.
However, if I manually add the autonav bock to my page and choose the same custom template, it all works perfectly fine. It just wont work when I try hardcoding the autonav to the theme template.
Any ideas?
For the record, 'templates/drop_down_menu/view.php' is acceptable.
I think the problem is in the view.php file since it wont load the css and js files when the autonav is hard coded. However, it picks up the css and js files when I manually add the autonav bock.
When my page renders to the browser, I have noticed that source code from the view.php file is being loaded on to the page, however the css file and the js file associated with the template is not being loaded.
Yet as mentioned in my previous post, when I manually add the autonav block to my page, it picks up the css file and js file automatically. It just wont pick them up when I hard code the autonav.
Any ideas why the css and js files are not being loaded?
(And just to be sure, when you say you put the view.php file in "templates/drop_down_menu/", you mean YOURSITE/blocks/autonav/templates/drop_down_menu/", correct?
<link rel="stylesheet" type="text/css" href="/blocks/autonav/templates/drop_down_menu/view.css" /> <script type="text/javascript" src="/blocks/autonav/templates/drop_down_menu/view.js"></script>
But I shouldn't have to manually add the css and js, they should be picked up automatically.
Also, the problem with this is the code gets added to the html body, where as it belongs in the header.
I also tried using addHeaderItem() to add the code to the header, but i just can't get this to work.
Try putting the js and css files into YOURSITE/blocks/autonav/ directory (2 levels up from where you have them now I believe).
If you don't know how to add css and javascript to your theme files, search the forums for this as it is a very common things to do so there are tons of other posts about it.
Hope that helps.
-Jordan
<?php $b = Block::getByName('My Main Nav Menu'); if( is_object($b) ) $b->display(); ?>
That's it. Assuming you named the global scrapbook block "My Main Nav Menu" it should work fine. You can create a custom template, and it doesn't have to be named view.php. Just select the template through the global block as you would for any other block. Make sure the template is located in...
YOURSITE/blocks/autonav/templates/my_custom_template_name.php
Unless there's some reason you can't or don't want to use this approach, it should work fine.
-Steve
> won't load his custom css/js
Ah yes, I see. Unfortunately, global scrapbook blocks aren't considered normal "page blocks" per se, and so outputAutoHeaderItems() never gets called for them
:-/
-Steve
YOURSITE/blocks/autonav/templates/my-custom-template/view.php
...then your custom css file would need to be here:
YOURSITE/blocks/autonav/view.css
Kind of sucks because it means you can only have one custom css/js file for ALL custom templates (which works fine if you just have one override for your entire site, but defeats the purpose of being able to choose from multiple custom templates). A workaround solution would be to just include all custom CSS in that one overridden view.css file but put different class names in front of them, then in your custom template surround everything with a div having that specific class. You could do a similar thing with the JS by having everything for one template in an initialization function in view.js, and only call the appropriate initialization function from within a <script> tag in the custom template view.php file.
Not ideal I know, but hopefully lets you solve this problem until the situation is fixed in a future version of C5.
Also, I am assuming that when you mean your path is 'templates/drop_down_menu', you mean its actually '/blocks/autonav/templates/drop_down_menu', as that's where it looks.