Page 1 of 1
help on making own template engine
Posted: Tue Mar 30, 2004 2:31 pm
by basdog22
hi.
I want to make a template engine and i somehow figured out how it is gonna work.
there will be some .tmp files that will hold the HTML and whenever i want to add something there will be a "#file#" string.
then i will be opening the file and change each occurance of #file# with my content.
It will be something like this:
Code: Select all
<?xml version="1.0" encoding="#iso#"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="#lang#" lang="#lang#">
<head>
<title>#title#</title>
<meta http-equiv="content-Type" content="text/html; charset=#iso#" />
<meta name="copyright" content="Κερασιώτης Βασίλειος" />
<meta name="rating" content="General" />
<meta http-equiv="pics-label" content='(pics-1.1 "http://www.icra.org/ratingsv02.html" comment "ICRAonline EN v2.0" l gen true for "#site_url#" r (nz 1 vz 1 lz 1 oz 1 cz 1) "http://www.rsac.org/ratingsv01.html" l gen true for "#site_url#" r (n 0 s 0 v 0 l 0))' />
<meta http-equiv="expires" content="0" />
<meta name="resource-type" content="DOCUMENT" />
<meta name="distribution" content="#distribution#" />
<meta name="author" content="Κερασιώτης Βασίλειος" />
<meta name="copyrighted" content="Copyright (c) 2004 by Kerasiotis Vasileios" />
<meta name="keywords" content="#keywords#" />
<meta name="description" content="#description#" />
<meta name="revisit-after" content="1 DAYS" />
<meta name="generator" content="Dev-PHP 1.9.4" />
<link rel="stylesheet" type="text/css" href="cartoon/cartoon.css" />
</head>
<body>
<table id="main_table">
<tr>
<td colspan="3" id="header_cell">#header#</td>
</tr>
<tr>
<td id="leftside_cell">#leftside#</td>
<td id="center_cell">#center#</td>
<td id="rightside_cell">#rightside#</td>
</tr>
<tr>
<td colspan="3" id="footer_cell">#footer#</td>
</tr>
</table>
</body>
</html>
Since i want the script to be extensible (right word???) i dont want to do it like this:
Code: Select all
<?php
eregi_replace("#file#","$content",$file);
?>
I would like to make a script that will find all occurances of #.....# and what is betwen them.
So my question is:
How can i search for strings betwen given characters like "##"??
Sorry for the long post

Posted: Tue Mar 30, 2004 3:11 pm
by basdog22
anyone ??

Posted: Tue Mar 30, 2004 3:13 pm
by markl999
I prefere the <title><?php echo $title ?></title> approach personally. Then you can just do stuff like :
$title = 'Home Page';
require_once TEMPLATEDIR.'index.php';
Not sure what the advantages of opening a file and using a regex on it are to be honest

*shrug*
Posted: Tue Mar 30, 2004 3:21 pm
by basdog22
I know this seems weird but this way anyone that wants to create a template will just do:
#extra_file# in the template
Create the extra_file.tmp and the system will ready to parse it immediately
or am i wrong???

Posted: Tue Mar 30, 2004 3:32 pm
by markl999
Well, writing a template engine can be a good exercise so i won't flame them too much even though i don't see a real need for them

But i do think you might be jumping the gun a little bit. Being able to replace #foo# with a value is just a small part of the template engine. How do you propose to handle display logic, for example, a table that lists a row for each value of an array. Eg in PHP you could just do foreach($array as $val){ echo '<tr><td>'.$val.'</td></tr>'; etc..etc. How is your template engine going to give the same functionality with your # syntax ? If you don't work it out now then you might end up in a mess later on

