I would like to request a version of the smarty theme engine that works with Drupal 5. I haven't tried to see is the 4.7 one does, but I would be surprised if it did.

CommentFileSizeAuthor
#6 smarty.engine_0.txt3.04 KByojoe
#2 smarty.engine.txt3.63 KBdjnz
#1 smarty.engine.patch11.86 KBTeddyX

Comments

TeddyX’s picture

Version: » 4.7.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new11.86 KB

Here is a patch to upgrade smarty.engine 4.7 to 5.0. It is based on differences between phptemplates 4.7 and 5.0. It works for me, but I haven't much time to thoroughly test it.

djnz’s picture

Version: 4.7.x-1.x-dev » master
StatusFileSize
new3.63 KB

Rather than patch the existing code, I have used a different approach. For future maintability it is much better to use the PHPtemplate code to set variables etc. - all the smarty template engine needs to do is the smarty-specific stuff (assigning variables, fetching templates etc.)

So here is a replacement for smarty.engine that works with Drupal 5. Note that this should NOT go in themes/engines/smarty, the correct Drupal 5 place is sites/all/themes/engines/smarty. You will have to move the smarty directory over too of course (and templates_c etc.)

This is not really production ready for new installs, but if you are trying to upgrade an exisiting installation of a smarty theme to Drupal 5, give it a go!

Note that the attachment is a complete php file, not a patch (remove the .txt extension of course).

barretr’s picture

Assigned: djnz » Unassigned
Status: Closed (fixed) » Needs review
djnz’s picture

You don't need to do that barretr. Just put what you need in template.php (not smartytemplate.php) which is automatically included by the phptemplate_init() function.

barretr’s picture

yojoe’s picture

StatusFileSize
new3.04 KB

I like the approach of djnz very much. I ported the Garland theme to Smarty yesterday, it works very well.

