Execute PHP with file_get_contents

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Execute PHP with file_get_contents

Post by EcazS »

Long story short, I replace certain "items" in a template.php file with information gathered from a DB.
$HEADER = file_get_contents(HEADER);
$MAIN   = file_get_contents(MAIN);
$FOOTER = file_get_contents(FOOTER);

echo str_replace($array_items, $merged, $HEADER);
echo str_replace($array_items, $merged, $MAIN);
echo str_replace($array_items, $merged, $FOOTER);
where array_items are the items in the template file (e.g. {page_title}) and merged is a merged array with the information from the DB.

This seemed like a fairly good solution until I got a request to make a shoutbox...big problems now.
The only way I though of doing this was to customize the template and do the shoutbox in plain PHP. The ability to customize the templates by adding in your own PHP seems like a fairly good and usable thing. However, echoing the actual contents of the file doesn't work...

So I tried using file_put_contents instead of echoing the str_replace and then including the template files but that means when someone updates the information in the DB they have to remove all the current information in the template by hand and then add the array_items in it again; so this didn't work.

Example template,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
	<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
        <meta name="description" content="{meta_desc}" />
        <meta name="keywords" content="{meta_keys}" />
        <meta name="author" content="{author}" />
        <title>{page_title}</title>
    </head>
    <body>
        <h1>{site_name}</h1>
        <div>
            <h2>{page_header}</h2>
            <p>
                {page_content}
            </p>
        </div>
        <div>
            <p>
                (C) MyCompany.com 2012
            </p>
        </div>
    </body>
</html>
So here I am; asking for help on how to solve this in an efficient way.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Execute PHP with file_get_contents

Post by jacek »

I don't think there is an efficient way really, you either have to use eval() or mess abour with writing to temporary files to then use with include().

How set on your template system are you ? If you replaced you {things} with $t['things'] and included the template files instead of processing their contents it would be a lot faster and involve a lot less messing about.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Execute PHP with file_get_contents

Post by EcazS »

jacek wrote:If you replaced you {things} with $t['things'] and included the template files instead of processing their contents it would be a lot faster and involve a lot less messing about.
That does seem like a good idea... I just don't know how I would set that up.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Execute PHP with file_get_contents

Post by jacek »

you have a header file for each teplate file that defines the template variables.

index.head.inc.php
<?php

$t['page_title'] = 'My Cool Page';
$t['menu_items'] = array('other_page.html', 'and_things.html');

?>
then in your main file (index.php or init.inc.php)
<?php

// boring normal stuff

include('index.head.inc.php');
include('index.page.inc.php');

?>
then on the page you can do
<title><?php echo $t['page_title']; ?></title>
It's the system I use for my forum :)
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Execute PHP with file_get_contents

Post by EcazS »

Alright, I see what you mean.

Since I already grabbed the content from the database and placed it in an array all I had to do was echo it out in the template and remove the unnecessary stuff I had before :lol:

This is definitively a better way of doing it, don't know why I never thought of doing it like this :lol:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Execute PHP with file_get_contents

Post by jacek »

It's a LOT faster, but the standard is to use things like {this} because it's less php code for people that don't know php to have to work out.
Image
Post Reply