Edison Posted June 26, 2019 Share Posted June 26, 2019 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 More sharing options...
elabx Posted June 27, 2019 Share Posted June 27, 2019 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 3 Link to comment Share on other sites More sharing options...
Edison Posted June 28, 2019 Author Share Posted June 28, 2019 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(); }); 1 Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted November 20, 2020 Share Posted November 20, 2020 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 More sharing options...
Robin S Posted November 21, 2020 Share Posted November 21, 2020 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; } }); 4 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now