Server-Side magazine interview, touches on D

Jacob Carlborg doob at me.com
Tue Jan 31 10:44:04 PST 2012


On 2012-01-31 16:51, Adam D. Ruppe wrote:
> On Tuesday, 31 January 2012 at 07:14:48 UTC, Jacob Carlborg wrote:
>> This is used in Rails as well. I mean, you need a way to get a value
>> from the controller into the view.
>
> Interestingly, a lot of PHP devs don't even use PHP for
> this!
>
> There's stuff like smarty in fairly wide use:
>
> http://www.smarty.net/why_use
>
>
> Though, of course, many PHP devs *do* use php for their
> templates too. (including me when I do php w/o D. I like
> the {$foo.bar} syntax in smarty but not the rest of it.)

It seems like Smarty is used mostly because of PHP being quite verbose. 
This is not the case with Ruby and specially not with the help of the 
many view helpers the Rails framework provides out of the box.

Note also that Rails provides many different template languages as 
plugins. There could well be a smarty plugin.

Looking at the syntax comparisons: http://www.smarty.net/syntax_comparison

Late's make a comparison with two Rails template languages, Erb (the 
default) and HAML.

smarty:
{$foo}

erb:
<%= @foo >

haml:
= @foo

php:
<?=$foo['bar']?>

smarty:
{$foo.bar}

In Rails you would most likely pass an object instead of an array/hash.

erb:
<%= @foo.bar >

haml:
= @foo.bar

If you actually do pass a hash it will like this:

haml:
= @foo[:bar]

Or you can implement "method_missing" (like opDispatch) in the Hash 
class and get this syntax:

haml:
= @foo.bar

smarty:
{foreach $foo as $bar}

{/foreach}

erb:
<% @foo.each do |bar| %>
<% end %>

or

<% for bar in @foo %>
<% end %>

haml:
- @foo.each do |bar|
     # indent

smarty:
{for $x = 1 to 20 step 2}
   {$x}
{/for}

haml:
- (1 .. 20).step 2 do |x|
     = x

{$foo|lower|escape}

haml:
=h @foo.downcase

To me it looks like HAML wins in every example.

Don't know if Smarty has this feature but this is so cool in Rails:

= render @posts

What happens is @posts is an array of model objects of the type Post. 
Rails will look in the directory containing all view files for a 
directory called "posts". Then it will look for a partial named 
"_post.html.haml". It will render that partial passing in the element in 
the current iteration to the partial as "post".

In Rails you are free to do whatever you want in the views (not sure if 
you can access everything you need). But you are encouraged to keep the 
Ruby code to a minimum.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-announce mailing list