Templating Engine

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

Sycor
Forum Newbie
Posts: 8
Joined: Fri Jun 11, 2004 3:54 am

Post by Sycor »

The subject of templates is quite complex, and until recently I had never tried my hand at a system for replacing flags/markers within a template with preset or dynamic variables. Having recently set up MySQL with my Apache/PHP local server, I chose what seemed to me to be the most sensible method for a simple template system.

Using MySQL, I created a simple table with only two columns: variable and value. Into this table I can simply place the name of the marker ("login_form", "name.of.pet") and it's value.

I then use a similar script to the following:

Code: Select all

<?php

$conn = db_connect();
$query = "SELECT * FROM variables";
$result = mysql_query($query, $conn);
$template = load_template();

while ($row = mysql_fetch_array($result))
{
$variable = "{" . $row['variable'] . "}";
$value = $row['value'];
$template = str_replace($variable, $value, $template);
}

echo $template;

?>
I'm not sure that this will work, as I coded it on the spot, but this is essentially what my system does.

It is easy to expand this script to include categories, so instead of having to use "{style_td_header}" you could use "{style:td_header}" or even "{style:td:header}".

It's been an interesting read so far. I hope this thread is kept up!
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Simple variable substitution is quite easy and I'm sure your method works well for simple layouts. But what happens for 'repeatable blocks' (as they are generally reffered to in templates)?

i.e. From previous examples, you query your database which returns a list of your users with their email addresses, how would you use your template system to generate the required output for a user list?
Sycor
Forum Newbie
Posts: 8
Joined: Fri Jun 11, 2004 3:54 am

Post by Sycor »

For repeatable blocks, three pieces of data would have to be supplied. The code encasing the blocks (DIV or TABLE), the code for each block (TR and TD) and the code ending the encasement. The data source (the user's database table) and user markers (for name and email) will have been customised beforehand, so all that's left is to write the "pre" code, loop through the block code and substitute variables, and write the "post" code.

Or am I wrong?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I'm not entirely sure I follow you, but I think you are suggesting a similar method to LiLpunkSkateR's method? If that is the case then I wouldn't consider it to be a complete template system (for the reasons I have mentioned previously).

If that's not the case, then I have picked you up wrong so, care to elaborate?
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Sycor may i remind you that <table> <tr> <td> structures are a little bit deprecated and this kind of structures are not recommended by the SEO standard (Search Engine Optimisation).

In this way your site will have future problems in search engines visibility
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

What about <SELECT> and/or Paging? How do you handle these?
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

The select is easy to manage in my template engine, but the paging system not.
At this level of developing i'm making the paging effect by using JavaScript and <div> structure.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

To give you some details, my content arhitecture allows me to create categories and articles, every category or article may have a display if i have defined for it a template.

Every article is able to contain:
- text positions, i my case i'm using the following syntax to get a text position: <engine:articleText pos="1" artId="23"/> if i do not define the artId then the present article is set as text source.
- images <engine:imageId pos="1" artId="12"/>
- and forms. Forms is something like a database table and allows me to store info in fields and rows.

In this way every user of my engine is able to creates his categories, templates etc with just few knowledge of HTML. The tags that looks like XML are parsed into php functions by the server.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Also sometimes i need to take all text from an article so i have defined the "posAll" argument, <engine:articleText posAll artId="23"/> and in this way i'm taking all content from the article.
matlin
Forum Newbie
Posts: 1
Joined: Mon Jun 21, 2004 8:24 am

Just wondering

Post by matlin »

Why has no one mentioned making e template engine from XML/XSLT. Like many Java based portals and such do.
That realy keeps the code away from the presentation.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Yes

Some posts of mine from this thread were talking about this kind od system, but nobody says nothing :evil:
Post Reply