[SOLVED] Page and class for individual post (eg HelloWorld for /hello-world)

Hi,

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?

thanks
J

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 App.php and Single.php variables are showing with @debug

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 - #6 by alwaysblank 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 HelloWorld's __construct is being called on all pages, not just on /hello-world

I’ve got a fundamental misunderstanding I think here

taking for instance the default /sample-page

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 @debug to 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
etc

and the only variable data is for App

@debug
App

  • Data
    • postobject
    • site_namestringline 9—12
  • Methods
    • titleline 14—32

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 app/Controllers/SamplePage.php file

thanks
J

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

body class: page-sample-page-data
controller: app/Controllers/PageSamplePage.php
template: page-sample-page.blade.php

body class: single-post-hello-world-data
controller: app/Controllers/SinglePostHelloWorld.php
template: single-post-hello-world.blade.php

seems to work fine now.

regards
J

This topic was automatically closed after 42 days. New replies are no longer allowed.