|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
SitePoint Wizard
![]() Join Date: Sep 2001
Location: Singapore
Posts: 5,332
|
Smarty 2.2.0 Released
Quote:
![]() |
|
|
|
|
|
|
#2 |
|
Wanna-be Apple nut
![]() Join Date: Sep 2000
Location: Halmstad, Sweden
Posts: 7,614
|
yay! Smarty rocks!
|
|
|
|
|
|
#3 |
|
SitePoint Wizard
![]() Join Date: Sep 2001
Location: Singapore
Posts: 5,332
|
my favorite PHP template engine
![]() and at the risk of shameless self-promotion, there's a Smarty article in my sig that could just get you convinced about using Smarty ![]() Last edited by redemption; Jul 14, 2002 at 01:27. |
|
|
|
|
|
#4 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Aug 2001
Location: lost, If you find me please return me to St.Louis
Posts: 396
|
Thanks for the info, I updated.
Do any of you know of a good forum for help with smarty templates? & using templates period? TIA ![]() |
|
|
|
|
|
#5 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2002
Location: Brisbane QLD Australia
Posts: 1,690
|
Redemption: hey nice article!!!!
|
|
|
|
|
|
#6 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 592
|
I read the article.
Still not convinced. But then, I don't see the need for ANY template engine. PHP already is one. (If someone wants my complete rant on this one, just let me know! )Vincent |
|
|
|
|
|
#7 |
|
Mlle. Ledoyen
![]() Join Date: Jan 2001
Location: UK
Posts: 7,312
|
Go ahead Vincent!
Sean ![]() |
|
|
|
|
|
#8 | |
|
Wanna-be Apple nut
![]() Join Date: Sep 2000
Location: Halmstad, Sweden
Posts: 7,614
|
Quote:
![]() |
|
|
|
|
|
|
#9 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2001
Location: Southern California, USA
Posts: 1,194
|
yeh, rant
![]() |
|
|
|
|
|
#10 |
|
SitePoint Wizard
![]() Join Date: Sep 2001
Location: Singapore
Posts: 5,332
|
rant! rant! rant!
![]() |
|
|
|
|
|
#11 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Vincent's rants are awesome! Rant away. It's going to be a tough one though - how do you cooperate on a site with a designer who only knows HTML, without using templates (or building a custom design interface)?
Also - why do none of the PHP template systems (that I'm aware of) use XML for marking up the "dynamic" parts of a template? Browsers ignore tags they don't recognise so for a designer, they could view a page without even seeing the XML or needing to interfere with it. |
|
|
|
|
|
#12 | |
|
Wanna-be Apple nut
![]() Join Date: Sep 2000
Location: Halmstad, Sweden
Posts: 7,614
|
Quote:
|
|
|
|
|
|
|
#13 |
|
SitePoint Wizard
![]() Join Date: Jan 2002
Location: Canada
Posts: 7,385
|
Good Article.
![]() I was about to switch, and reading that, I am for sure. FastTemplate was slow... ![]() Smarty, here I come! ![]() |
|
|
|
|
|
#14 | |
|
SitePoint Wizard
![]() Join Date: Sep 2001
Location: Singapore
Posts: 5,332
|
Quote:
![]() at least my article did convert one person to using Smarty ![]() <edit> ok judging from your posts (someonewhois), maybe not </edit> Last edited by redemption; Jul 15, 2002 at 19:44. |
|
|
|
|
|
|
#15 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 592
|
A rant, by request...
My apologies for posting this rant at so late a time, but I had to get in the right state of mind first, and I didn't want to let you down by posting a useless little rant. Also, it seems like I have a reputation to uphold...
I seriously believe that template engines like Smarty are a complete waste of our precious time. Luckily these packages are generally freely available, or they would be a complete waste of money as well. They do not add any features to PHP not already there, and by using them, dynamic web sites become more complex, a lot slower, and harder to maintain. That's right! Harder, not easier. Making these bold statements is plain silly without explaining them, so that is what I will do in the remainder of this rant. As this is a genuine rant, I may unpurposefully hurt the feelings of some well-respected people (at least by me they are ), and I may not always appear to be the nice guy I actually am... What does a template engine enable us to do? It allows us to separate content from business logic by supplying it with a template file. The template file is a normal HTML file, with special symbols (variables) in it instead of traditional content, and with special control structures (or: blocks) to influence the presentation logic. Here's an example of using a template file with Smarty: PHP Code:
PHP Code:
So why do people think we need a template engine? The answer to that question is very simple: PHP has become too complex. Back in the old days, when PHP4 was just PHP/FI, the language was nothing more than a simple template engine. It had variables and blocks, and that was about it. Now, after a couple of years, PHP has been added to (but nothing was removed!) - it now has classes - and a large function library was written. And the latter is where the problem lies. Pick any book on PHP from a shelf in your local bookstore, and look how result rows from a MySQL database are printed. (MySQL is of course the DBMS used in those books, which should already give you a clue about how bad the book is.) The mysql_-functions are used all over the place in the presentation layer. Here, in these forums, we have learned people to not use those mysql_-functions directly, but use a database abstraction layer instead. This makes coding simpler (no need to know all those functions for the various DBMS's) and when they decide to use another DBMS instead of MySQL (and they undoubtedly will at some point), the conversion will be painless. What is the result of those lessons? People no longer use those functions directly, so that's good. On the other hand, they still place their business logic all over the presentation code. That's not so good. Most people don't know what to do about it, untill they learn about those yukky template engines. No need to carry on here... At this point I also like to point out that saying "The layout must be strictly separated from the content" is a moot point. What is the layout? That's the colors, the fonts, the styles and so on. The plain HTML itself is the content. A couple of years ago cascading style sheets were introduced, and using these makes it possible to cleanly separate content from layout. I can change 99% of the layout of any of my sites by just changing the style sheet; there is no need to dive into the HTML. Only if the site needs a major overhaul the HTML must be changed. If your web designer tells you this isn't true, fire him. Another answer to the question "Why do people think we need a separate template engine?" is the following: the biggest part of the PHP community (and the ASP community, for that matter) knows nothing about software architecture. Most of the PHP programmers don't know what an O(n^2) algorithm is, or who Donald Knuth is, or why a sorting algorithm can never be faster than O(n log n), or how Dijkstra's shortest path algorithm works. If they don't know the answers to these basic questions, how can we expect them to know how the design a software program? I'm not saying the majority of the community consists of a bunch of idiots, because that certainly isn't true. There are lots of bright, intelligent people out there, but they just don't know how to write software, because that isn't in their field of expertise. And can we blame them? Of course not. People who use PHP aren't software developers to begin with. I have been programming now for about 13 years (I started when I was 12), and am currently graduating in University. In a couple of months, I will hopefully have a Masters Degree in Computer Science. This isn't meant to boast; it is meant to put things in perspective: I think I have the right to say that I know a little of what I'm talking about. I have been studying software architecture for many years know, and I still find it very hard. Programming is easy, yes. But developing software is extremely difficult. Don't trust anyone who tells you otherwise. Reading these forums, or examining PHP projects (PEAR, Smarty, Javuh, ...), I often become a little depressed. The questions asked are so simple; the mistakes made so stupid... But what can I expect? PHP wasn't built and isn't used by professional software developers (something I hope to become one day). The PEAR library is a badly designed (and badly implemented) library for several reasons. I have tried pointing these out several times (I had a long discussion with the PEAR developers), but most people simply don't get them, and praise the library to heaven. I try not to say something about this too often, because I'm likely to hurt feelings and there's no point in doing that. But then there are these people starting to praise these so-called 'template engines' and all I can think of is: "Here we go again..." Then I just have to say something about it. Again, I must stress that I don't blaim any PHP programmer personally, and that I don't think they are stupid. They are just working outside their field of expertise, that's all. It's like asking a carpenter to paint a house. He will probably do a nice overall job, but a real painter will undoubtedly do better. Well, this more or less concludes my rant. From now on I will be nice again to everybody, and I won't be saying all too bad things about template engines, the PEAR library, or anything else I have a strong opinion about. Unless you want me to, of course... Here are some questions/remarks you may have had when reading this rant, and my answers for them. Q: If you use variables like '$title', as in your example, all over your code, then the business logic becomes a mess, and there is a possibility of clashes between such variables. A: Then don't use these (global) variables. With little effort you can set up a simple class (or other structure) that stores the values for the page you are creating, allowing you to nicely separate presentation code from the business code. Q: The 'better' template engines have a cache to make loading of pages much faster. Clean PHP doesn't do this, so the template engine is, in that case, faster! A: Incorrect. The cache in a template engine is needed to store the parsed template in some other format (Smarty uses PHP for that), which can then be instantly restored. With clean PHP the format is already correct, so there is no need to use a cache. Less and simpler lines code, faster execution. Q: Instead of using a template engine-specific language, our web designers - who know nothing about programming - are now required to have knowledge of PHP! We can't have that, now can we? A: Why not? The template engine-specific language is nothing more than a minimal programming language with an ugly syntax. It has variables, conditionals, loops, and maybe even functions. As PHP is a full programming language, it has all of these constructs, and more. But that doesn't mean you require your web designers to know all there is to know about PHP. The set of requirements doesn't change at all. Just the basics will be enough. The added benefit is that if they want to, your web designers can start experimenting with PHP (or other scripting languages) much faster. Q: You said maintaining a web site generated by some template engine is harder to maintain instead of easier. Why? A: One reason is that an additional software package is involved. At least one person must be able to work with it. More knowledge is required. Also, not only the web site and PHP must be kept up to date; the template engine itself must be maintained as well. This means more work. Another reason is that the best solution for any problem is always the simplest one. Using a large, complex template engine where PHP itself suffices is not a simple solution. In other words: more work yet again. Vincent Last edited by voostind; Jul 16, 2002 at 02:25. |
|
|
|
|
|
#16 |
|
Wanna-be Apple nut
![]() Join Date: Sep 2000
Location: Halmstad, Sweden
Posts: 7,614
|
Your rant messed up my hair.
|
|
|
|
|
|
#17 |
|
Super Ninja Monkey
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2001
Location: Sioux City, Iowa
Posts: 709
|
Wow that blew me away. If you'd let me I'd like to make a section on my site called "Vincent's Rants" and put all the ones I can find.
![]() |
|
|
|
|
|
#18 | |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Quote:
You've mentioned using classes for content seperation before, in this (now I read this you must have been groaning as you read my first post there - no problem)Could you give a simple example of how you normally do that? E.g. constuct the header and footer, fill in the content depending on the "page" someone is viewing Also, what's your take on the Fusebox, if you've come across it. And finally (sorry - being very demanding), of the PHP application frameworks (plus ezPublish) out there, are there any you've used, would recommend? |
|
|
|
|
|
|
#19 | |
|
SitePoint Wizard
![]() Join Date: Jan 2002
Location: Canada
Posts: 7,385
|
Quote:
![]() It wasn't working, but you were right, it was faster... geez, what kind of name is FastTemplate, if it's dead slow? ![]() |
|
|
|
|
|
|
#20 |
|
SitePoint Wizard
![]() Join Date: Sep 2001
Location: Singapore
Posts: 5,332
|
i'm in a little of a hurry but i thought to let vincent have a go at 2 of my counter-arguments (for now
):1) i develop scripts for clients -- they aren't complete idiots and as such know some HTML D)... i've had requests that they change the layout for them or that they be allowed to easily change the layout... wouldn't templates be a particularly good way to solve that problem... mind you templates are far more readable than PHP scripts... and one thing is they can show in WYSIWYG editors like Dreamweaver, perfect for a quick redesign of the layout2) templates can be re-used... you can have 1 template for a particular section (or even the whole site) and then re-use them for each page, just changing the variable contents... i don't see that happening easily unless you write a class for that (like Perl/CGI or Python's HTMLgen) |
|
|
|
|
|
#21 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Oct 2001
Posts: 592
|
Quote:
Anyway, to answer redemption on how templates can make life easier: If you read my rant carefully (although I don't know why you would do that ), you'll see that I never said templates themselves are useless; I merely said third-party template engines are useless, which is something quite different. I just don't see why it would be easier for a web designer to write '{title|ucfirst}' (Smarty) instead of '<?= ucfirst($title) ?>' (PHP). It's even true that the latter is supported better by applications used by web designers, as for example Dreamweaver supports PHP, but not Smarty. There's nothing wrong with templates. On the contrary, using them is often a good idea. But as PHP is a template engine in itself, why would you need another one?My own approach to developing web sites is different from the normal approach ('normal' being the one taught in the average 'PHP & MySQL' book). Instead of working my way back from a set of pages given by a web designer to a fully dynamic database-driven PHP site, I view a web site as I view any software product (surprise!). Most of you will know by now I write my software object-oriented, and the same goes for websites. A page on a site is represented by a class, which has a method 'show' that prints the page. Of course that method doesn't do all the work by itself; it calls other methods on the page object itself to show different parts of the page (the header, the menu, the footer and so on). The nice thing about this is that I can easily write a subclass that overrides parts of the original class to generate a page that looks more or less the same, but works slightly different. A subclass 'NewsPage' for example might look in some subdirectory, read all text files in it, and print their contents, ordered on the dates the files were created. You might argue that this kind of approach makes it harder to separate the code from the HTML, but this is only partially true. It is true that a single HTML page is broken up in separate parts: headers, footers, and so on. But this is also the case for template engines. Depending on the complexity of the site, I can use simple classes that merely echo the necessary HTML inside the classes themselves, or classes that retrieve the content from (template) files on disk, or extract it from some database, or whatever. To bind the different pages the site can create together, I use a different class 'Site' or 'Application'. This class knows about all pages the site can create, and the settings for these pages (like the specific class to use and the arguments that need to be passed to the class on construction). These settings can be read from a text file, extracted from a database, or restored from a cache file. Whatever is more appropriate. This design more or less implies that my sites have only page 'index.php' that can be influenced by setting the parameter 'page' to some (valid) value. It reads the 'page' parameter, gets the settings for that page, instantiates the right class, passes the settings to it, and shows it. From what I've seen of Fusebox, this is more or less what they do there, although I do it a bit simpler. One important thing is that I can reuse this set of classes for many sites, and not just the one I currently happen to be working on. There are many application frameworks out there, but I don't use one myself. I've examined a few in detail, and although some work pretty well, I haven't yet found a framework I like. The code I use in my daily work shouldn't just work well, it should look good too. In my book, good looking code is fully object-oriented, well layered code. I haven't found such a package yet, nor do I think I ever will (for reasons I have made all to clear in my rant..). All PHP code I use is code I wrote myself. That's code I understand, code I trust, and last but certainly not least, it's code that Looks Good (TM). ![]() Vincent |
|
|
|
|
|
|
#22 | |||||
|
SitePoint Wizard
![]() Join Date: Sep 2001
Location: Singapore
Posts: 5,332
|
Quote:
... i know you never said templates were useless and i never thought or implied that you said that (if you read _my_ short post carefully )...Quote:
?Quote:
... that's my point ![]() Quote:
Quote:
... i believe in that too... so much so that it took me awhile to get used to the idea of using other peoples' scripts, code snippets, libraries... but think about it: if you use Perl, you're using modules which are other people's code, even the Perl interpreter is other people's code... the same goes for any other programming language... code-reuse is a Good Thing too... but like you say it should be code with quality, efficiency, security and stability...someone with strong opinions like yours make for good discussions and a new learning experience every time Vincent ![]() - Joel |
|||||
|
|
|
|
|
#23 |
|
Wanna-be Apple nut
![]() Join Date: Sep 2000
Location: Halmstad, Sweden
Posts: 7,614
|
You know, this is EXACTLY the reason I'm considering switching from PHP to ASP.NET. Their "templating" is soo good. Someone really should rip it off for PHP.
Code:
<asp:datalist id="MyList" layout="flow" showfooter=true borderwidth=0 runat=server>
<HeaderTemplate>
<table cellpadding=1 cellspacing=0 >
<tr>
<td colspan=4>
<b><font face="Verdana" size=3>Product Listing </font></b>
</td>
</tr>
<tr>
<td colspan=4 height=5 bgcolor="000000"></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td colspan=3 style="font-size:10pt">
<b><%# DataBinder.Eval(Container.DataItem, "title_id") %></b>
<span> <%# DataBinder.Eval(Container.DataItem, "title") %> </span>
</td>
<td align=right style="font-size:10pt">
<b><%# DataBinder.Eval(Container.DataItem, "price", "${0}") %> </b>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan=4 height=1 bgcolor="000000"></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<tr>
<td colspan=4 height=5 bgcolor="000000"></td>
</tr>
</table>
</FooterTemplate>
</asp:datalist>
Server controls are another thing that's very nice and clean. If you put a "runat=server" attribute in any HTML tag, you can simply change it in the script - like this: Code:
<script runat="server"> Message.InnerHtml = "This is another message"; Message.Style["color"] = "red"; </script> <label id="Message" runat="server">This is the default message</label> Code:
<span style="color: red;">This is another message</span> |
|
|
|
|
|
#24 |
|
SitePoint Wizard
![]() ![]() Join Date: Nov 2000
Location: Switzerland
Posts: 2,906
|
Mattias - looking at that code, I don't think it solves the problem Redemption wants to solve - that designers with HTML knowhow only will build the site design. That looks to me like it's going to take a .NET coder use it.
|
|
|
|
|
|
#25 | |
|
Wanna-be Apple nut
![]() Join Date: Sep 2000
Location: Halmstad, Sweden
Posts: 7,614
|
Quote:
Code:
<asp:datalist id="MyList" layout="flow" showfooter=true borderwidth=0 runat=server>
<HeaderTemplate>
<table cellpadding=1 cellspacing=0 >
<tr>
<td colspan=4>
<b><font face="Verdana" size=3>Product Listing </font></b>
</td>
</tr>
<tr>
<td colspan=4 height=5 bgcolor="000000"></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td colspan=3 style="font-size:10pt">
<b><%# DataBinder.Eval(Container.DataItem, "title_id") %></b>
<span> <%# DataBinder.Eval(Container.DataItem, "title") %> </span>
</td>
<td align=right style="font-size:10pt">
<b><%# DataBinder.Eval(Container.DataItem, "price", "${0}") %> </b>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan=4 height=1 bgcolor="000000"></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<tr>
<td colspan=4 height=5 bgcolor="000000"></td>
</tr>
</table>
</FooterTemplate>
</asp:datalist>
|
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 06:57.









)

D)... i've had requests that they change the layout for them or that they be allowed to easily change the layout... wouldn't templates be a particularly good way to solve that problem... mind you templates are far more readable than PHP scripts... and one thing is they can show in WYSIWYG editors like Dreamweaver, perfect for a quick redesign of the layout


Linear Mode