Posted: Tue Mar 30, 2004 3:40 pm
by basdog22
the engine will just parse ## something like this:
Code: Select all
<?php
function get_tmp($page)
{
//Get the tmp file and read the code
$file="$page.tmp";
$fp=fopen($file,"r");
$result=fread($fp,filesize($file));
return $result;
}
function site($center)
{
//get the basic files
$temp=get_tmp("index");
$header=get_tmp("header");
$left=get_tmp("left");
$center=get_tmp($center);
$right=get_tmp("right");
$footer=get_tmp("footer");
//then replace where needed
$temp=eregi_replace("#header#", $header, $temp);
$temp=eregi_replace("#leftside#", $left, $temp);
$temp=eregi_replace("#center#", $center, $temp);
$temp=eregi_replace("#rightside#", $right, $temp);
$temp=eregi_replace("#footer#", $footer, $temp);
$temp=eregi_replace("#iso#", $iso, $temp);
$temp=eregi_replace("#lang#", $lang, $temp);
$temp=eregi_replace("#keywords#", $keywords, $temp);
$temp=eregi_replace("#description#", $description, $temp);
$temp=eregi_replace("#distribution#", $distribution, $temp);
$temp=eregi_replace("#title#", $title, $temp);
$temp=eregi_replace("#theme#", $theme, $temp);
$temp=eregi_replace("#site_url#", $site_url, $temp);
return $temp;
}
?>
So if you assume that the index page is created and the main layout is ok then i can make some fuctions that will be assigning the foreach($array as $val){ echo '<tr><td>'.$val.'</td></tr>'; into a var and then with another
$temp=eregi_replace("#code#", $var, $temp);
I would be able of displaying it on any page
Isn't this right???
Don't hesitate to answer (I am still a php newbie)

Posted: Tue Mar 30, 2004 3:49 pm
by markl999
It's good you're persevering with it and have some code too, not trying to put you off or anything but i can still see a few problems
If you have a function to generate #code# (maybe using a foreach) then you've moved the display logic out of the display code, ie you are now defining how the rows would look and taken it out of the hands of the person using the template. What if the template user want to add some css style and use <td id="foo"> inside the loop for example? If you look at how template engines like
Smarty work then you'll see they provide template 'functions' to loop around arrays, format the output etc..etc..
How many 'features' the engine has depends on who you are targeting it at i suppose. If it's just meant to be a simple engine that can replace #foo# with a value then that's fair enough, but if it's supposed to be a 'full featured' engine then you'll need some way of allowing loops at least ... if that makes sense

Posted: Tue Mar 30, 2004 4:47 pm
by qads
i perfer to use {tag} in the html files and use a php file to parse it. i.e.
users.html
Code: Select all
<table>
<tr>
<!--repeat-->
<td>{username}</td>
</tr>
<!--repeat-->
</table>
users.php
Code: Select all
<?php
$temp = &NEW template();
$html = $temp->get_temp('temps/users.html');
$part = explode('<!--repeat-->', $html);
$temp->page = $part[0];
$query = mysql_query("select username from table limit 100");
while($data = mysql_fetch_array($query))
{
$array['username'] = $data['username'];
$temp->page .= $part[1];
$temp->replace_tags($array);
}
$temp->page .= $part[2];
$temp->output();
?>
now, i could post the code for temp class and its function but thats no fun for you guys

. so i am gona tell you what they do, that should give you a idea about what to do.
class: template();
has all the functions.....
function: get_temp($page_location);
opens the $page_location file and returns the contents.
function: replace_tags($array)
uses a foreach loop and replaces all {array_keys} occurances in the $temp->page, which is a var btw.
function: output();
simple, echo $this->page;
the whole class would look something like
Code: Select all
<?php
class template
{
var $page;
//
function get_temp($loc)
{
$fp = fopen($loc, 'r');
$content = fread($fp, filesize($loc));
return $content;
}
//
function replace_tags($array)
{
foreach($array $tag => $value)
{
$this->page = str_replace('{'.$tag.'}', $value, $this->page);
}
}
//
function output()
{
echo $this->page;
}
//
}
?>
why is this better?
well, with this style/method, you dont need to define the #tags# before, each page will build up on its own allowed you to make #tags# as you go along.
i havet tested this code, i just typed it up, it should give you a idea

.
good luck.
Posted: Tue Mar 30, 2004 5:32 pm
by kettle_drum
You just need to plan it out before you start to write it. To make a true templating system you need to seperate all the code from the template, so that the designer doesnt have to incourperate ANY php as thats not his/her job - so your right to go with the tag option. Make sure the tags that you use are nothing like you would use in a page, so no html, xml, javascript type stuff - your best option is probably something like [tag].
Then you do as others have suggested and open the template file and parse it for the [] and see whats inbetween, i would then have a large switch statment that controls what function should be called for each tag.
Then depending on what you want the site to do, you may need to add more template files, or split the main one into sections, as you need to include a template for other aspects of the page, such as how tables will look or rows of search results etc.
so you may end up parsing the template file for more than one thing. e.g.:
##MAIN PAGE START##
<html>
......
</html>
##MAIN PAGE END##
##SUB TABLE START##
blah
##SUB TABLE END##
##SEARCH RESULTS START##
.......
##SEARCH RESULTS END##
Like so, and you will end up with a part of the template for every section of the webpage. Then its just a case of parsing each section you need for the requested page, and calling any functions needed.
Then you will have all the code and design/html stuff completely seperate, and you can use multiple templates, and allow a team of html coders to work on their code, while you work on yours

Posted: Wed Mar 31, 2004 8:19 am
by basdog22
thanks for replying.
but how do i find the between string?? I mean how do i find "test " from "#test#"???
I am searching for this function...
I think it works like that:
Open temp file
search for #...# paterns and then return the strings between ##'s

Posted: Wed Mar 31, 2004 8:25 am
by patrikG
You'll have to acquaint yourself with regular expressions - which is a bit like learning Latin. While you're learning it, you'll hate it. But once you're over that phase, you're just amazed at how powerful it is. (good starting point for regular expressions is the php manual as well as google)
An easier, and dirtier, option is to use str_replace...
Posted: Wed Mar 31, 2004 3:02 pm
by basdog22
on my way to latin teacher
oops.... i must remember to take my manual together
thanks to all
