Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

The Logic of Templates (massassi.com)
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Article Discussions
View previous topic :: View next topic  
Author Message
wisl
Smarty n00b


Joined: 26 May 2003
Posts: 1

PostPosted: Mon May 26, 2003 2:15 am    Post subject: where is the separation of code and presentation? Reply with quote

hi!
with the line:
Code:
{section name=curuser loop=$user}

in the template you have included some business logic in the template.
although there is the possibility of special html-tags in the templates which are variables for the html-tags (eg. option in select-tags).
don't you expect some difficulties with designers who have to learn this special coding stuff of smarty Question
For example the class.FastTemplate only uses comment tags for dynamic blocks
Code:
<!-- BEGIN DYNAMIC BLOCK: somethingtodisplay_severaltimes -->
{NAME} - {EMAIL}<br>
<!-- END DYNAMIC BLOCK: somethingtodisplay_severaltimes -->

So the designers only need to use their tags Wink
The programming is done by PHP.
So why isn't there a complete separation of coding and presentation in smarty?
regards
bozo
Back to top
View user's profile Send private message
eadz
Smarty Regular


Joined: 30 Apr 2003
Posts: 61
Location: Auckland, New Zealand

PostPosted: Mon May 26, 2003 2:28 am    Post subject: Re: where is the separation of code and presentation? Reply with quote

wisl wrote:

Code:
<!-- BEGIN DYNAMIC BLOCK: somethingtodisplay_severaltimes -->
{NAME} - {EMAIL}<br>
<!-- END DYNAMIC BLOCK: somethingtodisplay_severaltimes -->

So the designers only need to use their tags Wink
The programming is done by PHP.
So why isn't there a complete separation of coding and presentation in smarty?
regards
bozo


I can do this with smarty :

{ block something=something }
{$name} {$email}
{/block}

.. But I do think that having 2 options for tags would be good. So you could have

<!--{ block something=something }-->
{$name} {$email}
<!--{/block}-->

so when you're using wysiwyg editors, you see the varibles but not the logic.
_________________
bBlog - Smarty based blogging software
I work for Webforce web site design ( Auckland, NZ )
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Mon May 26, 2003 2:51 am    Post subject: Reply with quote

Hi wisl!

Actually, in your example, you aren't really separating code and design either--you are shifting parts of it into "conventions" and are hiding others with syntax. Plus, you didn't point out that you end up putting design issues into your business code.

First of all, something like:

Code:
<!-- BEGIN DYNAMIC BLOCK: somethingtodisplay_severaltimes -->
{NAME} - {EMAIL}<br>
<!-- END DYNAMIC BLOCK: somethingtodisplay_severaltimes -->


isn't exactly straight 100% HTML, is it? In fact, it is an abuse of the comment tag. In this case, you are putting extra conventions on the meaning of an HTML comment to define programatic constructs. The template designer may understand -- but they will probably understand Smarty's method of doing this as well. The point though, is that you have code in your template.

Secondly, there is a reason for this: it recogonizes the fact that designers need to indicate repeatable sections in templates but it gives them very little control over the process. Instead of your application designer merrily building objects and your designer happily twiddling pixels, your designer is at the mercy of the programmer and vice versa. Need a table to work a bit differently than normal (but no changes to the underlying data, meahwhile), well you gotta haul your programmer into the room to make it work. (Your programmer will grumble -- after all, they got the data part right ! )

It shouldn't be surprising that there is a logic to design that requires certain procedures, steps and actions to be taken at given times. Smarty provides a lot of tools to allow a designer to do their own internal co-ordination without programmer intervention.

YES. Of course you can have business logic in your Smarty templates -- that is not denied. Yet you can equally well avoid this -- as with everything else, it depends on the talent of your team. If you are only relying on a bunch of fresh faces from the local business school to build your site, then you may end up with a slight problem down the line Wink

I think that Smarty provides a lot of features that aid in separating the logic by not only delineating the separation problem, but also giving adequate tools on BOTH sides of the fence so that cross-over is less necessary. Perhaps it means that you end up getting a two-sided sword with which you'll kill yourself. Or perhaps, the flexibility gives your programmers and designers a breath of fresh air allowing to focus more in their own domains because they have flexible and powerful tools. When it comes to code separation, well, you can lead a horse to water...

