Reputation: 461
I've got a silverstripe blog which I'm using for a couple different areas in the site and want to use a different template for each (rather than trying to use lots of conditions in the template)..I can't get the template to render - heres the bare bones:
class BlogExtension extends DataExtension
{
private static $db = [
'BlogType' => 'Varchar'
];
}
class BlogPostExtension extends DataExtension
{
public function isNews()
{
return $this->owner->Parent()->BlogType == 'news';
}
public function isBlog()
{
return $this->owner->Parent()->BlogType == 'blog';
}
}
And, I'm tring to do something like the following to render each blogpost type in either BlogPost_news.ss or BlogPost_blog.ss:
class BlogPostControllerExtension extends DataExtension
{
public function onBeforeInit() {
//render with custom template
if ($this->owner->isBlog()) {
return $this->owner->renderWith(BlogPost::class .'_blog');
}
}
But I don't think I'm quite on the right track here :)
Upvotes: 2
Views: 111
Reputation: 24406
You could always subclass Blog
and/or BlogPost
and call it News
and NewsPost
, which would then automatically look for templates called that as well. It would also show up in the CMS as a different page type.
It'd be a little tricky to modify the templates used, since you don't have direct access to the PHP class instances (e.g. if you'd extended them, you would). You may have some luck with an extension in the way you're attempting, but it'd rely on having a hook to modify the templates it chooses to use.
You could also override the Blog.ss
and BlogPost.ss
templates and put something like this into them:
<% if $isBlog %>
<% include MyCustomBlogTemplate %>
<% else %>
<% include MyCustomNewsTemplate %>
<% end_if %>
Then put your separated template logic into those individual templates.
Upvotes: 1