Jump to content

Alter the output of Page::render


---
 Share

Recommended Posts

How can I change the output of `Page::render`?

I've created a hook before page::render, but when I set the $event->return to something else, my whole page is replaced with that value.

For example:

 

public function beforePageRender(HookEvent $event)
{
    $event->return = 'blaat';
}

This will cause my whole page to be nothing more than just 'blaat'.

What I want, is that my menu, footer etc. are still visible but only the content of that page is replaced.

Link to comment
Share on other sites

I don't think you need to use a Hook for this. Since ProcessWire does not output any markup, you can echo whatever you want to be your content, whether it is from another page or another field or hard coded, or whatever. A couple of examples

$out = $page->title;
$out .= $page->summary;// some text field;
if($page->name == 'my-different-page') {
	$page->body = 'Some custom Content';// @note: overwriting body field content
	// or if grabbing from some other page
	//$settings = $pages->get('/path/to/other/page');
	//$out .= $settings->extraContentTextField;
}

$out .= $page->body;

echo $out;

Written quickly, pseudo code, etc...

  • Like 2
Link to comment
Share on other sites

  On 3/24/2017 at 4:18 PM, Soma said:

You need to hook after not before.

Expand  

Okay, and then? Because so far I only managed to get the full html page while I just want to reaplce $page->body (as an example).

  On 3/24/2017 at 3:52 PM, kongondo said:

I don't think you need to use a Hook for this. Since ProcessWire does not output any markup, you can echo whatever you want to be your content, whether it is from another page or another field or hard coded, or whatever. A couple of examples

$out = $page->title;
$out .= $page->summary;// some text field;
if($page->name == 'my-different-page') {
	$page->body = 'Some custom Content';// @note: overwriting body field content
	// or if grabbing from some other page
	//$settings = $pages->get('/path/to/other/page');
	//$out .= $settings->extraContentTextField;
}

$out .= $page->body;

echo $out;

Written quickly, pseudo code, etc...

Expand  

Yes, I know that, but that is in a template file. I want to alter it without using a template file as it should only be altered under specified conditions.

Link to comment
Share on other sites

  On 3/27/2017 at 7:38 AM, tpr said:

You can use str_replace() or preg_replace() but you can also modify fields in /site/ready.php since you can overwrite fields value:

$page->title = 'Custom title';

 

Expand  

That looks like an uggly hack since that could be any page? Wouldn't it be way better to perform such logic in a module instead of a template file?

Link to comment
Share on other sites

I still think it's more ugly than using a hook because if you get more of these exceptions to add to this file it becomes fat and less readable about what it does.

I found out that I just need to get a hook on \ProcessWire\TemplateFile::setFilename, but unfortunately that isn't a hookable method:/

Link to comment
Share on other sites

  On 3/27/2017 at 9:38 AM, tpr said:

Maybe you should post what your actual goal is, that would possibly generate more proper answers.

Expand  

Well it's in my first post:

What I want, is that my menu, footer etc. are still visible but only the content of that page is replaced.

I want to have a hook method in which I alter the content of the body based on a few conditions, but I don't want to replace the full HTML, only the content of the page, so that my menu, footer etc are still visible.
Link to comment
Share on other sites

  On 3/27/2017 at 9:41 AM, FIA2008 said:

I want to have a hook method in which I alter the content of the body based on a few conditions

Expand  
$this->addHookBefore('Page::render', function($event) {
    $page = $event->object;
    // use whatever condition suits your needs
    if($page->id == 1234) $page->body = 'blaat';
});

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...