Jump to content

Insert code into rendered page?


Pete
 Share

Recommended Posts

I'm creating a "maintenance mode" module and the somewhat-basic version 1 is almost complete.

What I'm trying to do now is display a message for logged-in superusers that the site is in maintenance mode and add it to the top of the page. This will appear on all pages in the admin and front-end.

What I would like to know is if there is any way to add code just after the opening body tag? I would assume that since every template is different, what I would need to do is do some sort of regexp to find the opening body tag and slot my code in just after it.

Any ideas how I can access the page output before it gets outputted? My module is hooking into Page::render like so:

$this->session->addHookBefore('Page::render', $this, 'maintenanceCheck');
Link to comment
Share on other sites

You could change it to an addPageAfter hook and then just prepend it to the output, i.e.

<?php
$out = $event->return;
$code = '<p>Site in maintenance mode</p>';
$out = preg_replace('/(<body[^>]*>)/', '$1' . $code, $out); 
$event->return = $out;  

PW's <body> tag doesn't actually have any attributes, so you could also do this (below) but the above is a little safer just in case we add attributes in the future (or somebody else's theme does):

$body = str_replace('<body>', '<body>' . $code, $body); 

Another option is to use JS in your hook. In this case, you could use the Page::render hook, or another if you preferred.

$this->config->scripts->add($this->config->urls->YourModuleClassName . 'your-file.js');

Your JS file would have this:

$(document).ready(function() {
    $("body").prepend("<p>Site in maintenance mode</p>"); 
}); 

It's perfectly fine to use a JS-only solution in PW admin because PW admin requires JS.

Link to comment
Share on other sites

Thanks ryan - just two things:

addPageAfter <- I didn't get that bit - do you mean addHookAfter?

Also, I'm trying to display the message in both the admin and live side of things. My thinking was that sometimes if you're logged in as an admin and you're previewing changes to pages on your site there's the possibility you could forget it's in maintenance mode, so a message in both places would be good. For that reason I'd probably go with your first suggestion in this case as both the admin and page templates are likely to have <body> tags (though I realise I'll have to do some extra regexp in case anyone's modified the body tag in a template file).

Link to comment
Share on other sites

That's right, sorry I meant addHookAfter (trying to type too fast).

If you need to show on the live end of things too, then I think you are right about the best method to use. I would probably change the regexp to this (add the "i" at the end), just in case someone is using uppercase tags for some reason:

$out = preg_replace('/(<body[^>]*>)/i', '$1' . $code, $out); 
Link to comment
Share on other sites

Hmm... can't seem to get it to work though.

The rest of my module works except this bit with the message.

Here's the line for that hook:

$this->session->addHookAfter('Page::loaded', $this, 'maintenanceMessage');

And here's the function (stripped out any additional checking so it should display the message no matter what's going on elsewhere in the module):

public function maintenanceMessage(HookEvent $event){
			$out = $event->return;
			$code = '<p>Site in maintenance mode</p>';
			$out = preg_replace('/(<body[^>]*>)/i', '$1' . $code, $out);  
}
Link to comment
Share on other sites

I'm not sure that it matters, but remove 'session' from your addHook line. Just make it: $this->addHookAfter(...)

Also for testing purposes, change the preg_replace to str_ireplace:

$event->return = str_ireplace("<body>", "<body>" . $code, $event->return);

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...