Jump to content

PW3 - Separate Header Footer & Sidebar Files


ridgedale
 Share

Recommended Posts

Hi,

I need to separate the header, footer and sidebar from the _main.php file.

I've tried all of the following without success:

<?php include ("header.php"); ?>
<?php include ("/_header.php"); ?>
<?php include ("./header.inc"); ?>
<?php $header = pages()->get('/rcl-header.php'); // include header
echo $header; 
?>

Any guidance would be appreciated.

Link to comment
Share on other sites

Hi @ridgedale,

it is recommended to include your files like this:

include("./header.php");

Like mentioned here.

But I prefer to make an extra folder "includes" and use the wireFileInclude function:

wireIncludeFile("./includes/header");

Cool thing about this function is:

  • PHP file ending is assumped
  • You can pass variables as array:
wireIncludeFile("./includes/header", array(
	'homepage' => $homepage
));

Regards, Andreas

  • Like 2
Link to comment
Share on other sites

Hi @AndZyk,

Thank you for your reply. Unfortunately that code does not render the header.php content, as I already indicated.

When I rename the file to header.php and add the the following code as suggested at the top of the _main.php file:

<?php namespace ProcessWire;
// _main.php template file, called after a page’s template file	
$home = pages()->get('/'); // homepage
$siteTitle = 'Regular';	
$siteTagline = $home->summary; 
?>

<?php include("./header.php"); ?>

I get the following when I look at the source code the following appears from line 34 onwards actually after a blank initial line and the sidebar content:

<!DOCTYPE html>
<html lang="en">
<head id='html-head'>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title id='html-title'><br />
<b>Fatal error</b>:  Call to undefined function page() in <b>/[local path to site directory]/site/templates/header.php</b> on line <b>6</b><br />


<p style='background:crimson;color:white;padding:0.5em;font-family:sans-serif;'><b>Error: 	Call to undefined function page() (line 6 of [local path to site directory]/site/templates/header.php) </b><br /><br /><small>This error message was shown because: site is in debug mode. ($config-&gt;debug = true; =&gt; /site/config.php). Error has been logged. </small></p>

I don't know if it makes any difference but the site-regular profile is being run with pw 3.0.61.

That error refers to the following line in the header.php file, btw:

	<title id='html-title'><?=page()->title?></title>

I've not had any issues with php include in the past. So this is a little frustrating.

Any further thoughts would be appreciated.

Thanks.

Edited by ridgedale
missing information added
Link to comment
Share on other sites

What is the line 6 in your header.php file? This line obviously causes the error. The include works, because if it wouldn't, there would be no error.

Have you tried to add the namespace in the first line of your header.php? I think you need it to use the new functions API.

  • Like 3
Link to comment
Share on other sites

Hi @AndZyk,

4 minutes ago, AndZyk said:

Have you tried to add the namespace in the first line of your header.php? I think you need it to use the new functions API.

Bingo! :)

Adding the following to the top of the hader.php file resolved the issue.

Thanks for your help. Very much appreciated.

  • Like 1
Link to comment
Share on other sites

Quote

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions:

  1. Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
  2. Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.

http://php.net/manual/ro/language.namespaces.rationale.php

So by using the variable you would have no need for the namespace, depending on what else is in your file.

Link to comment
Share on other sites

Hi Ridgedale, I didn´t know that it´s possible to approach the $page-title also via page()->title... and so I thought that´s why the "call to undefined function page()"- error came from... :)

 

Link to comment
Share on other sites

1 hour ago, gunter said:

Hi Ridgedale, I didn´t know that it´s possible to approach the $page-title also via page()->title... and so I thought that´s why the "call to undefined function page()"- error came from... :)

more info: https://processwire.com/blog/posts/processwire-3.0.39-core-updates/#new-functions-api

Side note: to make page() etc. (functions API) available you need $config->useFunctionsAPI = true; in /site/config.php as described in Ryan's article.

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

×
×
  • Create New...