Silverstripe Version: 4.4
The docs state renderWith
can be used to override the default template process. Rendering data to a template – SilverStripe Documentation
I have these classes:
Page - NewsListPage.
Controller - NewsListPageController
Template - NewsListPage
DataObject - Post
Viewing the list of news, pagination links and by tag all works fine. It just runs the normal inherited naming conventions - I don’t renderWith
in those methods it just uses the NewsListPage template.
The problem I have is viewing the DataObject of the News Post.
In the NewsListPageController I have a ViewPost
method with an $allowed_action
to it.
The code runs this method and finds the post but renders a 404 with this line:
return $this->customise($data)->renderWith('Layout/ViewPost');
“ViewPost” exists in templates/layout.
I added ?debug to the url and it coughed up:
Debug (line 161 of ModelAsController.php): Using record #11 of type NewsListPage with link /news/
#11 is the page ID of the NewsListPage
record. So - it seems like it is still wanting to render the page as the Page class and matching name convention file and ignoring the renderWith
even though the docs say it should be overriding the default template process.
Why is that?
To stick with naming convention I also tried renaming the template to NewsListPage_ViewPost
with no change.
I looked at the Lessons that covered (kinda) this (https://www.silverstripe.org/learn/lessons/v4/the-holderpage-pattern-1) but both are an instance of Page so not the same thing.
What is a better approach of mixing a Page and DataObject in this situation?
I did get this to work by creating a second controller with a route.yaml entry just for the viewing of the post. In that controller the renderWith
works as expected.
I’m still interested to hear your thoughts.