Jump to content
Sign in to follow this  
Edison

Markup Regions in Hooks

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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...