Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Automatic exclusion of JavaScript

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
hughprior
Smarty Rookie


Joined: 07 May 2003
Posts: 10

PostPosted: Sat Nov 29, 2003 7:47 pm    Post subject: Automatic exclusion of JavaScript Reply with quote

Having just recovered from a nightmare Smarty episode which has been haunting my website for the last 6 months (see http://www.phpinsider.com/smarty-forum/viewtopic.php?p=6346#6346) and caused me to waste a huge amount of time and energy, I have a proposition that Smarty exclude (at least by default) all content within

<script> </script>

or

<script language="javascript"></script>

style tags.

One measure of good software design is not how well it performs under correct operation, but how user-friendly it is under less-than correct operation. On this JavaScript thing, I give Smarty a fail.

It's not (I think) difficult to do, and it would make life easier for the poor unfortunate Smarty user which falls into the JavaScript trap.

Although Smarty talks about templates, in fact, in it's most basic form, a template is simply a standard HTML file with the occasional replacement parameter in it (personally I give all my templates an .htm extension which makes code coloring more automatic). If the poor unfortunate user happens to have used some particular HTML which conflicts with Smarty (i.e. JavaScript with curly braces), then Smarty will crash in the most unhelpful manner.

IMHO.


Hugh Smile
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Dec 01, 2003 7:50 am    Post subject: Reply with quote

Smarty doesn't know anything about the output you want to generate. It's not limited to HTML at all.

If you really don't need smarty vars within <script> tags you should write a custom prefilter which makes sure Smarty compiler excludes these script parts.
Back to top
View user's profile Send private message
hughprior
Smarty Rookie


Joined: 07 May 2003
Posts: 10

PostPosted: Tue Dec 02, 2003 6:39 am    Post subject: Reply with quote

andre wrote:
Smarty doesn't know anything about the output you want to generate. It's not limited to HTML at all.

If you really don't need smarty vars within <script> tags you should write a custom prefilter which makes sure Smarty compiler excludes these script parts.


Oh, come on! The whole basis of Smarty is that it is a template engine for working with PHP and HTML. Agreed, it might well be usable for lots of other things, but the PHP+HTML combination is the one it is mostly used for.

Even if that is NOT the case, this is what I AM USING IT FOR. Therefore, if it is rubbish at coping with that, then I'm sorry, but for me, it is rubbish at what I need it for.

And to suggest I need to write a prefilter! Pah!! Let Smarty get it's act together.

I have tried all the solutions proposed in the earlier thread, and each of them sucks (hence starting this thread). The least awful is to surround each of the <script></script> tags with {literal}{/literal}, but I have many pages where there are lots of scripts. And I am used to using a supposedly more simple template solution which has NO problem at all with JavaScript, so to me Smarty in this area is a big step BACKWARDS.

Therefore, for me, even though in general I am a fan of Smarty, on the <script> thing, even a much more basic template parser wins hands-down over Smarty.

I say again, let Smarty by DEFAULT ignore all content between <script></script> tags.
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: Tue Dec 02, 2003 8:11 am    Post subject: Reply with quote

huhgprior,

You need a custom prefilter but probably should just change delimiters and be done with it. Auto scanning for <script> tags as a default feature--in addition to scanning for smarty tags--is not as generally useful or beneficial as you imply. I for one am against it. Perhaps I am biased as virtually none of my javascript code is ever processed by Smarty and I wouldn't want the extra parsing requirements to slow down my pages--by default. Besides, some use < and > as delimiters and many already do custom prefilter scanning--this would pose problems for them.

You are right that sometimes developers run afoul of the escaping issue and FWIW, a new section was recently added (currently in cvs) to the designer's basic syntax chapter of the manual which covers the basics of escaping smarty parsing. It doesn't add anything new to what was already availble, but hopefully will more easily catch the attention of those readers who are content with less than a thorough read of the instruction manual.

IMHO, Smarty isn't about spoon feeding every imaginable convenience pre-built and pre-configured for the developer--thank goodness. Instead, I see it as a more general tool whose beauty lies in its flexibility. With Smarty you can build your particular conveniences to meet the needs of your project. Indeed, you can typically customize it to meet your needs no matter how crazy and despite what others may suggest.

Have fun,
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue Dec 02, 2003 2:14 pm    Post subject: Reply with quote

Here's a plugin which should give you an idea how to add the feature you want to your Smarty:
Code:

  function smarty_prefilter_excludescript($tpl_source, &$smarty) {
    $ld = $smarty->left_delimiter;
    $rd = $smarty->right_delimiter;
   
    $return = preg_replace('!(<script[^>]*>.*?</script[^>]*>)!s', $ld."literal".$rd."\\1".$ld."/literal".$rd, $tpl_source);
   
    return $return;
  }


Just load the prefilter after instantiating Smarty like this:
Code:

  $smarty = new Smarty();
  $smarty->load_filter('pre', 'excludescript');
  $smarty->display("template.tpl");


Using this prefilter all <script> tags will be ignored by Smarty.

The wonderful thing about Smarty is that it's so flexible Wink
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue Dec 02, 2003 2:17 pm    Post subject: Reply with quote

If you create a custom class you can register the filter in it's constructor automatically:

Code:

  class MySmarty extends Smarty {
    function MySmarty() {
      $this->Smarty();
      $this->load_filter('pre', 'excludescript');
    }
  }

  $smarty = new MySmarty();
  $smarty->display("template.tpl");

Back to top
View user's profile Send private message
chris
Smarty n00b


Joined: 18 Apr 2003
Posts: 2

PostPosted: Thu Dec 04, 2003 2:10 pm    Post subject: Reply with quote

My 2 cents:

I have some template where part of my javascript code is generated with Smarty, So this modification would brake my page.
The andre answer is better.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
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