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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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.
       
       
    • By louisstephens
      So I have been hard at work creating url segments for a template (api) and everything is working swimmingly in creating a simple end point for svelte.js. I have however, run into a few questions that I can wrap my head around.
      In my api template I have:
      if($input->urlSegment1 === 'clients') { header('Content-Type: application/json'); $clients = $pages->find("template=clients"); $client_array = array(); foreach ($clients as $client) { $id = $client->id; $title = $client->title; $url = $client->url; $clientName = $client->client_name; $clientColor = $client->client_color->value; $assigned = $client->assigned_to->user_full_name; $client_array[] = array( 'id' => $id, 'code' => $title, 'name' => $clientName, 'associated_users' => $assigned, 'url' => $url ); } $client_json = json_encode($client_array, true); echo $client_json; } The output json from this is:
      [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was curious is it possible to add in "clients" before this output json so it would appear as 
      clients: [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was not really sure of how to tackle this in my php code, and have spent more time than I care to admit trying to figure it out. Another question I have is that "associated_users" is returning null, which in this instance is correct. It is a multi page field that is set to pull a custom name field from the users template, ie "Louis Stephens" would be associated with the first page. I understand that I need to use a foreach to get the correct data, but I was really unsure of how to place this inside an array, or update the array with the new data. Any help with any of this would greatly be appreciated.
    • By Mithlesh
      Changing it to null
    • By Mithlesh
      Hi, I have one URL - writerrelocations.com/contact-now/

      I have one issue where my header Image is appearing again after the contact form, for your reference:



      I have checked my processwire template section where have not added any Image field besides Header Image, attaching the same for the reference:




      PLEASE help me to get rid of the image below contact form

      Waiting for the swift response!

      Thanks
       
×
×
  • Create New...