Dynamic content and templating

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

My pereferred method.... using Heredoc:

Code: Select all

<?php

$Template->GetHTTPHeaders();

$HTML = <<<HTMLEND
<!DOCTYPE blah blah....
  // >
<html>
<head>
<title>{$Template->GetTitle()}</title>
<style type="text/css">
@import "your_standard_css_stff.css"
{$Template->GetCSSExtra()}
</style>
<script type="text/javascript">
<!-- Hide
some_js_functions()
{
  //
}
{$Template->GetExtraJS()}
//End -->
</script>
</head>
<body{$Template->GetOnLoad()}>
<div id="page_container">
  <div id="whetever">
    Whatever other standard stuff is in your template
  </div>
  {$Template->GetBody()}
</div>
{$Template->GetFooter()}
</body>
</html>
HTMLEND;

echo $HTML; //Print back to browser

?>
Basically all the HTML looks and feels like HTML to everyone excluding the dynamic bits that can't really be fiddled with to any great extent. Everything dynamic is pre-computed and just added to the template as it's produced.

Heredoc is very clean and nice for others to edit without getting too mixed up in PHP code and it saves on all that Regex stuff... just my pereference :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

For such simple templates I would rather have plain HTML files for the designers to work with like:

Code: Select all

<!DOCTYPE blah blah....  // >
<html>
<head>
<title>{Title}</title>
<style type="text/css">
@import "your_standard_css_stff.css"
{CSSExtra}
</style>
<script type="text/javascript">
<!-- Hide
some_js_functions()
{
  //
}
{ExtraJS}
//End -->
</script>
</head>
<body {OnLoad}>
<div id="page_container">
  <div id="whetever">
    Whatever other standard stuff is in your template
  </div>
  {Body}
</div>
{Footer}
</body>
</html>
And then use a simple str_replace() Template class like:

Code: Select all

<?php

$Model = new MyModel();

$Template = new Template('myfile.html');

$Template->set('Title', $Model->getTitle());
$Template->set('CSSExtra', $Model->getCSSExtra());
$Template->set('ExtraJS', $Model->getExtraJS());
$Template->set('OnLoad', $Model->getOnLoad());
$Template->set('Body', $Model->getBody());
$Template->set('Footer', $Model->getFooter());
echo $Template->render(); //Print back to browser

?>
It gives you clean separation between the presentation and the code. The template HTML is editable in any editor. And importantly, you don't have to trust the template designer because they have no access to the PHP engine. Plus, if you want to generate XML or CLI output you just edit the template, not the code.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

I guess I'll drop another question regarding the McGruff's suggested method. :)

The way I understand it, the PHP code which replaces the custom tags should be as simple as possible, that is, it only outputs the collected data. So that would be simple constructions such as while, for, if, echo etc. Now I was wondering where do I define the code? My idea now is to have templates of simple PHP code structures, like this (while example):

Code: Select all

<?php

while({condition})
{
  echo '{stuff}';
}

?>
The above would seem like a straightforward way to construct simple while to echo some results from db. {condition} is replaced by what you need, {stuff} as well (the formatting for the latter will be gathered from .htm template). It also looks like a clean way, no need to write any while syntax: just get the while template, put the condition you need as well as stuff you need echoed, and replace your tags with the whole combination in .htm and write a new .php.

I've already tried the McGruff's method, wrote a regex class to read/replace stuff in .htm templates and I succeeded (actually that was my first regex try :D). So now I have 'definers' for each .htm template, and it's enough to run a single php script once to get all tags replaced in each .htm template and result written into new .php files.

Tell me what you think regarding the above idea, it's not that much thought about, so you might be able to indicate the reasons why I shouldn't use it.

How do you construct the PHP code you need the tags replaced with? Where do you store it/gather it from?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Yes that's the general idea. A few simple presentational functions might also be handy.
Post Reply