Skip to content
Advertisement

How do you include multiple page content in one php include file?

I am wondering how would I include various elements (header, footer, nav), in one php file to be included on various pages, rather than creating multiple php files to be called separately?

In my includes file I have the following code:

<?php
echo '<h1 class="sub-elements">The elements for the current submission period are:<span style="font-weight:bold; color:#ff6000;"> Carnival, Residue, Maudlin</span>.</h1>';
?>

Outputting onto an html page that is running php I have:

<?php include('php-includes/current-elements.php');?>

Is there a way to include a particular div or class, something other than the entire file itself?

I just didnt want to create a separate php include file for every little element that i want to include on the same page.

The code below is what I had within the same include file to call a different element to be displayed on another page.

<?php
function elements ()
{
echo'<h1 class="sub-elements">The elements for the current submission period are:<span style="font-weight:bold; color:#ff6000;"> TESTING</span>.</h1>';
elements();
}
?>

Any help would be great!

Advertisement

Answer

It sounds like you are trying to avoid templates. The basic idea is to define a separate file for each of head, footer, nav and include those from your content template.

head.php

<!doctype html> <html> ... 

nav.php

<body> <a href=..> page1 </a> <a href=..> page2 </a> <a href=..> page3 </a>

foot.php

</body> </html>

And to create a complete page, you would do:

<?php
include 'head.php';
include 'nav.php'

An article about prune juice.

include 'foot.php';
?>

Now, if I understand correctly, you want to include only one file and be able to generate the various elements (head, nav, whathaveyou). You could do this by wrapping the include statements in functions:

ubertemplate

<?php
function head() { include 'head.php'; }
function nav() { include 'nav.php'; }

function randomElement() { ?>
    An article about prune juice. <?php
}

function totalymisguideddynamiccontents() {
    echo "<div> foobar <span> moo </span></div>"
}

function() { include 'foot.php'; }
?>

..then in your final page, call these functions:

uberpage

<?php

include 'ubertemplate.php';
?>    

<?php head() ?>
<script src=superlib.js></src>
<?php nav() ?>

Yomama so big when she wears a yellow coat people yell TAXI!. 

<?php foot() ?>

Finally, if I understand correctly and you think the uberpage approach seems like a good idea, …then you should probably try it out. That may well be the only way to realize that it is flawed. I don’t mean to mock your idea, but there are better ways to approach templates and the first way, the one you are trying to avoid, is cleaner (but that’s only my opinion).

For a better way, look at twig templates or do some more research and find a framework that suits your needs.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement