...

View Full Version : Template Engine?



thesavior
01-03-2006, 03: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.


<html>
<head>
<title>{:pagetitle:}</title>
</head>
...


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:

...viewpage.php?id=3

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?

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

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


<!--/path/to/template.tpl-->
<h3>{C_TITLE}</h3>
<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..


<?php
$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...



<?php
$tpl= file_get_contents('/path/to/template.tpl');
$content = str_replace(
array('{C_TITLE}','{C_BODY}','{C_COMMENTS}'),
array($r['c_title'],$r['c_body'],$r['c_comments']),
$tpl
);
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.

thesavior
01-03-2006, 04: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'].

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

Regards
www.takedom.com

Velox Letum
01-03-2006, 08: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;

thesavior
01-03-2006, 03: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.

Brandoe85
01-03-2006, 04:34 PM
I believe he meant the address in the title:
http://smarty.php.net/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum