Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

The Logic of Templates (massassi.com)
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions
View previous topic :: View next topic  
Author Message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 08, 2003 5:49 am    Post subject: Smarty and Separation Reply with quote

Quote:
I know I have said this already a hundred times, but Smarty is the only serious way of cleanly keeping application and presentation logic apart.

I've been thinking about this a lot recently and I keep on coming to the same conclusion: Smarty doesn't separate logic any more efficiently than PHP alone. What it does do is provide syntax and framework. Part of the proof of that is that you can do pretty much anything in-template as you can in PHP. Many say that amounts to abuse of Smarty. Yet the fact that it allows this goes to show that Smarty by itself does not separate these logics. Instead, the application designer is still left to do that by providing additional framework -- Smarty just makes doing that much easier and offers several helps including its syntax.

On the syntax level, it is important to note a very large compaint of the pure PHP style: despite the fact that it CAN be used to separate logics, the way that it embeds several syntaxes is "wrong" in many people's eyes. I tend to agree--but Smarty does no better on this regard. It only makes it easier to swallow.

The HTML (or whatever your template is spitting out) is still hard-boiled. You see this problem become very evident on multi-language sites. Worse, it forces an HTML (in this case and in general) centric view on Smarty, which I think is counter-productive as a whole. The disucssions elsewhere on the forums relating to the html_* plugins act as a pointer to this issue.

An old-fashioned template system (a simple text or macro replacer) does things like this:

<b>{$mytext}</b>

The designer decided that {$mytext} is to be rendered as bold, and no one else cares about that decision. You don't need the sophistication of Smarty to support that.

Yet, is that code fragment really what the designer is paid to do? NO. The designer's job is to decide that $mytext is bold -- not that it should be in <b> ... </b> tags -- that's only a consequence of the technology. The key here is that HTML is not just hard-boiled text but really is language--in this case, a rendering language--and that is not being addressed by this usage pattern.

Smarty provides several ways to support a semantic definition of templates, one of the better of these being modifiers:

{$mytext|bold}

That's much smarter. With just a little bit of under-the-hood tweeking, you can get that to render in a variety of ways. This is where *I* think Smarty is best leveraged and shows tremendous advantage over other similar systems or even "standard" Smarty usage.

This really does separate your design logic from other portions of the application (including the rendering) while at the same time providing a software hook into those semantics. The application programmer can change them at will and the application can re-use templates in a variety of ways. I think this is very liberating while it also leverages the work of the designers.

And look--only one syntax to deal with! Now the verbs are in the control of the application designer and the nouns are in the control of the content designer. Now that's separation!

I'm wondering how many out there agree (or disagree).
Back to top
View user's profile Send private message
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Thu May 08, 2003 5:04 pm    Post subject: Reply with quote

In that case, all your design markup will be in the form of smarty plugins/filters/etc.? In that case, what happens if you only want bold in one output format (IE: a presentation markup - HTML), but not in a more data-centric markup (XML). You still have to make two sets of templates. Only this way, all the presentation takes place in one place with easy to use syntax (HTML) as opposed to having to embed everything inside a php function (in your {$var|bold} example).
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 08, 2003 5:44 pm    Post subject: Reply with quote

Brian wrote:
In that case, what happens if you only want bold in one output format (IE: a presentation markup - HTML), but not in a more data-centric markup (XML). You still have to make two sets of templates.


I've been considering that point. I suspect that a thoughtfully designed framework will permit this type of pattern without the multiple templates. Either you maintain different libraries of plugins (say in directories) that exhibit different behaviour, or you extend the way modifiers work slightly so that you can create a context block function that determines the how the contained modifiers behave (see here).
Back to top
View user's profile Send private message
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Thu May 08, 2003 6:07 pm    Post subject: Reply with quote

You're ending up with a ton of layers... I generally prefer to reduce them as far as possible - more layers = more code = more bugs. Check this out:

http://www.joelonsoftware.com/news/20030428.html
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 08, 2003 6:38 pm    Post subject: Reply with quote

Hey, I'm trying to get more layers!

More layers = more separation = more focused development (at any given layer).

Now, I'm not saying n-thousand layers is a good thing, and I don't think I'm going for some far-fetched usage pattern here. In fact, the layers I'm suggesting are processing layers more than they are abstraction (as in class) layers. There is a big difference. I'm talking about a type of pipeline and that is a well understood, researched and optimizable problem.

Funny how on the one hand folks want separation, yet on the other hand they want a flat model. Sheesh. I find it hard to believe that people living in script land are reluctant to layer--at least at the logical level.

The article you linked was mildly interesting though most of the points in are debunkable and further, he's talking about the .Net platform. If there is one thing that the .Net isn't is dynamic and that makes a difference, too.

Smile If you like flat models, you might want to consider sticking to C (or lower) Smile
Back to top
View user's profile Send private message
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Thu May 08, 2003 6:48 pm    Post subject: Reply with quote

admin: sorry this message was accidently erase. my fault Sad. please repost.
Back to top
View user's profile Send private message Send e-mail
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 08, 2003 7:23 pm    Post subject: Reply with quote

Sorry Brian, I accidently edited your message instead of posting a reply. It was unintentional.

And yes, I'm sure I did a bad job explaining my story--I'm sorry about that. I'm still struggling with the approach because I think it has merits, but I haven't found the right way to implement it yet--so I'm still stuck with the usual way of doing things.

Notice that what I'm trying to do is get to the point where everything is expressed as data. I like data. That doesn't mean that there are no loops or other intersting Smarty features, mind you. It also doesn't necessarily imply that you end up with HTML in your modifier code. I have generic tagging code that is configurable enough to tag most types of data using most types of formats.

The reason for Smarty in the process is to leverage all the things it already does--including compilation (which helps flatten out the layers). perhaps a two stage compiler with the first pass rendering a "normal" Smarty like page??

Anyhow, I *will* get something along these lines working to my satisfction and when I do I hope I will have something more meaningful to share.

In the meantime, thanks for your comments, they ARE appreciated!
Back to top
View user's profile Send private message
cm
Smarty Rookie


Joined: 13 May 2003
Posts: 6

PostPosted: Tue May 13, 2003 1:55 pm    Post subject: Reply with quote

Quote:
the primary reasons why I prefer Smarty over pure PHP templates are:

1) most things are easier in templates (think of {$foo|truncate:30:"..."} which would need several ifs and so on in PHP)

Well, actually that would simply need a function truncate() in PHP which already is provided by Smarty. I.e. Smarty's advantage in this case is to provide quite a few useful routines for the end-user. That also means, tho, that if there was some kind of freely available "Presentation Logic Library" (with the same level of quality of course), that would be able to replace Smarty, without the extra syntax. You know, I happen to think that those features - custom syntax and presentation logic separated from application logic - are quite orthogonal.

Quote:
2) it's a complete, ready-to-use, stable application

You know, even PHP isn't that unstable either Wink The only thing it lacks in that respect is completeness, and a custom library (see above) could provide that for presentation logic.

Quote:
3) things like caching, filters and other damn useful features would take a lot of time to implement in an own pure PHP template class

See Brian's post for that.

Quote:
4) it's insanely fast, as it compiles templates

Compiles them to.. PHP code. Exactly, my point.

Quote:
5) plugins, filters etc make it extremely customizable

There is some cool language construct called "function" in PHP Smile

Quote:
6) it's LGPL

<php>/license.txt

Quote:
7) the developers are cool guys

Can't really comment on that one Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue May 13, 2003 4:46 pm    Post subject: Reply with quote

@cm: so what is your point?? You don't need Smarty?

It has already been said many times that there is nothing Smarty provides functionality-wise that can't be done in straight PHP (for obvious reasons), so repeating that doesn't particularly add to the argument. Wink The fact that both Smarty and PHP use delimiters to allow intermixing of hard-boiled text and code instructions is little bit of a moot issue and misses the point. Yes, logic separation and syntax ARE somewhat orothogonal, BUT "cleaner" syntax can lead to cleaner design, yes?

What Smarty IS: a lot of functionality that people otherwise develop (or package) on their own, WITH custom syntax, WITH security, WITH caching and WITH simple extensiblity.

What Smarty is NOT: a replacement for PHP.

Smarty is task oriented tool, PHP is general tool (despite what Zeev and the other PHP developers may say on the lists). People use Smarty when they have to deliver maintainable, flexible websites but don't want to re-invent the wheel all the time. Borrowing a metaphor: it enables a production pattern and in doing so, enjoins many types of users (other than PHP coders) to participate in the production process.
Back to top
View user's profile Send private message
cm
Smarty Rookie


Joined: 13 May 2003
Posts: 6

PostPosted: Wed May 14, 2003 4:08 pm    Post subject: Reply with quote

Quote:
@cm: so what is your point?? You don't need Smarty?

My point was exactly what I stated above, nothing more, nothing less. I simply corrected what that guy posted above, the rest is your interpretation (i.e. no need to repeat your stuff again and again, no need for two question marks either Smile).

