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 anderson
      Hi,
      For the purpose of learning, as shown in this photo, I created a repeater field "we", then a template, then a page.
      But in /templates/testrepeater.php, I has some problem, the "foreach" part does not work as expected.
      <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member); ?> <img src="<?php echo $member->images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <? endforeach;?> </body> </html> Could anybody please help point out my error?
      Thanks in advance.

    • By gregory
      Hi guys, I need help.
      how do I translate Next Page?
      <?php if($page->next->id) {echo "<div class='float-right'><a class='button' href='{$page->next->url}'> Next Page </a></div>";} ?> I usually use this:
      <?php $lang = $user->language->name; if($lang == 'default') {echo "Next Page";} else {echo "Pagina successiva";} ?> or
      <?php echo __("Next Page"); ?> Thanks
    • By nuel
      Hi there
      Basically I want to call code within a ProcessWire page that isn't used as a template. Example: www.mypwpage.com/myphpfile.php
      I have a working PW Website with a couple of pages like /artists, /releases, /videos etc. Now I need a page /download without any editable fields in the backend, just calling some PHP code (that was coded by another guy) containing a form that checks unique download-codes in a second database and starts the download of the desired file. The script is working fine right now as part of a static website, but since I built PW behind the site, this independent «Download Section» of the page doesn't work anymore.
      Right now I have the main file download.php as a page template on a newly created empty page called /download, so until now the form is working (wow). After sending the form containing the download-code, the file check_code.php in a subfolder /site/templates/download is called and that's where I get an error.
      Any help?
×
×
  • Create New...