View previous topic :: View next topic |
Author |
Message |
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Apr 29, 2003 8:40 am Post subject: |
|
|
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 |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Tue Apr 29, 2003 9:46 am Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Apr 29, 2003 11:03 am Post subject: |
|
|
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
Last edited by boots on Tue Apr 29, 2003 11:04 am; edited 1 time in total |
|
Back to top |
|
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
Posted: Tue Apr 29, 2003 11:03 am Post subject: |
|
|
Sorry, the forum is that new so I did not search here.
But I have something else
I include a header.tpl into my scripts, but my templates all need different <title> and a special <img src=>. 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 <body>-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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Apr 29, 2003 2:44 pm Post subject: |
|
|
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 |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Tue Apr 29, 2003 2:49 pm Post subject: |
|
|
Most people see the word FAQ and ignore the link. Sadly enough _________________ "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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Tue Apr 29, 2003 3:06 pm Post subject: |
|
|
AZTEK wrote: | Most people see the word FAQ and ignore the link. Sadly enough |
So you think they won't ignore the "STICKY" entry?
Perhaps the error message if somebody misuses "{" or "}" should be rewritten to: "RTFM"
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 |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Apr 29, 2003 3:37 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Apr 29, 2003 3:47 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Apr 29, 2003 3:56 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Apr 29, 2003 4:11 pm Post subject: |
|
|
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. |
|
Back to top |
|
Nexus Smarty n00b
Joined: 02 Nov 2004 Posts: 1
|
Posted: Tue Nov 02, 2004 4:31 am Post subject: |
|
|
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 |
|
liuchangjohn Smarty n00b
Joined: 05 Jan 2005 Posts: 1
|
Posted: Wed Jan 05, 2005 6:46 am Post subject: A prefilter for nicer javascript escape |
|
|
Hi, this is my first post here 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? 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 |
|
jayhunter Smarty n00b
Joined: 21 Oct 2006 Posts: 1
|
Posted: Sun Oct 22, 2006 2:39 am Post subject: |
|
|
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 |
|
|