View Full Version : Template Engine?

01-03-2006, 02:31 AM
I am looking for something that will do the following.

I have a html document that i can do the normal template things.


Then i want to have a file that lists things like

pagetitle --> $result['tuturl']

What is different from other ones of these that i have found online, is that instead of replacing it with static content like

pagetitle --> THIS IS A PAGE TITLE

Im using something that goes like:

$query="SELECT * from tutorials WHERE id='$_GET[id];'";
$row=mysql_query($query) or die(mysql_error());
$result = mysql_fetch_array($row);

and so lets say i go to a page with a url like:


it opens the html file for viewpage, and parses the viewpage template which is like that of my first code above, and takes the tuturl and puts it into the title...

Might sound like any template script, except i want mine this to be dynamic depending on the id in the url and the like.

Does anyone know of one like this, or script that i can work with?
Or if not...then just plain out...how do i do this?

01-03-2006, 03:09 AM
Hi, templating is usually at least a 2 part process, often 3.

e.g. You have the actual template ..e.g.

<span class="para">{C_BODY}</span>
<span class="para">{C_COMMENTS}</span>

then a routine for gathering the data to replace which might for example look like this..

$sql = mysql_query("
SELECT c_title,c_body,c_comments
FROM $table
WHERE id='{$_REQUEST['id']}'");
$r = mysql_fetch_assoc($sql);

then you can either just parse the tempate there and then, OR pass the data ($r) to a templating function or class.. the former...

$tpl= file_get_contents('/path/to/template.tpl');
$content = str_replace(
echo $content;

how far you go with templating is another matter , my personal template class has simple IF and INCLUDE support and the ability to iterate through sections and auto-replace certain constants , some templating systems introduce nearly thier own language which I personally think is counter-productive but each to thier own.

Anyways, thats the basics.

01-03-2006, 03:24 AM
ok, i think i can use that, now, to make it easy to add things, how can i do a simple looking thing like:

{:title:} => .$row['title'].
{:author:} => .$row['author'].
{:cata:} => .$row['category'].
{:date:} => .$row['date'].

01-03-2006, 07:01 AM
Hello i think you are looking for some template engine.
If you have some time you can look at above address. :thumbsup:


Velox Letum
01-03-2006, 07:39 AM
ok, i think i can use that, now, to make it easy to add things, how can i do a simple looking thing like:

{:title:} => .$row['title'].
{:author:} => .$row['author'].
{:cata:} => .$row['category'].
{:date:} => .$row['date'].

$content = file_get_contents('tpl/page.tpl');
$content = str_replace('{:title:}', $row['title'], $content);
$content = str_replace('{:author:}', $row['author'], $content);
$content = str_replace('{:category:}', $row['category'], $content);
$content = str_replace('{:date:}', $row['date'], $content);
echo $content;

01-03-2006, 02:54 PM
zombbb...that is a website selling hosting, i don't want hosting, i want to know how to make one of these on my site...i didn't see what im looking for anywhere there.

and thanks velox, ill use that.

01-03-2006, 03:34 PM
I believe he meant the address in the title: