Forum Moderators: coopster

Message Too Old, No Replies

Smarty PHP templates

Are they good? Are they suitable for a PHP novice?

         

peterdouglas

9:01 pm on Sep 20, 2005 (gmt 0)

10+ Year Member



I just heard about Smarty PHP templates. After reading up on them, I'm still not exactly sure what they are. Not sure what they mean by a template engine.

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!

maxi million

5:57 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



hi peter

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

maxi million

6:00 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



by the way, if u r wondering what is the final html...it is

<table>
<tr>
<td>
Hi Peter, welcome to webmasterworld.com!
</td>
</tr>
</table>

peterdouglas

6:33 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



Thanks, that makes a bit more sense now. Someone asked if we knew Smarty templates and if we could work on a php website. I don't want to take on something I can't do, and the explanations on google didn't make much sense to me.

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!

FiRe

9:26 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



<?php

//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]

peterdouglas

11:28 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



Looks good. I'm still learning PHP so we'll see how I do!