Take it easy.

ps. I think it is lovely that people are fanatical about code boundaries. Separation is lovely and should be strived for but I wonder, for example, if we would really want every car to be built with high-performance standards. Use the right tools for the right job -- that's what I say Smile

pps. I also love how designers almost always get characterized in a way that suggests that maybe they are one sandwich short of a picnic. All I know is that the one's I have worked with have been more than adequately bright, to say the least.
Back to top
View user's profile Send private message
cm
Smarty Rookie


Joined: 13 May 2003
Posts: 6

PostPosted: Wed May 28, 2003 4:10 pm    Post subject: Reply with quote

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.

That would be true for an ambitious, yup. In case you're referring to me saying that "In my opinion Smarty is very well-suited as a DS (meta)language in a PHP-ish work environment.", you sorta missed my point, which wasn't to say that that weren't possible, but that Smarty is IMO one of the best choices in a PHP-ish environment (i.e. implying that there might be better choices in other languages).
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed May 28, 2003 8:07 pm    Post subject: Reply with quote

boots wrote:

pps. I also love how designers almost always get characterized in a way that suggests that maybe they are one sandwich short of a picnic. All I know is that the one's I have worked with have been more than adequately bright, to say the least.


Brightness isn't necessarily the issue. All programmers and designers are typically bright Very Happy, and each are talented within their element. Therefore it is an important factor to determine these boundaries. This differs for everyone, so you may lean toward conventions that best suit your disposal of talent.

Monte
Back to top
View user's profile Send private message Visit poster's website
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Thu Jun 05, 2003 4:22 am    Post subject: Updated article... Reply with quote

I have posted an updated article here:

http://www.massassi.com/php/

It contains the new template.php code as well as the caching code (with examples). Hopefully you will find it interesting.

Brian
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: Mon Jun 09, 2003 9:48 pm    Post subject: Reply with quote

Hi again Brian! Thanks for taking the time to update your article and update us here. I think that better characterizes your argument, especially with the longer example.

I'll take a different tact and argument this time and I'll only gloss over the fact that even you admit that you aren't happy with your caching code though that emphasizes the fact that as a maturing library, Smarty eases the application programmer's workload by providing often-requested and well-tested features.

My main reason for writing, though, is to take a moment to consider your conclusion:
Quote:
In short, the point of template engines should be to separate your business logic from your presentation logic, not separate your PHP code from your HTML code.

My read of that suggests that you mean to say that there is no logical difference between:
[php:1:73902247fd]<h1><?php echo $mydata ?></h1>[/php:1:73902247fd]and
Code:
<h1>{$mydata}</h1>

In fact, there is a difference. The PHP example is code while the template example is data. Yes, Smarty eventually compiles the template to code, but since it is first considered as data, it means that the programmer has more opportunities to play with it.

As a small example, Smarty provides a variety of filter mechanisms which allow the template to be modified on-the-fly, as it were. Many Smarty sites use filters to provide multilingual capablities. In a sense, because the template can be treated as data, it can be evaluated and modified in multiple contexts successively. Code does not share that property -- at least in a language like PHP. [aside: Even that has some caveats as the new PHP Streams and User Stream wrappers (still in early development) look like they have the potential to allow you to transparently add a lot of custom functionality to the way files are processed. IMHO, from my testing they aren't ready yet but I'm hoping they will be by the time PHP5 rollsout.]

Reading the various articles on PHP as Template engine I sometimes get the impression that the message is that PHP has already solved the basic language issues like loops and variables, etc -- so don't re-invent the wheel, use PHP! I can't say I favour that kind of reasoning because it ignores the issue of context. [It also reminds me of the one language arguments (like everyone should speak English or 100% Pure Java)]

I'll say it again, though: Smarty won't be the best solution for every single site or use. Then again, chances are you don't have a Linotype Typesetter sitting in your home office either. Smile

Of course, treating code as data is not everyone's cup of tea but I tip my hat to the folk who wrote Smarty: considering the target, they have created a very practical and useful framework.

