----- Original Message ----- 
From: "ravee kumar" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Friday, June 01, 2007 10:49 PM
Subject: [php-list] how to use templates


can any one guide me to how to use templates in php.

ravi
---------------------------------
Hi Ravee,
               This depends on how complex the web site is to be and 
therefore the number of templates and different meta tags ect.

The simple way is to place your template in folder call templates and have a 
tag in the template that represents where the content goes. This is not very 
usefull.

So your PHP to generate the page looks something like this -

<?php

function get_template($name)
  {
  $template = file_get_contents("templates/" . $name);
  $header = substr($template, 0, strpos($template, "[template]"));
  $footer = substr($template, strpos($template, "[template]") + 10);
  // add 10 to get past the text [template]
  $result["header"] = $header;
  $result["footer"] = $footer;
  return $result;
  }

$template = get_template("common_pages.tmp");

echo($template["header"]);

// Do all your PHP to generate the page content here

echo($template["footer"]);

?>

The other way is when you want to place many elements into a template.

<?php

$page_name="contact_us";
$template = "common_pages";

function get_page($page_name, $template)
  {
  $sub_parts =  "[title]|ttl [meta]|mta [navbar]|nav [breadcrumbs]|brd 
[loginbox]|log [content]|htm";
  $temp = explode(" ", $subparts);
  foreach($temp as $part)
    {
    $text = substr($temp, 0, strpos($temp, "|"));
    $extention = substr($temp, strpos($temp, "|") + 1);
    $sub_parts[$extention] = $text;
    }
  $html = file_get_contents("templates/" . $template . ".tmp");
  foreach($sub_parts as $extention => $text)
    {
    $file_name = "pages/ . $page_name . "." . $extention;
    if (file_exists($file_name) && is_readable($file_name))
      {
      $temp = file_get_contents($file_name);
      $html = str_replace($text, $temp, $html);
      }
    else
      {
      $file_name = "pages/ . $page_name . "." . "def"; // def for default
        if (file_exists($file_name) && is_readable($file_name))
        {
        $temp = file_get_contents($file_name);
        $html = str_replace($text, $temp, $html);
        }
      }
    }
  }

$page = get_page($page_name, $template);

echo($page);
?>

PS: I have a templing system here that allows you to edit and have php in 
the subparts. I can send it on if you want offlist. 

Reply via email to