I did a few changes to djnz version of smarty.engine, I would like to present and discuss with you:

  • To include phptemplate.engine, I use the drupal_get_path() function require_once drupal_get_path('theme_engine', 'phptemplate') . '/phptemplate.engine'; instead of the hardcoded path require_once './themes/engines/phptemplate/phptemplate.engine';
  • I changed the line where to look for the smarty libs from
    $smarty_path = dirname(__FILE__).'/Smarty/libs/Smarty.class.php';
    to
    $smarty_path = drupal_get_path('theme_engine', 'smarty') . '/libs/Smarty.class.php';
    This way we use the drupal_get_path() function. And, like it's written in the README of smarty.module, it's sufficient to copy just the libs directory of the Smarty download to the smarty module directory, so we look for the libs in /sites/all/themes/engines/smarty/libs and not /sites/all/themes/engines/smarty/Smarty/libs.
  • I changed smarty_init() to include 'smartytemplate.php' in the theme folder, to be compliant with the documentation at http://drupal.org/node/70247. Just calling phptemplate_init() would try to include template.php instead of smartytemplate.php.
  • djnz had commented out the two functions _smarty_callback() and _smarty_default_variables(). Why? I completed them.
  • There is an (copy and paste?) error in the _smarty_default() function, which disables the suggestions. I changed
    return _phptemplate_default($hook, $variables, $suggestions = array(), $extension);>
    to
    return _phptemplate_default($hook, $variables, $suggestions, $extension);>
  • By the way: Why do we need the additional $variables['user'] = $GLOBALS['user']; line in the _smarty_default() function? I mean, why do we need it when using Smarty as engine, but not when using PHPTemplate?
  • The Singleton-like way (&smarty_get_object()) of geting the Smarty object looks very nice at first glance, but it causes big problems in the end. This is a bit complicated, let me explain it with the following example:

    If a node (node_1) is to be rendered, the node object is first created and then passed to the Smarty object within the $variables array (_smarty_render($file, $variables)). This is usually fine. Your template, let's call it node-typeA.tpl, can access the node_1 object and finishes without problems. But now imagine that node-typeA.tpl want's to include the teaser of another node (node_2) first. What happens? Yes, while theming the teaser of that node_2, we overwrite the variables array of the (singleton) Smarty object. The result is, that after the teaser of node_2 is themed and the control flow comes back to node-typeA.tpl, we can't access the node_1 object anymore, it has been overwritten.

    That's why I commented out the static declaration of the Smarty object instance in &smarty_get_object(). This way a new Smarty object is created with each _smarty_render() call. Well, if you agree, that we can't use the Singleton pattern here, we should remove the ampersand from the function name => smarty_get_object().

  • I did some more changes to the &smarty_get_object() function. I changed the way the paths are setup using some drupal functions path_to_theme(), path_to_engine(), ... I also registered a plugins and a templates directory in the current theme folder. This way themes can bring their custom smarty functions and modifiers.
  • I removed the two theme functions smarty_function_theme() and smarty_function_theme_links() from smarty.engine (they don't belong here) and moved them to separate files in the plugins directory (sites/all/themes/engines/smarty/plugins/function.theme.php and function.theme_links.php). This is the way the Smarty manual describes how custom functions should be registered.
lvthunder’s picture

I'm getting errors in the smarty themes. I've tried 3 of them and they all say roughly the same thing.
Here's the message.

Smarty error: [in themes/greypop_smarty/page.tpl line 10]: syntax error: unrecognized tag 'theme' (Smarty_Compiler.class.php, line 580) in C:\Users\Stephen\workspace\drupal\themes\engines\smarty\libs\Smarty.class.php on line 1095.

Also instead of primary links I get the word array. Can someone help me.

yojoe’s picture

As I said in my post, I moved the two functions smarty_function_theme() and smarty_function_theme_links() into separate files in the plugins dir. I didn't included those two files in my post. I can upload them here too, if you want. Or you copy & paste them from the last lines of the file in post #2 and name the files function.theme.php and function.theme_links.php and put them in the plugins directory (sites/all/themes/engines/smarty/plugins).

robin monks’s picture

I've tried this patch, and still get array in the menu bar even with the function.theme.php and function.theme_links.php in the engines/smarty/plugins directory. Ideas?

Robin

robin monks’s picture

Found the answer, seems Drupal 5 handles primary/secondary links a bit differently:

  {if count($secondary_links)}
    <ul id="secondary">
    {foreach from=$secondary_links item=link}
      <li><li><a href="{$link.href}" class="secondary-menu-item menu-item">{$link.title}</a></li></li>
    {/foreach}
    </ul>
  {/if}

  {if count($primary_links)}
    <ul id="primary">
    {php}
    /*print_r($this->get_template_vars());*/
    {/php}
    {foreach from=$primary_links item=link}
      <li><a href="{$link.href}" class="primary-menu-item menu-item">{$link.title}</a></li>
    {/foreach}
    </ul>
  {/if}

Seems to work just fine.

Robin

robin monks’s picture

Status: Needs review » Reviewed & tested by the community

+1 Tested & Works on Drupal 5.1, at the very least this is enough to start a 5.x branch

Robin

djnz’s picture

I am delighted that there is real interest in making Smarty work for Drupal in the best way possible. I am disappointed though that the maintainer/developers of this project do not seem to be so interested - either in a version for Drupal 5 or other improvements.

This has happened before with Drupal and Smarty, and so I set up the wgSmarty project to get things going. Then the 'official' Drupal Smarty project caught up and wgSmarty was redundant...

Now it looks like it is time to get wgSmarty going again.

tclineks’s picture

I initially like the simplified approach -- I'd have to look into if this would affect custom function registration but offhand it should be workable.

I think the best plan is to
a. get a 5-supported version out the door asap
b. and then release an updated version that allows more control in the form of custom function registration (I have this code worked out but never got it committed, I'd be happy to share).

Frankly I don't use Drupal or Smarty in my projects anymore and am open to passing the maintenance torch to someone willing.

If interested send me a message and we can work out what's best.

tclineks’s picture

djnz and I are in communication and should have a 5.0 version out shortly!

djnz’s picture

HEAD now contains a merge of my code posted below, some 'production ready' improvements I had done, those of yojoe's comments I was able to incorporate (more documentation on those coming), and some of the helper function pre-registration code that tclineks had done for 4.7.

Amazingly, most of this seems to work (the plugin function pre-registration being the bit that needs more effort to bring my code and Travis's together) so it is nearly ready for tagging a 5.x.1.x release - testing gratefully received.

lvthunder’s picture

I'm having a problem with the primary links and the title link. It's a little strange.

OK I'm at my homepage http://photo.lvthunder.com and all the links for the title and primary links work.
If I go to say http://photo.lvthunder.com/view/photo101 and the title link is http://photo.lvthunder.com/view/photo101 and the same primary link is http://photo.lvthunder.com/view/view/photo101

Here is the source of the HTML

<div id="header">
      <a href="" title="Index Page"><img src="/themes/greypop_smarty/logo.png" alt="Photography by Stephen" /></a>

      <br class="clear" />
</div>
<div id="top-nav">
  
      <ul id="primary">
          <li><a href="view/photo101" class="primary-menu-item menu-item">Photography 101</a></li>
        </ul>
  </div>

And here is a portion of my page.tpl is

  {if $logo}
  <a href="{url}" title="Index Page"><img src="{$logo}" alt="{$site_name}" /></a>
  {/if}
  {if $site_slogan}
    <span id="site-slogan">{$site_slogan}</span>
  {/if}
  <br class="clear" />
</div>
<div id="top-nav">
  {if count($secondary_links)}
    <ul id="secondary">
    {foreach from=$secondary_links item=link}
      <li><a href="{$link.href}" class="secondary-menu-item menu-item">{$link.title}</a></li>
    {/foreach}
    </ul>
  {/if}

Does anyone have any suggestions?

Also when do you think we will have a released or at least a dev snapshot of this theme engine.

yojoe’s picture

I created a new project for the port of the Garland theme to the smarty theme engine: http://drupal.org/project/garland-smarty
Anybody who wants CVS access and help maintaining this port please let me know!

@djnz: Now that the changes are in HEAD, could you please setup a drupal 5.x development release of the smarty engine?

Atlanticom’s picture

I am very interested in getting this patch up and finished. At the moment there seems to be a lack of progress, and so i would like to offer my support. I've got 8 years experience in PHP and website programming; i would only be interested in getting the SMARTY template engine working with Drupal 5 however.

GeekElectro’s picture

The X-Cart shopping cart utilizes smarty tags. I would think quite a few X-Cart users would be interested in more easily integrating the themes between X-Cart and Drupal by using smarty tags.

djnz’s picture

Assigned: Unassigned » djnz
Status: Reviewed & tested by the community » Fixed

If X-Cart uses smarty as its theme engine, I would have thought it is quite easy to adapt an X-Cart theme for use with the Drupal Smarty Theme Engine, but as X-Cart is paid-for software (and presumably its themes are paid-for too) I don't think you will find any of the Open Source developers that do all the work on Drupal to be very interested in this.

This feature request should have been closed on the release of Smarty 5.x-0.1, (I am about to release a v6) so I am marking it fixed now.

djnz’s picture

Status: Fixed » Closed (fixed)

Fixed? I should have set it to closed.

Status -> closed.

Status: Needs review » Closed (fixed)