just trying to understand how the classes work. If i add a HelloWorld.php then this class will run for the included /hello-world sample post
but data (public functions) are not passed to the template.
Is it possible to create a page/class for a specific single post?
for instance in Timber/Twig you can create eg single-103.twig where 103 is the post id, and this gets pulled into single.php ,
Timber::render( array( 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ), $context );)
which could be done easily with the Sage single.php
but in terms of the actual page /hello-world and the class HelloWorld, is there a way to inject that data (ie the controller functions) into a template?
What have you tried so far?
I can see
app/controllers/HelloWorld.php constructor gets called since
hello-world is a body class, but none of the function variables are available. Only
Single.php variables are showing with
I had assumed all controllers would pass variables to the template via their functions, so I’ve misunderstood how it is wired up.
Have a look at your body classes on the page and paste them in here. Anything with
-data can be used for controller.
By default templates only receive data from two controllers,
App and whatever template-specific one they match first. More details here: How to create a controller file for a custom template page in Sage 9 You can instruct Controller to inherit from all matching templates by enable the tree hierarchy: https://github.com/soberwp/controller#inheriting-the-treehierarchy
Thanks I had put in
protected $tree = true; earlier but it didn’t work. Maybe I did something wrong. Trying again.
And I’ve just realised
__construct is being called on all pages, not just on
I’ve got a fundamental misunderstanding I think here
taking for instance the default
I can create a
page-sample-page.blade.php that loads ok for
/sample-page but I thought if I added a
app/Controllers/SamplePage.php then this would run (due to the
sample-page body class) and then expose it’s variables
when I add
page-sample-page.blade.php I get a bunch of notices
Notice: Undefined variable: last in /srv/www/trellis.test/current/web/app/themes/sage/vendor/soberwp/controller/src/Blade.php on line *28*
Notice: Trying to get property 'tree' of non-object in /srv/www/trellis.test/current/web/app/themes/sage/vendor/soberwp/controller/src/Blade.php on line 28
and the only variable data is for
- site_namestringline 9—12
what am I missing here please? which body classes will actually pass variables to the page?
again I’m using the default install here without any changes other than adding the
sorry, got it
leaving this here for anybody else having problems working it out
it uses the body classes that have
-data but without the
Data in the class name
so we need
seems to work fine now.
This topic was automatically closed after 42 days. New replies are no longer allowed.