Although I consider other "template systems" (which is actually the wrong term) to be superior to Smarty, in PHP it does the job pretty well for me and I'm actively using (and customizing!) it a lot.

Quote:
Smarty is task oriented tool, PHP is general tool

I think "domain-specific" and "what-might-eventually-become-a-general-purpose language" describe them better.
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Wed May 14, 2003 4:30 pm    Post subject: Reply with quote

yeah, boots, that's exactly what I wanted to ask him, too, as I still don't get what he wants to say

cm:
this was my opinion, the reasons for me to use it. I didn't

and on again with my/your points:

cm wrote:
Wom.bat wrote:
the primary reasons why I prefer Smarty over pure PHP templates are:

1) most things are easier in templates (think of {$foo|truncate:30:"..."} which would need several ifs and so on in PHP)

Well, actually that would simply need a function truncate() in PHP which already is provided by Smarty. I.e. Smarty's advantage in this case is to provide quite a few useful routines for the end-user. That also means, tho, that if there was some kind of freely available "Presentation Logic Library" (with the same level of quality of course), that would be able to replace Smarty, without the extra syntax. You know, I happen to think that those features - custom syntax and presentation logic separated from application logic - are quite orthogonal.

Aight... I was talking about the Syntax, and a {$var} is definitely nicer than <?php echo $var; ?> (yep, I know about <?=$var?>, but what if it's disabled on the destination machine?), not to talk of modifiers or things like cycle...

cm wrote:
Wom.bat wrote:
2) it's a complete, ready-to-use, stable application

You know, even PHP isn't that unstable either Wink The only thing it lacks in that respect is completeness, and a custom library (see above) could provide that for presentation logic.
Did I say that? Smile

cm wrote:
Wom.bat wrote:
3) things like caching, filters and other damn useful features would take a lot of time to implement in an own pure PHP template class

See Brian's post for that.
...

cm wrote:
Wom.bat wrote:
4) it's insanely fast, as it compiles templates

Compiles them to.. PHP code. Exactly, my point.
Err... yeah... what's your point, then? Smarty compiles to PHP, so there is few performance overhead. That was my point. The reason to use it. If Smarty wasn't there, I'd for sure use pure PHP templates[/quote]

cm wrote:
Wom.bat wrote:
5) plugins, filters etc make it extremely customizable

There is some cool language construct called "function" in PHP Smile
cool... nice to hear that Wink

cm wrote:
Wom.bat wrote:
6) it's LGPL

<php>/license.txt
Lesser GPL is another reason for Smarty. If it was GPL, I wouldn't be able to use it for commercial applications.

cm wrote:
Wom.bat wrote:
7) the developers are cool guys

Can't really comment on that one Smile
They are Wink

you know... I didn't say anything against PHP... I just like Smarty a little more than PHP in my templates...
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 14, 2003 4:44 pm    Post subject: Reply with quote

Quote:
Although I consider other "template systems" (which is actually the wrong term) to be superior to Smarty, in PHP it does the job pretty well for me and I'm actively using (and customizing!) it a lot.


CM: I really don't get you. Either you think PHP alone is better than Smarty (as you argued) and you go down that road or you think "other template system" are better than Smarty and you use them. But no--you state that you actively use Smarty. Are you a masochist? Razz

I doubt that you are, so I'll ask: what is it about Smarty that causes you to use (and customize) it? After reading your posts, I'm really interested to know! After all, you know why we use it, but we only know why you wouldn't use it.
Back to top
View user's profile Send private message
cm
Smarty Rookie


Joined: 13 May 2003
Posts: 6

PostPosted: Fri May 16, 2003 3:28 pm    Post subject: Reply with quote

Wom.bat, I tried to express that most of your arguments were nonsense in comparison to "pure PHP templates". Of course they are arguments for Smarty in comparison with other (PHP) template engines, but in that issue they just didn't work out too well Smile

boots, I don't think I ever stated that "PHP alone" was better than Smarty. In my opinion Smarty is very well-suited as a DS (meta)language in a PHP-ish work environment. As I said the only point of my original post was to caricature that Wom.bat's arguments in favor of Smarty weren't really arguments when comparing with "pure PHP templates".
Back to top
View user's profile Send private message
Pineapple
Smarty n00b


Joined: 23 May 2003
Posts: 3

PostPosted: Fri May 23, 2003 3:49 am    Post subject: Reply with quote

Hypothetically, if Smarty were ever ported to other languages, all of the templates would work with no modifications. That would not be the case with PHP based templates.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri May 23, 2003 4:29 am    Post subject: Reply with quote

Pineapple, that is *very* hypothetical -- but I like your thinking!!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 3 of 6

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP