Including files for templates

This is a discussion on Including files for templates within the PHP General forums, part of the PHP Programming Forums category; I'm trying to write a template system, my template is the HTML layout, and my content is fetched from ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-23-2008
Terry Burns-Dyson
 
Posts: n/a
Default Including files for templates

I'm trying to write a template system, my template is the HTML layout, and
my content is fetched from another source. However I don't quite understand
how to output the template so that all the variables are parsed by PHP.
Simple version of what I'm trying to do;

ob_start( );

extract( $params, EXTR_PREFIX_SAME, "am_");

$pageContent = file_get_contents( "page_to_display.html");

include( "template.html");

echo ob_get_clean( );


$pageTitle is in the template, it's replaced, $pageContent is in the
template, it's replaced. But any variables within the page_to_display are
simply output into the page rather than processed by PHP. I realise that
file_get_contents is basically returning a string and I"m just doing string
replacement when I include the template.html, so my only question is, what's
the actual way of doing this?

Thanks

Reply With Quote
  #2 (permalink)  
Old 03-23-2008
Daniel Brown
 
Posts: n/a
Default Re: [PHP] Including files for templates

On Sun, Mar 23, 2008 at 6:32 AM, Terry Burns-Dyson
<hammerstein_02@msn.com> wrote:
[snip!]
> $pageTitle is in the template, it's replaced, $pageContent is in the
> template, it's replaced. But any variables within the page_to_display are
> simply output into the page rather than processed by PHP. I realise that
> file_get_contents is basically returning a string and I"m just doing string
> replacement when I include the template.html, so my only question is, what's
> the actual way of doing this?


The simples way would be like so:

<?php
// content/page1.php
#
# Define the variables for the content of the page.
#


$page_title = "Welcome To My Website!";

$paragraph_a =<<<EOT
Welcome to my website!

This is a HEREDOC that we'll be using in the template to display some
text, formatted with HTML line breaks and all.

It's that simple!
EOT;

$paragraph_a = nl2br($paragraph_a);

$main_image = "images/main.png";

$main_image_link = "http://www.other-website.tld";
?>

<?
// templates/page1.php
#
# The formatting for the page.
#


/* All of your headers and starting
HTML should be included by your
switching script. This file will also
require that short_open_tags is on.
If you can't/don't want it on, just
modify the tags herein. */
?>
<div>
<?=$paragraph_a;?>
</div>
<div><a href="<?=$main_image_link;?>"><img src="<?=$main_image;?>"
border="0"></a></div>
<? // End of templates/page1.php ?>

<?php
// templates/header.php
#
# A very basic sample header.
#


?>
<html>
<head>
<title><?=$page_title;?></title>
</head>
<body>
<?php // End of templates/header.php ?>

<?php
// templates/footer.php
#
# A very basic sample footer.
#


?>
</body>
</html>
<?php // End of templates/footer.php ?>

<?
// index.php
#
# A simple index switch page.
#


if(isset($_GET['s']) && strlen($_GET['s']) > 1) {
switch($_GET['s']) {
case "page1":
$section = $_GET['s'];
break;
case "contact":
$section = $_GET['s'];
break;
case "about":
// We'll pretend the page name was changed here.
$section = "about_us";
break;
default:
$section = "home";
break;
}
} else {
$section = "home";
}

include('content/'.$section.'.php');
include('templates/header.php');
include('templates/'.$section.'.php');
include('templates/footer.php');
/* This means that the content file with the
variables is parsed first, and defines the
variables within the scope of this execution.
It's included before the header.php file so
that the $page_title variable is defined. */
?>


Everything herein was typed directly into this email and is
untested, so it's by no means guaranteed to work, has not been
properly sanitized or tested, and is for informational purposes only,
to get you started in the right direction.

--
</Daniel P. Brown>
Forensic Services, Senior Unix Engineer
1+ (570-) 362-0283
Reply With Quote
  #3 (permalink)  
Old 03-24-2008
Philip Thompson
 
Posts: n/a
Default Re: [PHP] Including files for templates

On Mar 23, 2008, at 12:10 PM, Daniel Brown wrote:
> On Sun, Mar 23, 2008 at 6:32 AM, Terry Burns-Dyson
> <hammerstein_02@msn.com> wrote:
> [snip!]
>> $pageTitle is in the template, it's replaced, $pageContent is in the
>> template, it's replaced. But any variables within the
>> page_to_display are
>> simply output into the page rather than processed by PHP. I
>> realise that
>> file_get_contents is basically returning a string and I"m just
>> doing string
>> replacement when I include the template.html, so my only question
>> is, what's
>> the actual way of doing this?

