Page 1 of 2

To use SMARTY?

Posted: Thu Feb 16, 2006 9:02 pm
by JMichaels22
Hey all,

Im pretty new to PHP and just starting to really study and get familar with the code. I was wondering if I should follow the instruction of one of the books I bought and code everything with SMARTY or should I just get standard PHP programming down?

I see the value of seperating the PHP code from HTM, but dont want to get started with a language on the wrong foot?

What are your thoughts?

Thanks again and look forward to contributing to this forum!

-Jassen

Posted: Thu Feb 16, 2006 9:09 pm
by Roja
Personally, I find it considerably easier to code with Smarty.

By doing so, I can keep the output (the html) seperate from my processing instructions.. makes it easier to read my code, and a LOT easier to debug/fix my output (like to get html compliance, change layout, etc).

Its really simple to use smarty, and it teaches a good programming habit.

Posted: Thu Feb 16, 2006 9:13 pm
by John Cartwright
I personally avoid using smarty, simply because I don't believe in the whole "learn a new language for templating" smarty syntax style. I don't mind that much about these kinds of things.. just those darn designers don't know what the hell smarty is :P and I sure don't have time to teach them.
I do however use a, shall I dare say similar setup with the exception of it being much simpler.

Smarty is however the ideal solution for many many developers, considering they do have such great features.. caching , etc.

Posted: Thu Feb 16, 2006 9:25 pm
by Christopher
Most importantly, you want to put your presentation code into a layer separate from your data model. But that means that that presentation layer has more that just template code in it, because something has to control the template system and manage connecting the data model to the template. The problem with super duper templating systems like Smarty is that they often lead to combining non-template presentation code with the data model into a Transaction Script. That is an architecture that, in my experience, leads to a lot of code replication.

The leap from Transaction Script based designs and Domain Model based designs is probably the most difficult for programmers to make.

I would suggest creating the an architecture that has a clear Presentation/Model separation. Design a solid presentation layer and then select the lightest weight template system that will support your needs. There are many around and several different styles. But don't start with the template system and build the rest of the system around it.

Posted: Thu Feb 16, 2006 10:51 pm
by Gambler
PHP itself has a built-in template engine. That is why each script starts with <?php. Since it's already there, I believe programmers should use it, unless they have really good reasons not to.
Smarty is however the ideal solution for many many developers, considering they do have such great features.. caching , etc.
Actually, it's one of the reasons I would advise against using Smarty. It tries to do everything and it's bloated. Caching, for example, is in no way related to presentation logic.

Posted: Thu Feb 16, 2006 11:17 pm
by namitjung
Using Smarty is all depends on your choice. When I was new to the php i didn't use Smarty.If you are already familiar with other server side scripting language take a short time to look at php functions,explore it and you can find Smarty is really good choice. You can always get latest php information from http://www.php.net. But if php is your first scripting language i don't recommend to implement Smarty Template Engine to get start with php. Take Some time to get familar with php and go for Smarty


Happy Programming :D

Posted: Thu Feb 16, 2006 11:33 pm
by josh
I write my apps like this


Output goes into the "template" object (you can calll it MVC if you want to),

the template file has PHP script embedded directly in it, for instance

Code: Select all

<div id="menu">
<?php foeach( $this -> menu as $menu ) echo $menu; ?>
</div>

the template file just gets included inside the "generate" method of the template object, which gets called at the end of each page. Since I already know PHP and it already exists I'd rather use that then 'invent'/learn some scripted templating engine like gambler said.

Posted: Thu Feb 16, 2006 11:50 pm
by namitjung
Go for your choice.

Posted: Fri Feb 17, 2006 12:33 am
by John Cartwright
jshpro2: How would you handle conditions? Would those also be in the template file?

Posted: Fri Feb 17, 2006 1:26 am
by matthijs
My preference is to use only php. I would not like to learn another language on top of that. A cms like wordpress handles the presentation logic like this:

Code: Select all

<?php
foreach($posts as $post) :
?>
   <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
   <p><?php the_content(); ?></p>
<?php endforeach; 
?>
I find that very easy to work with.
I don't see how something like this would be better:

Code: Select all

