Jump to content

Combination of 404 & prependTemplateFile = double header?


Adam Kiss
 Share

Recommended Posts

Hey all,

I have a quick (?) question here. I am using prepend/appendTemplateFile in my config for site's header/footer. When using URL segments, I throw 404 Exception for non-existing URL segments, which is all well.

The problem is, that at the moment of 'throw new…', there already is one page header rendered, and the exception renders it again for 404 page.

Is there any way around this?

Thanks.

Edit: require_once for the append/prepend files solves this, so I opened issue on github: https://github.com/ryancramerdesign/ProcessWire/issues/282

Link to comment
Share on other sites

require_once isn't ideal for prepend/append template file settings. When you call $page->render(); you want to know that everything you expect to be included is included. A lot of us call render() methods on other pages, which a require_once() could break. If there are things that need to be require_once (like function or class definitions) you can always move those to another file and require_once them from your prependTemplateFile. My prependTemplateFile usually looks something like this:

// initialize page regions with default values
// this will be output eventually in _main.php
$headline = $page->title;
$browserTitle = $page->title; 
$body = $page->body;
$sidebar = '';

// include shared function libraries
require_once('./includes/finder.php'); 
require_once('./includes/render.php'); 

As for your particular case, it sounds like you've got some logic running after output has started. You want to move that logic to somewhere before output starts. Given that, prependTemplateFile is not an ideal place to output header markup. However, if you want to continue doing that, then you may be able to place this at the top of your prepend TemplateFile:

if($page->name == 'http404') return; 
  • 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

  • Recently Browsing   0 members

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