>
> The simples way would be like so:
>
> <?php
> // content/page1.php
> #
> # Define the variables for the content of the page.
> #
>
>
> $page_title = "Welcome To My Website!";
>
> $paragraph_a =<<<EOT
> Welcome to my website!
>
> This is a HEREDOC that we'll be using in the template to display some
> text, formatted with HTML line breaks and all.
>
> It's that simple!
> EOT;
>
> $paragraph_a = nl2br($paragraph_a);
>
> $main_image = "images/main.png";
>
> $main_image_link = "http://www.other-website.tld";
> ?>
>
> <?
> // templates/page1.php
> #
> # The formatting for the page.
> #
>
>
> /* All of your headers and starting
> HTML should be included by your
> switching script. This file will also
> require that short_open_tags is on.
> If you can't/don't want it on, just
> modify the tags herein. */
> ?>
> <div>
> <?=$paragraph_a;?>
> </div>
> <div><a href="<?=$main_image_link;?>"><img src="<?=$main_image;?>"
> border="0"></a></div>
> <? // End of templates/page1.php ?>
>
> <?php
> // templates/header.php
> #
> # A very basic sample header.
> #
>
>
> ?>
> <html>
> <head>
> <title><?=$page_title;?></title>
> </head>
> <body>
> <?php // End of templates/header.php ?>
>
> <?php
> // templates/footer.php
> #
> # A very basic sample footer.
> #
>
>
> ?>
> </body>
> </html>
> <?php // End of templates/footer.php ?>
>
> <?
> // index.php
> #
> # A simple index switch page.
> #
>
>
> if(isset($_GET['s']) && strlen($_GET['s']) > 1) {
> switch($_GET['s']) {
> case "page1":
> $section = $_GET['s'];
> break;
> case "contact":
> $section = $_GET['s'];
> break;
> case "about":
> // We'll pretend the page name was changed here.
> $section = "about_us";
> break;
> default:
> $section = "home";
> break;
> }
> } else {
> $section = "home";
> }
>
> include('content/'.$section.'.php');
> include('templates/header.php');
> include('templates/'.$section.'.php');
> include('templates/footer.php');
> /* This means that the content file with the
> variables is parsed first, and defines the
> variables within the scope of this execution.
> It's included before the header.php file so
> that the $page_title variable is defined. */
> ?>
>
>
> Everything herein was typed directly into this email and is
> untested, so it's by no means guaranteed to work, has not been
> properly sanitized or tested, and is for informational purposes only,
> to get you started in the right direction.


Here is another way (same disclaimer as above):

<?php
$replace = array(
'REPLACE_ME_1' => $replaceMe1,
'REPLACE_ME_2' => $replaceMe2,
'REPLACE_ME_3' => $replaceMe3,
'REPLACE_ME_4' => $replaceMe4,
);

$template = file_get_contents ('templates/somePage.tpl.php');
$somePage = str_replace (array_keys($replace), array_values($replace),
$template);
?>
<html>
<head><title>Contents of some page</title></head>
<body>
<div id="somePage"><?php echo $somePage; ?></div>
</body>
</html>

Happy coding!

~Philip
Reply With Quote
  #4 (permalink)  
Old 03-24-2008
Eric Butera
 
Posts: n/a
Default Re: [PHP] Including files for templates

On Sun, Mar 23, 2008 at 7:32 AM, Terry Burns-Dyson
<hammerstein_02@msn.com> wrote:
> I'm trying to write a template system, my template is the HTML layout, and
> my content is fetched from another source. However I don't quite understand
> how to output the template so that all the variables are parsed by PHP.
> Simple version of what I'm trying to do;
>
> ob_start( );
>
> extract( $params, EXTR_PREFIX_SAME, "am_");
>
> $pageContent = file_get_contents( "page_to_display.html");
>
> include( "template.html");
>
> echo ob_get_clean( );
>
>
> $pageTitle is in the template, it's replaced, $pageContent is in the
> template, it's replaced. But any variables within the page_to_display are
> simply output into the page rather than processed by PHP. I realise that
> file_get_contents is basically returning a string and I"m just doing string
> replacement when I include the template.html, so my only question is, what's
> the actual way of doing this?
>
> Thanks
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


I used to use this extract/include methodology before I really thought
about the performance aspect of it. I would create a template object
that could have variables injected into it and then tell it to
render(). The render process would extract everything in the data
array & include a template file.

After I found out about xdebug I stopped doing this.

Now my approach is more in line of the Zend_View[1] template engine.
Just a generic class to define the path to the template, a way to put
variables into it, and a render function. The big difference this
time is that instead of extract I just use $this-> inside the
template.

[1] http://framework.zend.com/manual/en/zend.view.html
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:03 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0