views:

760

answers:

6

Hello there,

I just created this thread to discuss with people who have adopted the smarty system, and how many time it took to you to understand it, because i just can't get the idea, instead of making every easier as everybody says i think it just make more complex to code.

More than a problem with only smarty is with the whole MVC model, with CakePHP or KohanaPHP i experience the same problems, u need to do 3x lines of code and files for something that u can do with a few lines of simple php.

Maybe u can tell how did u learned to code using MVC model :)

+2  A: 

I wouldn't recommend using Smarty because PHP is already a templating language, some frameworks such as Zend don't have a built-in templating engine because it makes more sense using php natively for this purpose.

Using it would be redundant, and as you stated, would make things more complex with more overhead.

I would only recommend using a templating system for languages that are not purely web based, such as Python, where templating systems like Mako and Jinja2 are available.

meder
I can't understand why at some point Smarty was the rage and everyone had to use it. Plain old PHP is so much cleaner and efficient that I have never seen a point in templating engines.
Tatu Ulmanen
thanks for your answer :) would be good if someone talk about the MVC frameworks in php (Cake, kohana) and their experiences with them
Korrupzion
The only thing I would add to this is a mention of XSLT.
Jordan S. Jones
@Korrupzion - I've been coding PHP for 5 years, in a framework for about 18 months (currently using Cake). I wouldn't consider coding PHP without one at this point in time. They're just too handy to use simple PHP. Unless you need speed, which is the downside to frameworks (they run at 10-20% of your straight PHP code, although in more complex apps this will converge a little bit). I highly recommend trying one of the big ones -- cake, symfony, zend, codeignitor, kohana). It will make your coding so much faster, easier, and better.
Travis Leleu
btw, if you're like I was, where I was inconsistent with my coding and naming standards, and didn't exactly write code I'd publish... Then a framework is great to help you improve your style. Cake is the most strict about following convention (their motto is "convention over configuration"). Zend is probably the most powerful (they have Lucene tieins, loads of other stuff).
Travis Leleu
+4  A: 

I would put Smarty in a different basket to the heavyweight frameworks like CakePHP, Symfony, etc in that it's quite easy to understand. At least I thought so. The manual is pretty good. Take a look at the Crash Course:

include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');

// display it
$smarty->display('index.tpl');

with:

<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: {$name}<br>
Address: {$address}<br>

</body>
</html>

which is a lot clearer than having:

<?php echo $model->name; ?>

or even:

<?= $model->name ?>

littered all over your code.

And that's it pretty much. I mean there's more to Smarty, obviously. Modifiers, making code cleaner to do odd/even table styling and so forth. But all it is is a nicer syntax for writing PHP views.

You can put in a Smarty template something like:

{$post|strip|stripTags|wordwrap:30}

and this will output a variable, replace all white-space with a single space, remove any tags and wrap on word boundaries with a line length of 30.

That's a lot to do in a single line.

Now only that but you can create custom functions/modifiers/plugins of your own easily enough. I have some that do versioning of static content automatically. See PHP Smarty Tutorial: Caching and Versioning Static Content. You can turn:

{image src="logo.gif"}

into:

<img src="logo.gif?89723422" width="223" height="87">

where the actual height and width are calculated and cached.

cletus
A: 

How about caching? Smartys caching system is pretty great, especially, when you have limits on databases connections and can't connect every user to the database every time he's on the page.

I do however admit, that other then the caching system and the simplicity of use, Smarty isn't that great.

You can take compiled tpl files from Smarty, make the code much simpler, and use it as a template, and just call it by include. I think it's a good start to understand how to template yourself. Or am I just talking bollocks?

PawelMysior
+1  A: 

For me MVC frameworks (and the MVC design pattern in general) are all about 4 things:

  • teamwork (in a framework different people can reuse the same pieces)
  • maintenance (are you going to have someone else ever touch your code? will you remember in 6 months where to look for something, or the structure of your classes?)
  • automation (scaffolds, prototyping, tasks using the cake shell, database migration, unit testing)
  • transparency (think of all the coders who have looked at and tested Cake's Auth component or sanitization libraries -- why should I recreate one, only to leave a open a boneheaded security hole?)

Sometimes these aren't relevant to a project, most of the time they are.

stevenf
A: 

For my point of view smarty is for designers who dont know server scripting language and must "communicate" with the developer to complete the project.

ntan
A: 

This is but scratching the surface of what Smarty is capable of but I hope it provides a useful demonstration of what makes Amsarty so good.

Php Programmer