Hi - I'm new to Drupal and don't mean to start a war over this.

Curious to hear opinions of why use one or the other of these template engines.

Performance, logical organization of templates, and ease of maintenance are my primary considerations.

Thanks
Graham

Comments

wpd’s picture

There is little difference between the two. The drupal smarty template engine is a copy of the phptemplate.
I use the Smarty version because:
- I do not like php in my templates (I really hate: <?php print $whatever; ?>)
- I and my CSS designer already know smarty
- I do not want my CSS designer writing php ;)

PHPTemplate is more popular with the drupal.org crowd, so some of the documentation assumes you are using that.

White Paper Designs

ardas’s picture

>> The drupal smarty template engine is a copy of the phptemplate.

As I know the main advantage of PHPTemplate is that there is no parser which makes site to load longer and use more memory.
Is there any parser in Smarty as it was in XTemplate ?

wpd’s picture

Smarty compiles the templates the first time they are run into straight PHP files.
After the initial run, it is straight PHP that is run.
I think the performance is comparable after the first run; however, I believe most drupal installs will be limited by the database speed not CPU power. In general, performance is impossible to discuss without specifics.

White Paper Designs

Dublin Drupaller’s picture

interesting discussion.

I'm probably guilty as charged for adding many phptemplate snippets to the handbook, so I'm one of those "drupal.org crowd" people you're talking about, but, what you're saying is an interesting point i.e. for a CSS guy or Graphic designer to embrace Drupal, he/she would probably connect quicker with a smarty based theme, rather than a phptemplate one.

having said that...it could also be argued that the CSS guy should be just writing CSS and shouldn't need to write PHP. besides, if you and your CSS designer already know Smarty (which is essentially php without the angled brackets ), what's the problem?

The leap across to working with phptemplate themes couldn't be that huge, could it?

Smarty snippet:

<?php
  {if count($secondary_links)}
    <ul id="secondary">
    {foreach from=$secondary_links item=link}
      <li>{$link}</li>
    {/foreach}
    </ul>
  {/if}
?>

phptemplate snippet to do the same:

<?php if (count($secondary_links)) : ?>
    <ul id="secondary">
    <?php foreach ($secondary_links as $link): ?>
      <li><?php print $link?></li>
    <?php endforeach; ?>
    </ul>
  <?php endif; ?>

..horses for courses stuff, I suppose.

My two cents on the matter is that the ability for Drupallers to use and share snippets quicker than sharing modules and themes via Drupal.org led me to getting stuck into phptemplate as a natural progression. It just made more practical sense. Whether it's for design or from a "how do I put that there?" perspective.

As an aside, I wonder how difficult it is to convert the various snippets already on drupal.org into "smarty" snippets?

If it's something as simple as that to increase the attraction of Drupal to the design community, it's worth nudging, me thinks.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

wpd’s picture

I believe the template engine choice comes down to personal preference. I am just listing my preferences ;)

having said that...it could also be argued that the CSS guy should be just writing CSS and shouldn't need to write PHP. besides, if you and your CSS designer already know Smarty (which is essentially php without the angled brackets ), what's the problem?

Someone has to write the .tpl files. Especially if you use custom modules and/or flexinode. The reason I do not use PHPTemplate is that is too powerful. There is no way to restrict the php code to only be used to present the data. For example, many snippets on drupal.org subvert access control and/or use poor defensive coding measures. Using, smarty you can be relatively sure there is no database access or other non-view related working going in your theme.

I agree that PHP and smarty have a similiar syntax for the most part, but consider someone who only knows html and css. They are not programmers. If you send them to php.net it is overwhelming. Smarty on the other hand, is orders of magnitude smaller.

Your comparision of smarty/phptemplate is slightly biased. The smarty version is a direct copy of the phptemplate version. Consider formatting text or other display functionality.

$title = "Get first characters from this title"; 
{$title|truncate:8:"..."}
Result:
Get first...

How do you do that in PHP? Smarty also has other view specific functions that are easy to use.

It sure will not hurt to promote the capability to use smarty with drupal. That is why I replied to this thread ;)

White Paper Designs

Dublin Drupaller’s picture

I see what you mean...not much difference, but, the syntax is easier to remember for smarty.

Smarty snippet:

$textfield = "get first characters from this title";
{$textfield|truncate:8:"..."}
result:
Get first...

Phptemplate snippet

<?php $textfield = "get first characters from this title";
print (truncate_utf8(strip_tags($textfield), 9));
print "...";?>

Result:
Get first...

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

mevkok’s picture

HAHAHA... of course PHP is better xD:

PHP:
-Function used:

function trunc($text, $start, $count = false, $prefix = false){
	if($count) $count = $start+$count;
	return substr($text, $start, $count) . $prefix ? $prefix : "";
}

-Method: $textfield = "get first eight characters from this title"; echo trunc($textfield, 0, 8, "...") ;

-Result: "get firs...";
.
.
.
.
SMARTY:
-Function used:

function smarty_truncate($text, $count, $prefix = false){
	return substr($text, 0. $count) . $prefix ? $prefix : "";
}

-Method:
at php calling smarty:
$smarty->assign('textfield', "get first eight characters from this title");
at the template:

{$textfield|truncate:8:"..."}

-Result: "get firs...";

Jaypan’s picture

You realize you're responding to a post from 8 years ago?

WorldFallz’s picture

not only that, but twig is what what selected for d8 so smarty isn't even on the radar, lol.

sepeck’s picture

PHPTemplate is more popular with the drupal.org crowd, so some of the documentation assumes you are using that

Well, phpTemplate was known to be slated as the 4.7 theme engine for a year, so the interest and direction went that way. That said there is a smarty section of the handbook. Not using Smarty I don't know how up to date it is, but if people add child pages there, I am more then happy to approve them out of moderation.

The flexibility of the engines allows Drupal to serve a more diverse audience. :)

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

gdclarke’s picture

Thanks for the thoughtful posts. I'll use SMARTY for many of the reasons stated by the others.

With respect to the posted SMARTY PHPTemplate example, IMHO the SMARTY example is better as it only has ONE set of <?php ?> tags. I find having <?php ?> tags sprinkled through a template makes the code difficult to read and makes debugging more difficult. One of the things that bothered me about ColdFusion.

Some other things I like about SMARTY.

+ Well documented
+ Good support
+ Big committment to SMARTY from PHP community

Thanks

Graham

tclineks’s picture

Hello, I'm the maintainer of the Smarty Engine.

I'd just like to comment that the Smarty example shouldn't have *any* <?php tags.
Dublin Drupaller may have simply done this to have the better formatting -- the {code} formatting doesn't highlight.

Also, although the documentation is lacking I'm pretty quick with support. Sending me a message through the contact form here will usually get a quick response.

I'm also completely open to suggestions of any nature, from wording in a README to coding style.

As has been mentioned here, it is largely a direct port of phptemplate and referencing it is the thing to do in many cases.

There are differences though.
It is preventative of placing powerful php code directly into the templates (ignoring {php} tags for the moment -- I avoid them whenever possible) but I don't think many people imagine it belonging there anyhow. However, it's very easy to add custom tags to move processing out to a function in your theme-specific smartytemplate.php file.
It creates a little extra work (a few simple lines) but it is proper in its enforcement of logic separation.

With finals almost over I can get some added niceness and some needed documentation released.
I have come up with some usability improvements while working on other projects and am eager to get them out there.

If only drupal cvs repo./packaging policy allowed me to have smarty coupled, wouldn't have to deal with berlios =)

Anyhoo,
Thanks for using my Smarty theme-engine,

Travis

CheezItMan’s picture

Is there a guide to the Drupal 6 Theme Engine?