iTom
04-01-2007, 06:52 PM
PHP is amazing. If you want to get to know it, see the website (http://www.php.net/) (php.net). It's a web scripting language with so many possibilities. One of these possibilities is templates. You can make easy templates, backing them with a database using MySQL and many other things. I bring you: SimpleTemplates!
What's the deal with SimpleTemplates?
SimpleTemplates was an idea I clocked onto about a month ago. It struck me like a penny falling from the top of the Empire State building. I needed a way to do substitution with variables and implement certain tags, like <st:if> or something to show data, without using PHP tags, because they open a security hole. Plus, in contrast, usually the templates are far smaller.
In all simplicity the system works by using eval(), addslashes() and a few other clever functions. As you probably know, $variables in strings are replaced by the respected values. I decided use this to my advantage. I can also do some replacing to get the <st:if> tags working.
In these tutorials I will be teaching you how to create a SimpleTemplates (ST) script and a template, then we will be going onto <st:if> (and maybe other control structures!) and then back it up with a database.
For example, you could:
<html>
<head>
<title><?php echo $blahtitle; ?></title>
</head>
<body>
<?php echo $blahtext; ?></body>
</html>
But it would be more appealing, secure, smaller and easier to use the following code:
<html>
<head>
<title>{$blahtitle}</title>
</head>
<body>
{$blahtext}
</body>
</html>
(Some members will point out the brackets around variables in strings are not neccessary, but I like to code variables in my templates like that. In code or in other strings I don't normally do that. This makes them stick out much more and potentially means a highlighting script for templates like the one above would be easier to make)
Considering that $blahtitle might be "Hello, world!" and $blahtext might be "<b>Hello, world!</b>", both would generate the same HTML (when combined with ST), but the length and ease of coding is significantly reduced.
<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<b>Hello, world!</b>
</body>
</html>
In the next post I will be explaining how to do this.
What's the deal with SimpleTemplates?
SimpleTemplates was an idea I clocked onto about a month ago. It struck me like a penny falling from the top of the Empire State building. I needed a way to do substitution with variables and implement certain tags, like <st:if> or something to show data, without using PHP tags, because they open a security hole. Plus, in contrast, usually the templates are far smaller.
In all simplicity the system works by using eval(), addslashes() and a few other clever functions. As you probably know, $variables in strings are replaced by the respected values. I decided use this to my advantage. I can also do some replacing to get the <st:if> tags working.
In these tutorials I will be teaching you how to create a SimpleTemplates (ST) script and a template, then we will be going onto <st:if> (and maybe other control structures!) and then back it up with a database.
For example, you could:
<html>
<head>
<title><?php echo $blahtitle; ?></title>
</head>
<body>
<?php echo $blahtext; ?></body>
</html>
But it would be more appealing, secure, smaller and easier to use the following code:
<html>
<head>
<title>{$blahtitle}</title>
</head>
<body>
{$blahtext}
</body>
</html>
(Some members will point out the brackets around variables in strings are not neccessary, but I like to code variables in my templates like that. In code or in other strings I don't normally do that. This makes them stick out much more and potentially means a highlighting script for templates like the one above would be easier to make)
Considering that $blahtitle might be "Hello, world!" and $blahtext might be "<b>Hello, world!</b>", both would generate the same HTML (when combined with ST), but the length and ease of coding is significantly reduced.
<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<b>Hello, world!</b>
</body>
</html>
In the next post I will be explaining how to do this.