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

Smarty wants to parse a javascript!
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Tue Apr 29, 2003 8:29 am    Post subject: Smarty wants to parse a javascript! Reply with quote

Hi!

Sometimes I use Javascript and some Javascript use something like

if(expr) {...

but if I use something like this, smarty gives an Error:

Quote:
Fatal error: Smarty: [in mytemplate.tpl line 417]: syntax error: unrecognized tag: document.write...


seems that smarty doesn't react on HTML-Comments: <!--

What could I do against it?

Andreas

________
BMW N53

Last edited by andreas on Fri Feb 04, 2011 9:00 am; edited 2 times in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue Apr 29, 2003 8:40 am    Post subject: Reply with quote

Four options:

1) since you can't use { or } (they are delimiters) you can use the built in {ldelim} (outputs a {) and {rdelim} (outputs a })

2) You can wrap your javascript in {literal} tags:

Code:
{literal}
function myfunc() {
}
{/literal}


Smarty ignores everything between {literal} ... {/literal}, so you would have to do the following if you are writing dynamic code:

Code:
{literal}
function myfunc() {
return '{/literal}{$myvar}{literal}';
}
{/literal}


3) If your script is simple and is static, you might be able to put it into a configuration file.

4) If your script is static, you can collect it all into a .js file and use the <script> src parameter to include the file.
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Tue Apr 29, 2003 9:46 am    Post subject: Reply with quote

This really should be stick or something because I think this has been asked atleast twice now
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
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 Apr 29, 2003 11:03 am    Post subject: Reply with quote

Yeah, there a few topics that should be made sticky.

I also wish there was a link to the documentation in the links bar above Smile


Last edited by boots on Tue Apr 29, 2003 11:04 am; edited 1 time in total
Back to top
View user's profile Send private message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Tue Apr 29, 2003 11:03 am    Post subject: Reply with quote

Sorry, the forum is that new so I did not search here.

But I have something else Wink

I include a header.tpl into my scripts, but my templates all need different &lt;title&gt; and a special &lt;img src=&gt;. That's no problem, I can use {include} to pass these 2 variables(title-string, img-name), but sometimes I have one or more javascripts which have to be startet from &lt;body&gt;-tag by onclick. if I had just one it would be no problem, I could pass it as title-name..., but if I have 3 scripts to load? OK, I could pass 3 or more special variables by {include}.
Is there a better way to do this?

Thank you!

Andreas

________
iolite portable vaporizer


Last edited by andreas on Fri Feb 04, 2011 9:00 am; edited 2 times in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Apr 29, 2003 2:44 pm    Post subject: Reply with quote

AZTEK wrote:
This really should be stick or something because I think this has been asked atleast twice now


Is the FAQ not enough??
Back to top
View user's profile Send private message Visit poster's website
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Tue Apr 29, 2003 2:49 pm    Post subject: Reply with quote

Most people see the word FAQ and ignore the link. Sadly enough Sad
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
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: Tue Apr 29, 2003 3:06 pm    Post subject: Reply with quote

AZTEK wrote:
Most people see the word FAQ and ignore the link. Sadly enough


So you think they won't ignore the "STICKY" entry? Very Happy

Perhaps the error message if somebody misuses "{" or "}" should be rewritten to: "RTFM" Very Happy
Seriously. Perhaps you could rewrite it from
Quote:
Fatal error: Smarty: [in mytemplate.tpl line 417]: syntax error: unrecognized tag: document.write...

to something like
Quote:
Fatal error: Smarty: [in mytemplate.tpl line 417]: syntax error: unrecognized tag: document.write... use {ldelim} and {rdelim} for literal use of { and }

Perhaps this makes things clearer for newbiews Rolling Eyes
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Apr 29, 2003 3:37 pm    Post subject: Reply with quote

It's not immediately known to the parser that this is the problem. This same error happens any time the syntax is unknown, not just with javascript blocks.

Also, if you're using a different delimiter like <% %> then that error message wouldn't make sense.

Monte
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 Apr 29, 2003 3:47 pm    Post subject: Reply with quote

Its worth noting that the FAQ link on the forum site gives FAQ for forum operation only. Worth adding links for often searched for references?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Apr 29, 2003 3:56 pm    Post subject: Reply with quote

Actually the entire Smarty navigation bar should be on the page so the forum looks more integrated rather than a separate site. Something I haven't had time to do, maybe Tom could since he did the template?
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 Apr 29, 2003 4:11 pm    Post subject: Reply with quote

mohrt wrote:
Actually the entire Smarty navigation bar should be on the page so the forum looks more integrated rather than a separate site. Something I haven't had time to do, maybe Tom could since he did the template?


yaaaah!

