Forum Moderators: coopster
I've got some BASIC PHP knowledge (used PHP includes, date function, etc, nothing fancy). To be honest, I'm more of a designer than a programmer. Could someone explain in plain English what Smarty PHP templates are and if they'd be suitable for someone like me?
Thanks!
there may be many people who ud be able to give you a more learned description/evaluation/introduction to smarty...but you want an explanation in plain english..so here it is...
whether to use smarty or not depends on many things...first of them all is whether or not ur boss tells u to! jokes apart, smarty does this: it separates the design/presentation elements of the site from the program/content elements.
so a programmer writes cumbersome programs without bothering in the least, if at all the output can be displayed in ameaningful manner. thats where smarty helps him out..he simply passes the output out to a physical template file which displays the output.
a designer only needs to design the basic structure of the webpage without the content, and the content gets included here and there. pls note include here does not refer to php include() function.
typically its like this:
in the program file you write something like,
$name = "Peter";
$sitename = "webmasterworld.com";
and assign $name and $sitename to a related tpl file.
im not going into the exact syntaxx of how you "assign", as that would invoke discussions on using the smarty object.
the tpl file has this
<table>
<tr>
<td>
Hi {$name}, welcome to {$sitename}!
</td>
</tr>
</table>
hope this clears some of your doubts. now whether it ud be suitable for u is another qstn. depends on what u want and how far prepared are u to exlore this side of php, but my suggestion try it out. theres no harm in trying to learn something new!
cheers
I'd rather practice working with Smarty templates a bit first, before taking on a project, but it sounds like something I'd like to learn. Still need to get more comfortable with working in php in general.
Thanks again!
//This class was developed by FiRe
class Template {
var $template;
function publish($file) {
$this->template = file_get_contents($file);
foreach ($GLOBALS as $key => $value) {
$this->template = str_replace('{' . $key . '}', $value, $this->template);
}
eval("?>" . $this->template . "<?");
}
}
?>
This is a little class I wrote similar to smarty but smaller and easier. Put that code in a file called "template.php" and then you can do similar things.
Example Usage:
File: home.tpl
<html>
<body>
Welcome {name} today is {day}!
</body>
</html>
File: home.php
<?php
//All variables must be preset and will appear in replace of {variablename} in the tpl file
$name = "bob";
$day = "sunday";
include("template.php");
$tp = new Template;
$tp->publish('/path/to/home.tpl');
?>
Now if you went to home.php you would see:
<html>
<body>
Welcome bob today is sunday!
</body>
</html>
Hope that helps :)
[edited by: jatar_k at 10:44 pm (utc) on Sep. 21, 2005]
[edit reason] no urls thanks [/edit]