Again, thanks for your interesting article! If you want to see an even more extreme example of your case, may I suggest the self-proclaimed AwesomeTemplateEngine? I warn you though--the template class weighs in at a hefty 338 BYTES. I'll let you decide if its heavy on features or not Laughing The readme is longer than the template class (which also includes comments, by the way), but I will include the readme here anyhow since it is right on topic:
Quote:
AwesomeTemplateEngine v1.0

License: GNU Public License

Installation:

Extract the Zip somewhere in your web directory and enjoy. The example should
make all clear.

The bottom line:
We're wasting our time deluding ourselves we're building n-tier applications
with template systems. Vast column inches and energy is being burnt by PHP
coders everywhere learning template systems. But why? PHP already has the
perfect templating mechanism which looks <?php echo ("Like this!"); ?>.

"Oooo", you cry, "that doesn't seperate logic from content!". Nope but
neither does any of template systems. They only invent a new language to
handle while loops and if conditions, so those "dumb web designers" who
can't handle PHP have to learn something equally "complex" instead. How
long did it take you to learn PHP? All you need to do is get the web
designers to read the PHP manual on Control Structures;

http://www.php.net/manual/en/control-structures.php

Then you include() a PHP file which contains HTML and a little PHP for
the if's and loops.

AwesomeTemplateEngine was inspired by the following discussion:
http://www.sitepointforums.com/showthread.php?s=&threadid=67849
- read the comments by voostind

Flame the author here:
http://forum.pinkgoblin.com/viewtopic.php?p=325#325
Back to top
View user's profile Send private message
Brian
Smarty Rookie


Joined: 29 Apr 2003
Posts: 21

PostPosted: Mon Jun 09, 2003 11:29 pm    Post subject: Reply with quote

It's hard to find the time to make a fast and usable caching engine whilst working 8.5 hours a day and commuting 4. It's going to be a long summer.

Anyway, thanks for your comments. I think the dead horse has been beaten so much that he doesn't even look like a horse anymore, but rather, a big mass of bloody pulp Razz

Dozens of people have written me and expressed their appreciation for the article and the concepts presented therein. I use the template class there (which is comparable in size to the AwesomeTemplateEngine Razz) in a large project that is about 95% done (the nice thing was that switching from bTemplate was only a matter of changing the templates, NOT any of the php code, because I kept the API the same). Others have already sent improvements and claim to be using the class in their projects.

What I'm trying to say is... I don't think there's anything else I can do to get my point across -- if you don't understand it by now, you probably never will. If you do understand it but disagree with it, nothing I can say will change your mind.

