Way to hardcode custom block view template from another package?
Permalink
I've seen discussion about this here before, but no answers outside of adding the block to the page defaults and setting the custom template there.
Unfortunately, this won't work in my case. I'm trying to do an ajax refresh of a block and want to apply my own package's custom template to it.
My package has a custom view template for 'leteco_wall' called 'commentable' The commentable template is in a folder rather than a single file (so it looks like my_package/blocks/lerteco_wall/commentable/view.php and my_package/blocks/lerteco_wall/commentable/view.css)
So in my new block's controller, after removing the lerteco_wall div via jquery, I'd like to add it back using this:
There are two problems with this:
1. the commentable template cannot be found because it is not local to the original lerteco_wall block folder and 2. it is not a single file (it is looking for commentable.php)
Any ideas on how to accomplish this? For my personal site, I've just replaced the original view.php and view.css lerteco_wall templates with my own and then used this in the new block's controller:
Works fine and dandy, but I'd like to package this up for the marketplace so some automagical stuff needs to happen so that uses don't have to overwrite files etc.
If this is confusing, I'll include the package as it stands now (it is still pretty rough).
Unfortunately, this won't work in my case. I'm trying to do an ajax refresh of a block and want to apply my own package's custom template to it.
My package has a custom view template for 'leteco_wall' called 'commentable' The commentable template is in a folder rather than a single file (so it looks like my_package/blocks/lerteco_wall/commentable/view.php and my_package/blocks/lerteco_wall/commentable/view.css)
So in my new block's controller, after removing the lerteco_wall div via jquery, I'd like to add it back using this:
if ($_REQUEST['ajax'] == true) { $bt = BlockType::getByHandle('lerteco_wall'); $bt->render('commentable'); exit; }
There are two problems with this:
1. the commentable template cannot be found because it is not local to the original lerteco_wall block folder and 2. it is not a single file (it is looking for commentable.php)
Any ideas on how to accomplish this? For my personal site, I've just replaced the original view.php and view.css lerteco_wall templates with my own and then used this in the new block's controller:
if ($_REQUEST['ajax'] == true) { $bt = BlockType::getByHandle('lerteco_wall'); $bt->render('view'); exit; }
Works fine and dandy, but I'd like to package this up for the marketplace so some automagical stuff needs to happen so that uses don't have to overwrite files etc.
If this is confusing, I'll include the package as it stands now (it is still pretty rough).
To your first point, yes, I made a mistake in the original post. The directory is my_package/blocks/lerteco_wall/templates/commentable
I can apply my template from the front end without any issues. The only problem occurs when I try to render the view from my block's controller.
Yeah, it would be no problem inlining all of the css, but the render is still is looking for a file local to the orginal lerteco_wall block directory.
Thanks for the input!
I can apply my template from the front end without any issues. The only problem occurs when I try to render the view from my block's controller.
Yeah, it would be no problem inlining all of the css, but the render is still is looking for a file local to the orginal lerteco_wall block directory.
Thanks for the input!
I just copied the view.php to ../commentable.php and changed the render() function to
and it seems to use your template successfully.
(Adding the 'templates/' was surprising, but there's some code that I dont' fully understand the purpose of which strips the 'templates/ then calls ->setBlockCustomTemplate() but only if 'templates/' is part of the view name.)
James
$bt = BlockType::getByHandle('lerteco_wall'); $bt->render('templates/commentable');
and it seems to use your template successfully.
(Adding the 'templates/' was surprising, but there's some code that I dont' fully understand the purpose of which strips the 'templates/ then calls ->setBlockCustomTemplate() but only if 'templates/' is part of the view name.)
James
Nice! Perfect solution. I dumped the CSS into the commentable.php file and it looks slick. Thanks.
Just a few thoughts.
1. You should have no problems including your template when rendering my block. I know it can be done from the front-end, and I'm currently looking at line 89+ of block_view_template.php. Originally I thought your problem was that you had commentable/view rather than commentable/templates/view, but that doesn't seem to be an issue. You should be able to include just the single file looking at the code, it seems my_package/blocks/lerteco_wall/commentable.php should work).
2. Looking at the code, I see no way to include an entire subdirectory (which will then include the view.php and any css/js. Kinda surprising. But once you've got the template rendering, I guess there are two things to do:
a) include the CSS locally, either inline or with an include directive within the template. I'm doing this with some JS scripts in some alpha code I have for my 'simple content' package, and I'm setting a JS variable to true after including the first time, then checking for said variable.
b) On the other hand... you're theoretically calling some of this ajax from within your own block (ie, your block has been added to the page). In that case, you should just merge all the css into one file, even if it will only be used when lerteco_wall is present.
James