Programmatically add block with custom template to theme when both are in a package

Permalink 1 user found helpful
I have a package that contains all the files relevant to my theme. This includes some block templates as well as the theme files.

My question is this. How can I add a block (autonav) to my theme file and have that block use a custom template that is located within the package that also contains the theme file.

My file structure looks like this:
- theme_my2feethd1
-- blocks
--- autonav
---- templates
----- m2f_dropdown
------ <template files>
-- themes
--- my2feethd1
---- <theme files>

The template works as it should when I am manually adding the autonav as a block using the normal method of doing so, however I just want to programmatically add the autonav in the theme file code like so:
$bt_main = BlockType::getByHandle('autonav');

This example does not work, so what should be used as the render parameter?

Thank you :)

View Replies: View Best Answer
jero replied on at Permalink Reply
Since you're creating a package, you should add a new view for the autonav in packages/"your_package"/blocks/autonav/templates/Myview.php. You wouldn't add the template to the theme.

Then, when you want to use the template, use


Note that you do not need anything except the view.php.

On a slightly different tack, if you're going to hack then autonav template, I'd recommend starting with this one:
ideasponge replied on at Permalink Reply
WE actually do need more than the view.php file since there is also some CSS and javascript used for that menu.

The reason for the block template to be part of the package is to streamline the management of the site design for future upgrades. The structure I am using is sound, I just need to know how to call the custom template from within the package.

Using 'templates/m2f_dropdown' doesn't work unfortunately.

Thank for the reply though :)
jero replied on at Permalink Reply
You may have missed something somewhere - placing a block template within a package works just fine - I have done this just this afternoon on my own site. Perhaps there's a typo somewhere. Try adding an autonav block to a page and the selecting a custom template - yours should show up in the list.

You can also include css and js, but instead of using templates/yourcustomerview.php, you would need to use templates/yourcustomview/view.php, and drop the css and js into view.css and view.js, both within the templates/yourcustomview folder.

One caveat you should be aware of though, is that js and css won't be picked up if you're loading the block programatically. In this case, you could simply add the appropriate links into your theme instead.
ideasponge replied on at Permalink Reply
Yes that is how we have the template setup now and it works when manually selecting the template using the normal method. I did not know about the view.css and view.js files not being loaded so thank you for that.

When I use this code
$bt_main = BlockType::getByHandle('autonav');
$bt_main->orderBy = 'display_asc';
$bt_main->displayPages = 'top';
$bt_main->displaySubPages = 'all';
$bt_main->displaySubPageLevels = 'custom';
$bt_main->displaySubPageLevelsNum = '1';

I don't get any errors but the only link that shows up is the Home link, nothing else. I get the same result when using 'view' as the parameter for the render call.
jero replied on at Permalink Reply
I'm not sure why, but it seems with that combination of parameters, the number of elements in the aBlocks array is 1. I'm seeing this in the standard view.php.

You could try here:

but admittedly, this doesn't cover your combination. Good luck.

Perhaps a solution might be to use the template I mentioned earlier, weeding out the links you don't want having specified "all" sub pages in the controller parameters. Good luck!
ideasponge replied on at Permalink Reply
ideasponge replied on at Permalink Best Answer Reply
I found the problem. I had two. First was that I was setting the values in the block wrong, I need to use it with
$bt_main->controller->orderBy = 'display_asc';

Instead of
$bt_main->orderBy = 'display_asc';

Second was that since my template is actually a folder containing multiple files, I needed to use 'templates/m2f_dropdown/view'.

I am assuming this is a bug so I will report it just in case.
jero replied on at Permalink Reply
D'oh! I should have spotted the $bt->controller->parameter thing, and now I look at my notes, it's staring right back at me.

The templates path sounds right. I think it looks for either a file with suffix .php, or a folder containing a view.php so if you've got a hierarchy underneath templates, you'd need to make the path yuo feed the controller match.