Programmatically add block with custom template to theme when both are in a package1 user found helpful
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:
------ <template files>
---- <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'); $bt_main->render('m2f_dropdown');
This example does not work, so what should be used as the render parameter?
Thank you :)
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:
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 :)
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.
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'; $bt_main->render('templates/m2f_dropdown');
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.
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!
$bt_main->controller->orderBy = 'display_asc';
$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.
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.