I think Tom said it would be too hard ( http://www.phpinsider.com/smarty-forum/viewtopic.php?t=3 )

Just teasing. Twisted Evil
Back to top
View user's profile Send private message
Nexus
Smarty n00b


Joined: 02 Nov 2004
Posts: 1

PostPosted: Tue Nov 02, 2004 4:31 am    Post subject: Reply with quote

boots wrote:
Four options:
1) ...
2) ...
3) ...
4) ...
Hi, I was just delving into this problem and thought I'd point this out here in case it helped someone. Option #5.) (worth mentioning) is simply changing the smarty delimiters to something more compatible with HTML. As was pointed out elsewhere, you can extend the smarty class, or just reset the delim elements.
Code:
<?php
class MySmarty extends Smarty {
   function MySmarty() {
      $this->left_delimiter   = '<!--{';
      $this->right_delimiter   = '}-->';
      //...
   }
}
?>

Code:
$smarty->left_delimiter = "<!--{";
$smarty->right_delimiter = "}-->";
And with that, my smarty world is perfect and complete. BTW, great forum. This one solution has solved a template war argument, and freed me up to concentrate on more important things.

Someone also noted that getting errors due to "javascript" amounted to "misusing" the curly brackets. I cringed at that. Definitely the programmer in you talking. There should really be a "misuse" present in basic html that should break a template system I feel. I've been feeling like a dork not to have figured out a way to get rid of this limitation, as I'm trying to wrap up a product I don't want to have repeated "why is my template bombing the app" questions on for no reason.

Best Regards,
~ Nexus
Back to top
View user's profile Send private message
liuchangjohn
Smarty n00b


Joined: 05 Jan 2005
Posts: 1

PostPosted: Wed Jan 05, 2005 6:46 am    Post subject: A prefilter for nicer javascript escape Reply with quote

Hi, this is my first post here Very Happy Though I've been using smarty for quite a while and it really impressed me. One of the few things that pissed me off is the javascript escape, which are generally done in two ways,
1.
Code:
{literal}
function myfunc() {
return '{/literal}{$myvar}{literal}';
}
{/literal}


That's a lot of code to output one simple $myvar! Isn't template languages supposed to be simple and concise?

2.
Code:
$smarty->left_delimiter   = '<!--{';
$smarty->right_delimiter   = '}-->';


Well, why the hell should my PHP be bothered about how the template is passed, or worse, if the template contains javascript?? It's the clean seperation between presentation and logic that brought me to smarty in the first place! And, the whole template's deliminater is changed, inside HTML or javascript!.

While the above methods are bearable, I got really pissed up in a recent project that required extensive use of "danymic javascript" and I was forced to come up with the following prefilter that supports a new tag {script}. Basically in your HTML, instead of writing <script>...</script> or <script type='text/javascript'>..</script>, you write {script}{/script} and {script type='text/javascript'}..{/script}. And between the tags, the smarty deliminators are {{ and }}. So the above example becomes,

Code:
{script type='text/javascript'}
    function myfunc() {
        return '{{$myvar}}';
}
{/script}


Neat huh? Smile Well, that's what I wanted. The code is simple:

Code:
function _escape_script($matches)
{
    $type =& $matches[2];
    $script =& $matches[3];
    $script = preg_replace('/{{/', '{/literal}{', $script);
    $script = preg_replace('/}}/', '}{literal}', $script);

    $result = '{literal}';
    if (empty($type)) {
   $result .= '<script>';
    } else {
   $result .= "<script type=$type>";
    }
    $result .= $script;
    $result .= '</script>{/literal}';
   
    return $result;
}

function escape_script($tpl_source, &$smarty)
{
    // detect all {script type=...}..{/script} tags
    // s: DOTALL: . matches newline as well
    // U: UNGREEDY: match as little as possible
    $regex = '/{script(\s+type=(\S+))?}(.*){\/script}/sU';
    $new_source = preg_replace_callback($regex,
       _escape_script,
       $tpl_source);
    return $new_source;
}


And in your favourate place, add

Code:
$smarty->register_prefilter("escape_script");


And voila! I hope this will save the day of some chap like me.
Back to top
View user's profile Send private message
jayhunter
Smarty n00b


Joined: 21 Oct 2006
Posts: 1

PostPosted: Sun Oct 22, 2006 2:39 am    Post subject: Reply with quote

Anyone likes my way?

In <head> Element of your page
Code:

{literal}
<script type="text/javascript">
<!--
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
//-->
</script>

{/literal}
<!-- HeaderContent start -->
{if isset($HeaderContent)}{$HeaderContent}{/if}
<!-- HeaderContent end -->


In your other templates where you need to start sth.
Code:

{capture name=javascript}
{literal}
<script type="text/javascript">
<!--

addLoadEvent(
   function()
   {
      startMe();
   }
);

//-->
</script>
{/literal}
{/capture}

{if $smarty.capture.javascript ne ""}{assign var=HeaderContent value="`$HeaderContent``$smarty.capture.javascript`"}{/if}



Works perfect for me. It puts all my JS stuff in the head. onloads are no problem from any template that gets parsed with the head.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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