Jump to content

Markup Regions in Hooks


Edison
 Share

Recommended Posts

I am using Markup Regions in Templates and they are awesome ?. I would like to use them also in Hooks/Modules, but here I am having some troubles.?

When placed in a Template File this simple snippet works perfectly. It pre-pends and combines with the main header section. 

Quote

<header id="header" pw-prepend><div><p>I enjoy ProcessWire!</p></div></header>

The same snippet in Page::render or TemplateFile::render hooks does not work as expected. It renders but without pre-pending and combining to the header. In the html source, pw-prepend directive remains visibile, like if Processwire had not processed the markup region. May be these two events are fired too late to take benefit of markup regions? Do I need to use a different hook ?

Quote

wire()->addHookAfter('Page::render', function($event) { $event->return .= $out }

wire()->addHookAfter('TemplateFile::render', function($event) { $event->return .= $out }

It would be nice to find some examples documenting how to use markup regions inside hooks/modules.

Link to comment
Share on other sites

TemplateFile::render does seem to be a good place to tweak the markup right before it goes into the MarkupRegions class. Is that one not working?

Here is where the template is rendered: https://github.com/processwire/processwire/blob/master/wire/modules/PageRender.module#L514

Here is where the markup regions are populated: https://github.com/processwire/processwire/blob/master/wire/modules/PageRender.module#L524

 

  • Like 3
Link to comment
Share on other sites

Thank Elabx, that's very helpful. I will make further trials with markup regions in hooks, but as I was a bit in a hurry I solved it the old way ? ... making a str_replace on $event-return. 

wire()->addHookAfter('Page::render', function($event) {
	$event->return = str_replace('<header>', '<header>' . $this->banner->markup(), $event->return) . $this->banner->assets();
});

 

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

I've come across a situation where I too need to apply markup regions via a hook (inside a module), but it's not applying with Page::render or TemplateFile::render.  I tried various combinations of things.

@Edison were you able to ever get it working properly?  @elabx any other suggestions?

Note: it works fine if I do it without hooks inside /site/templates/_init.php, if that provides any insight.

Link to comment
Share on other sites

11 hours ago, Jonathan Lahijani said:

I've come across a situation where I too need to apply markup regions via a hook (inside a module), but it's not applying with Page::render or TemplateFile::render.

Markup Regions work by having markup appear before the opening <html> tag. Reference: https://processwire.com/docs/front-end/output/markup-regions/#technical-details-of-how-markup-regions-work

So when hooking TemplateFile::render you would insert the HTML that will populate a Markup Region at the start of the rendered template file output, so it will appear before the contents of the auto-appended _main.php. Example:

$wire->addHookAfter('TemplateFile::render', function(HookEvent $event) {
	$template_file = $event->object;
	$out = $event->return;
	// If the template file being rendered is basic_page.php...
	if($template_file->filename === $event->wire()->config->paths->templates . 'basic_page.php') {
		// ...then prepend the following Markup Region markup
		$my_region = <<<EOT
<div id="body">
	<p>Hello world</p>
</div><!--#body-->
EOT;
		$event->return = $my_region . $out;
	}
});

 

  • Like 4
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...