Jump to content
ridgedale

PW3 - Separate Header Footer & Sidebar Files

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

why don´t you use

?=$page->title?

 

Share this post


Link to post
Share on other sites

Hi gunter,

Simply adding:

<?php namespace ProcessWire; ?>

to the top of the _header.php page resolved the issue.

Can you explain why and how your suggestion might have helped with an example?

Share this post


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

Share this post


Link to post
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... :)

 

Share this post


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

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Marvin
      Hello, i want to ask, i maintain a website that using a processwire and php, and i want to make an archive at my website using a subfolder system, but when i try,
      the sebfolder is show but when i click the files in that subfolder not show, and my browser just show me an error Invalid argument supplied for foreach(), i don't know why it error
      Here i attach my code and my screenshoot website :
      This is my code

      This is result of my website

      This is my error

       
       
    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By CareerTeam GmbH
      Hi there,
      We are an executive search agency based in Germany looking for a freelancer (2-5 days per week) supporting us with the development and design of our websites. The position will be located in Hamburg, Germany and it would be great if you are on short call. German language knowledge is mandatory. 
      You can reach me via email jobs@careerteam.de.
      Thank you!
      Regards
      Annemie
    • By louisstephens
      I was really unsure of how to actually title this post, so I do apologize (if someone has a better idea, I will gladly edit it). I am using the profields: pagetable field to allow people to create their own "content" (copy, image, button, etc etc) and rearrange it. I also included a field called "column_size" using the RangeSlider set to (1-12).
      I guess I'll clarify a bit more on this. I am using flexbox where the "row" is <section></section> and the columns are <div class="column"></div> have given the "columns"  flex: 1 1 0; so no matter how many columns you have, the columns will automatically adjust for new content. Where my confusion is coming in: If a user has set up 3 copy items (with 12, 5, 7 respectfully for the column_size), how do I actually output this in my template? I was going to use a switch statement to handle the various items which I thought made it quite easy, but with closing sections and columns I have confused myself as I assume I need an if statement to check if the column size is > 12, or = 12 to determine the actual closing/opening of sections. I apologize if I have not made this very clear. I am a bit unsure how to word this let alone to go about this. 
      Im very appreciative of for any insight into this.
       
       
    • By Atlasfreeman
      Hi!
      I want to make a small site, a one page site.
      And i have this idea about doing 2 to 3 diffrent template that i can load into the index / home page.

      I want to do this with an array so that i can keep creating more topics (with the template) id needed.
      <?php include('./head.inc'); // include header markup ?> <?php $children = $page->get('template=onecolmn|twocolumn, sort=sort'); foreach($children as $child) { include($child); } ?> <?php include('./foot.inc'); // include footer markup ?>  
      As you can see i have to template wish i want to control from the backend.
      I know include does not work this way, put what is my other option to make an array that loads the whole page on an other pages.
       
       
×
×
  • Create New...