Re: Including files for templates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxx> 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux