Hard coding autonav wont pick up custom template

Permalink 2 users found helpful
I'm trying to hardcode an autonav to my site for the main menu.

I 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?

 
aghouseh replied on at Permalink Reply
aghouseh
Do you have the file in the filesystem as "templates/drop_down_menu/view.php"? I'm not certain that works. Proper implementation is "templates/drop_down_menu.php" and then call 'templates/drop_down_menu' with ->render.

$bt->render('templates/drop_down_menu');


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.
hivoltage replied on at Permalink Reply
Just tried this, no it doesn't work either.

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.
cannonf700 replied on at Permalink Reply
cannonf700
This worked perfect for me. I hard coded an autonav block but needed to use the "exclude_subpage" template. I changed the second part from drop_down_menu to exclude_subpages and it did exactly what I needed it to do - thanks!
hivoltage replied on at Permalink Reply
After reviewing the issue, I believe there must be something wrong with my custom templates view.php file.

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?
jordanlev replied on at Permalink Reply
jordanlev
Where did you put the js and css files?
(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?
hivoltage replied on at Permalink Reply
yes, this is the path. I added the css and js files in the same folder as the view.php file.
hivoltage replied on at Permalink Reply
If I manually add the following code to the view.php file, it works.

<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.
jordanlev replied on at Permalink Reply
jordanlev
Yeah you don't want to do this.

Try putting the js and css files into YOURSITE/blocks/autonav/ directory (2 levels up from where you have them now I believe).
hivoltage replied on at Permalink Reply
nope, same problem
hivoltage replied on at Permalink Reply
how would i write the addHeaderItem() to add these files? I've already tried, but I may have been doing this wrong.
jordanlev replied on at Permalink Reply
jordanlev
Because this is a built-in system block, it's not very straightforward to add your own css and js files this way. What I usually do is just include the css for the nav menu in my theme's main css file (and include any required javascript from the theme header as well) -- generally the nav menu is on every page of the site so it's not a problem to include it in the theme templates.

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
Shotster replied on at Permalink Reply
Shotster
I'd like to suggest an alternative approach which works fine for me. First, create and configure your main nav menu as a global Auto-Nav scrapbook block. Then, in your header.php file, add the following code...

<?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
jordanlev replied on at Permalink Reply
jordanlev
I completely agree that this is a simpler way to go. But the major stumbling block the OP is having now is that the custom template won't load his custom css/js -- and this problem will unfortunately not be solved by using the global scrapbook method. Although it's worth a shot -- perhaps there is some glitch in the C5 system that will make it magically work using this technique??
Shotster replied on at Permalink Reply
Shotster
> the major stumbling block the OP is having now is that the custom template
> 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
jb1 replied on at Permalink Reply
jb1
Yeah, that's the method I use as well.
plindelauf replied on at Permalink Reply
plindelauf
I'm running into exactly the same problem: css and javascript is not picked up when rendering a custom template this way. Any solutions yet?
jordanlev replied on at Permalink Reply
jordanlev
I don't think there's an ideal solution for this yet. I have noticed that for custom templates, you often need to put the custom js/css files in the top-level block directory instead of in your template directory. For example, if you put your custom template here:
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.