{foreach(#posts as #post) :
}
   <h2><a href="{ #the_permalink(); }" id="post-{ #the_ID(); }">{ #the_title(); }</a></h2>
   <p>{ #the_content(); }</p>
{ endforeach; }
Maybe a seperate template language is only useful in a situation when the templates are designed by others, which you don't trust with php. (they do have to learn the template language..)

I know there are/have been some heated debates about this. In the end it's someone's own choice what to use, depending on a lot of factors. A couple of interesting reads I found:
http://www.phppatterns.com/docs/design/ ... te_engines and http://www.massassi.com/php/articles/template_engines/

If you do want to use a template language, I've heard some good things being said about savant

Posted: Fri Feb 17, 2006 3:58 am
by Maugrim_The_Reaper
Focus on the PHP principles first, then come to grips with using standard libraries like Smarty, or ADOdb (database connectivity). It's hard to use a library effectively without knowing what exactly it does, and how it does it - and the only way you'll understand that is to know the basics - even if it is sticking everything into a huge single PHP file.

Just keep in mind your first coding sucesses can be bettered once you start looking at Object Oriented programming, using libraries, etc.

On the PHP vs Templating Lib debate, Smarty is widely accepted. It may not be ideal, but it does offer a standard interface that many people will be familiar with. Lower entry barriers... For a pure PHP solution maybe someone has used Savant?

Posted: Fri Feb 17, 2006 5:09 am
by Jenk
I've not used myself, but have seen a package that uses PHP and only PHP for all it's layers, with the 'simple' separation from presentation and business/data logic like the below:

Code: Select all

?>

<div class="DataContainer">
<?php 
if ($content->rowCount('tbl_MachineInfo') > 0) {
    echo '<table class="tblMachineInfo">' . "\n";
    echo $content->getHTMLTableHeader('tbl_MachineInfo') . "\n";
    
    while ($content->hasNextRow('tbl_MachineInfo')) {
        echo $content->getHTMLTableRow('tbl_MachineInfo') . "\n";
    }

} else {
    echo 'No data to display';
}
?></div>
Whilst I am quite experienced with Smarty, I dislike the idea of creating a new syntax when we have a perfectly viable one that is already in use. It just screams 'overheads' at me using an interpreted language (PHP) to parse a user made interpreted language/syntax (Smarty)

Posted: Fri Feb 17, 2006 5:52 am
by jmut
Jenk wrote:I've not used myself, but have seen a package that uses PHP and only PHP for all it's layers, with the 'simple' separation from presentation and business/data logic like the below:

Code: Select all

?>

<div class="DataContainer">
<?php 
if ($content->rowCount('tbl_MachineInfo') > 0) {
    echo '<table class="tblMachineInfo">' . "\n";
    echo $content->getHTMLTableHeader('tbl_MachineInfo') . "\n";
    
    while ($content->hasNextRow('tbl_MachineInfo')) {
        echo $content->getHTMLTableRow('tbl_MachineInfo') . "\n";
    }

} else {
    echo 'No data to display';
}
?></div>
Whilst I am quite experienced with Smarty, I dislike the idea of creating a new syntax when we have a perfectly viable one that is already in use. It just screams 'overheads' at me using an interpreted language (PHP) to parse a user made interpreted language/syntax (Smarty)
I don't see any separation in this code at all. I use smarty and I like it.
What editor will get this echo stuff and highlight appropriately?
What will this this \n mean to a designer?
I comment this particular "simple" solution you are mentioning....it is way worse than smarty.
Other than that...I agree with some previous examples that smarty has issues as well.

Posted: Fri Feb 17, 2006 6:03 am
by Jenk
The \n can simply be included in the return when it constructs the row, I typed it in out of habit.

Smarty's syntax is just as complex as that, so arguing that to a designer mine is confusing vs Smarty is moot. (I'd even go as far as arguing that insisting Designers cannot read a single line of code is a poor argument, but that's a different story.)

That code performs just what Smarty's {IF} and {LOOP} does.

"What editor?" Are you serious? The list is huge.. this forum shows it is easily identifiable. Smarty is not as widely available for syntax highlighting in editors, so that's actually a downer for Smarty.

Posted: Fri Feb 17, 2006 6:23 am
by jmut
Jenk wrote:The \n can simply be included in the return when it constructs the row, I typed it in out of habit.

Smarty's syntax is just as complex as that, so arguing that to a designer mine is confusing vs Smarty is moot. (I'd even go as far as arguing that insisting Designers cannot read a single line of code is a poor argument, but that's a different story.)

That code performs just what Smarty's {IF} and {LOOP} does.

"What editor?" Are you serious? The list is huge.. this forum shows it is easily identifiable. Smarty is not as widely available for syntax highlighting in editors, so that's actually a downer for Smarty.
could you give me one editor that will highlight this as html:

Code: Select all

echo '<table class="tblMachineInfo">'