Re: templates with php
| From: | Chris Monson | Date: | Mon, 31 Jul 2000 17:12:18 +0000 |
| Subject: | Re: templates with php | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9202@lists.php.net to get a copy of this message | ||
I honestly can't imagine why people who use PHP seem to insist on wanting a separate templating engine. PHP does templates just fine. It works fine doing it the way that Rasmus suggests, and it also works in a more CGI-based approach:
Suppose I have a PHP script that displays different pages based on some parameters passed in. These pages are really HTML templates, so it needs to fill in values before displaying them.
That's easy! Just include the pages after setting the appropriate variables.
Example:
switch( $mode ) {
case 'index': do_index(); break;
case 'other': do_other(); break;
}
function do_index()
{
$title = "This is the Index Title";
$name = "Me";
include( 'index.html' );
}
funciton do_other()
{
$title = "Other Title";
$name = "you";
include( 'other.html' );
}
And then your index.html file could look something like this:
<html>
<head>
<title><?=$title?></title>
</head>
<body>
Page for <?=$name?><br>
This is the index page
</body>
</html>
The 'other.html' file could be similar. The cool thing about this is that if you have very long lists of things, or the page needs to make a lot of queries to fill up, you can call functions from those pages, you can access objects, variables, looping constructs, etc. PHP does great as a templating language if that is the paradigm you want to follow. Adding a separate template parser is really bad overkill for something like this.
Additionally, PHP was designed with the cool "split" way of handling loops and other block constructs (it works with standard braces, too, but the split way of doing it is much more clear, especially to an HTML designer that has to work around the code):
<? foreach $blah as $b:?>
Repeated HTML code here: $b<br>
<?endforeach;?>
Good luck.
Chris
carlos@ifx.com.br ("Carlos Henrique") wrote:
Hi there! I'm starting a new project from scratch and I my idea is do it with html templates. What I mean is a directory with some templates like index.html, details.html, checkout.html, etc, which one with symbols in it, like @title@, @price@, @description@, etc... When the page is requested some kind of pre-processor reads variables and show the dynamic page. My question is how can I do that with php? What's the best way? Regards Carlos -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net