So I'm going to unsubscribe myself from this thread and bow out of the discussion Smile Of course, I'll still be around here since we use Smarty at work (so I'm working with it in some form or another for 8 hrs a day).
Back to top
View user's profile Send private message Send e-mail
harryf
Smarty n00b


Joined: 12 Jun 2003
Posts: 2

PostPosted: Thu Jun 12, 2003 1:06 pm    Post subject: Reply with quote

As the author of AwesomeTemplate (it was ages ago and not meant to taken seriously - I'd need to add output buffering for it to be serious Wink) from just I'd add some thoughts (as I've raved against Smarty in the past but have since mellowed - becoming a father has helped alot) and to give Jason the idea I'm stalking him.

Anyway, my main reason for not likely 99% of PHP template engines today stems from laziness. For me the process of "binding" between HTML and PHP should ideally be the equivalent of having a database persistance layer. Projects like DB_DataObjects make it possible for me to avoid having to write any SQL in my PHP; DataObjects explores my database schema then gives me an API that's a massive time saver. That may seem like a confusing point of view but if I have to write a PHP script which maps PHP variables to a template, I feel I'm wasting my time. Most of my templating blues comes from eZ publish 2.2.x which is not as well designed as Smarty but approaches the same level (templates can be compiled / cached etc.).

The point here is I think PHP badly needs to look at where the competition is going (ASP.NET templates and now Sun with Java Server Faces) and some steps in the same direction (I can recommend ASP.NET in a Nutshell if you're looking to do some research). I've made an attempt to demonstrate ASP.NET templates in PHP terms here on Sitepointforums. Note this is a very basic example and I'm not suggesting compiling the template at "runtime" is a good idea.

The approach of ASP.NET is to use the template to instantiate "code behind" objects and assign data defined in the template (using HTML attributes) to object attributes. The code we then write to make the page dynamic uses the objects that the ASP.NET parser has instantiated to modify the page.

From a PHP perspective, if you have a template like;

Code:

<div align="center">
    <img id="myImage" scr="http://www.php.net/logo.png" runat="server">
</div>


The compliled version of this page might be something like;

Code:

<div align="center">
    <?php
    $myImage =& new HTMLImage();
    $myImage->src="http://www.php.net/logo.png"
    ?>
</div>


(note very simple example here - we'd probably need a general $page object which the HTML and the $myImage object are assigned to).

Some code which "controls" this "view" might be;

Code:

<?php
if ( isset ($_GET['hideImages']) ) {
    $myImage->hidden = "true";
}
?>


The advantages of doing this a many. First we've eliminated all control structures from the template and the syntax basically just HTML (which appeals to designers).

Also I now no longer have to write any HTML (at least not in my PHP) because I've effectively got a "persistance layer" for generating HTML.

Much more importantly, with a standard libary of HTML widgets (for basic tags and other more complex widgets such as a calendar) and a syntax which is accepted by tool vendors, it becomes possible to have "drag and drop" page designers (which is exactly what MS have with Web Matrix (free) and VS plus Sun a planning a blitz next year I think to introduce a whole load of new tools for Java along the same lines).

The core of Smarty could be a very good place to start (there's some cross over with HTML_Template_Flexy BTW) but the big thing is having a library of classes for HTML tags and more complex widgets. [url=phphtmllib.newsblob.com/]phpHtmlLib[/url] is perhaps a very good basis for the class library although it needs many more widgets to match the .NET system.web.ui.webcontrols namespace. The objects it creates also need to be aware of their "context" within a page hierarchy, so it becomes possible to access them "externally". For example if a page looks something like this in PHP code;

Code:

$page = new HTMLPage();
$title = new HTMLTitle('Powered by PHP');
$page->addHead($title);
$table = new HTMLTable();

while ( $row = mysql_fetch_array($result) ) {
    $table_row = new HTMLRow();
    $table_row->bindColumns($row);
    $table->add($table_row);
}

$page->add($table);


The problem here is how to you access individual rows in the table? The classes probably need some methods like get_parent(), get_children(), get_by_type() etc.

Anyway - all just thoughts. I would be great to see Smarty making this happen as it's pretty much accepted as a kind of "standard" in PHP. In general PHP badly needs to catch up with ASP.NET while we're still ahead.

Phew - rant over.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Fri Jun 13, 2003 3:00 am    Post subject: Reply with quote

Welome HarryF, and thanks for visiting us here and providing your comments. I have to say that when I first ran across your AwesomeTemplates code that it did make me chuckle Smile

I agree with you (and many that I have talked to have said the same) that the asp.net template system is very cool. It certainly has ideas that merit consideration. For various reasons, I'm not convinced that it is going to be the next big thing -- but that is another topic altogether.

It occurs to me that you might be able to do a similar thing using Smarty by writing a custom plugin library for your tags and then writing a prefilter that scans templates for tags that contain the runat="server" attribute and replace them with the correpsonding smarty template code for that tag. The Smarty template code would obviously point to your custom plugins.

Alternatively, the Smarty parser can probably be hacked to automatically do something along the same lines so that it instead of (or in addition to) scanning for its custom {} syntax it would (also) scan for HTML style syntax that contained the runat="server" attribute.

It looks to me that the asp.net templating paradigm leads to a preponderance of objects in the template layer and my gut tells me that is not a good choice, at least for PHP4. Besides, what is the benefit of have an HTML object class? All it does is template data and extending an HTML type object doesn't really make a lot of sense to me. So why not keep it as a flat function like Smarty does? That way it can be included or even inlined (using a compiler function) into the template output block without the need for new objects to be constructed at template runtime.

I wonder if any of this touches at all on this topic, Reverse Transformation which hints at an api for template parts.

Greetings.
Back to top
View user's profile Send private message
harryf
Smarty n00b


Joined: 12 Jun 2003
Posts: 2

PostPosted: Fri Jun 13, 2003 11:59 am    Post subject: Reply with quote

Time short right now so a quick answer. While I agree that parsing a page into hundreds of objects is a bad idea, in practice, looking as ASP.NET, there's only a few instances of runat="server" per page.

Although you're right for a single page there's little gain in turning an img tag into an object (apart from how it seperates logic from html), where the ASP.NET approach is really powerful is with it's widgets (which requires widgets classes of course). With relatively simply template syntax you can control the behavious of a complex widget such as a calendar class. Trying to manually achieve the same using a PHP script is hard going.

One further optimization that can be done though is to apply PHP's output buffering to the template for a further layer of "caching". ASP.NET's approach to this is to have a special syntax placed in the template which controls the caching of output e.g.;

Code:

<%@ OutputCache Duration="60" VaryByParam="none" %>


Anyway - Walt Boring, author of phpHTMLLib has submitted a request to PEAR: http://marc.theaimsgroup.com/?l=pear-dev&m=105550251105335&w=2 - think this is an important move (support appreciated).
Back to top
View user's profile Send private message Visit poster's website
cm
Smarty Rookie


Joined: 13 May 2003
Posts: 6

PostPosted: Sun Jun 22, 2003 1:46 pm    Post subject: Reply with quote

Quote:
{section name=curuser loop=$user}

in the template you have included some business logic in the template.


Well, wouldn't that be display logic in contrast to business logic? Thus, wouldn't it even be a good thing (tm) to let the template designer do that?
Back to top
View user's profile Send private message
Nate
Smarty n00b


Joined: 08 Jul 2003
Posts: 1

PostPosted: Tue Jul 08, 2003 6:37 pm    Post subject: Reply with quote

boots wrote:
First, your reasoning leaves a bit to be desired. PHP uses $ variable syntax while javascript does not. Shouldn't that get the designers squeezing the same grapes?
Not really, considering $ is a completely valid prefix for javascript variables. Rolling Eyes Razz
Back to top
View user's profile Send private message
petervroman
Smarty n00b


Joined: 11 Jul 2003
Posts: 1

PostPosted: Fri Jul 11, 2003 5:10 pm    Post subject: where is the separation of code and presentation? Reply with quote

For those of us who work with designers who not only do not know how to write any code, but also generally do not write HTML (they use products like Dreamweaver and GoLive) templating is a fantastic idea. Putting design logic in your HTML templates is not. The only people who should be writing for loops etc. are programmers. I want my designers to know how to use color to draw attention to something, and what an effective layout is. I defiitely do not want them to have to figure out how to read or write programming logic. Most of these folks went to art school and never took any programming classes, which is fine, let the programmers program and the designers design.
Back to top
View user's profile Send private message AIM Address
jc
Smarty n00b


Joined: 13 Jul 2003
Posts: 2

PostPosted: Sun Jul 13, 2003 3:22 pm    Post subject: Reply with quote

i very much like brians ideas and for simple templates i didnt see the point in all the {$var} glue so i switched to a modified version of brians class. My version integrates the caching into the main class. The caching is then enabled if you pass an optional cachetime to the constructor.

Code:

$page =& new CTemplate("news",300);
if ( !$page->is_cached() )
{
  /* snip all sql calls to fetch news posts */

  $page->set("news", $posts);
}

// call to site "backend", assign page to wrapper template
$eng->finish($page);


however i soon realised that all the extra <?=$ ?> where giving me headaches. but i still like the simple template system Brian proposed. what to do. i simply added is_compiled() and compile() functions to the CTemplate class and it now transforms {$VAR} templates into the php equivalent. is_compiled() simply compares the filemtime's to determine if the template needs recompiling. compile() preg_replaces {include "test.php"}, {foreach $items as $item}, {endforeach} and {$variable} type syntax.

Thanks to Brians article I have a homebrew PHP template engine in 183 lines (113 without comments) with caching and compiling. Writing templates in the {$DUMBASS} language is optional, and the php version will take priority if it is more up to date. Before that article I would have run a mile and assumed it was too difficult for me to be able to do this. Thanks Brian!

[edit]
here is source. Brians two template classes merged, cache api changes slightly and simple "compilation" of {$DUMBASS} script attached to front.

http://www.notdotnet.co.uk/template.phps

suggested improvements (to the regex especially) would be appreciated Smile
[/edit]
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Article Discussions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 4 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