Page 3 of 3
Posted: Fri Jun 11, 2004 4:38 am
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!
Posted: Fri Jun 11, 2004 5:29 am
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?
Posted: Fri Jun 11, 2004 6:05 am
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?
Posted: Fri Jun 11, 2004 7:46 am
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?
Posted: Fri Jun 11, 2004 8:13 am
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
Posted: Sat Jun 12, 2004 9:56 am
by Buddha443556
What about <SELECT> and/or Paging? How do you handle these?
Posted: Mon Jun 14, 2004 9:27 am
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.
Posted: Mon Jun 14, 2004 9:32 am
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.
Posted: Mon Jun 14, 2004 9:34 am
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.
Just wondering
Posted: Mon Jun 21, 2004 8:24 am
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.
Posted: Tue Jul 06, 2004 1:18 am
by fastfingertips
Yes
Some posts of mine from this thread were talking about this kind od system, but nobody says nothing
