View previous topic :: View next topic |
Author |
Message |
hbilgen Smarty n00b
Joined: 12 Sep 2003 Posts: 4
|
Posted: Fri Sep 12, 2003 1:54 pm Post subject: How to use Javascript codes in template files? |
|
|
Hi,
I'm using javascript codes in my template files, but Smarty recognizes function curly braces '{' of functions as variables and gives error.
How to use those javascript function codes in template files? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
hbilgen Smarty n00b
Joined: 12 Sep 2003 Posts: 4
|
Posted: Fri Sep 12, 2003 2:44 pm Post subject: Thanks |
|
|
Thanks a lot,
I solved my problem by your answer. |
|
Back to top |
|
irbrian Smarty Rookie
Joined: 08 Oct 2003 Posts: 32 Location: USA
|
Posted: Tue Feb 24, 2004 1:33 am Post subject: HTML Style tags: Using { } curly braces in Style Header |
|
|
It took me awhile to find the answer to this question because I was looking for information on using curly braces ({ }) for the <STYLE></STYLE> tags in HTML. That particular issue is not addressed here, so I'm stating it below purely for the convenience of those who may try to find the answer to this problem and are using the Search feature.
<STYLE /> tags in HTML may cause an error such as:
Code: | Fatal error: Smarty: [in template.tpl line 6]: syntax error: unrecognized tag
|
Of course, this is because the curly braces { and } are the delimeters for smarty tags, and thus Smarty is expecting a valid tag between them, when in fact your template contains Cascading Style Sheets (CSS) formatting data.
To correct this problem, use the {literal}{/literal} Smarty tags:
Code: |
<style>
{literal}
TD {border: none; font-size: 8pt}
{/literal}
</style>
|
|
|
Back to top |
|
aearon Smarty n00b
Joined: 03 Aug 2004 Posts: 2
|
Posted: Tue Aug 03, 2004 7:50 am Post subject: |
|
|
here's a new little problem, do you know of any way to do this in a more practical way?
what it want in a template is something like this:
Code: | <style>
font {
font-family: arial;
color: #{$settings->settings.color_menu1font};
}
</style |
now as you can see i couldn't just use {literal} tags around the whole style block... because i want smarty to interpret the part within the css tags...
my current solution is just escaping all the css tags indivually like this:
Code: | <style>
{literal}font {{/literal}
font-family: arial;
color: #{$settings->settings.color_menu1font};
{literal}}{/literal}
</style |
but that's impractical and looks confusing... any idea? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Aug 03, 2004 2:15 pm Post subject: |
|
|
use {ldelim} and {rdelim} in place of the curly braces. |
|
Back to top |
|
aearon Smarty n00b
Joined: 03 Aug 2004 Posts: 2
|
Posted: Tue Aug 03, 2004 3:59 pm Post subject: |
|
|
better! thanks |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Wed Aug 11, 2004 10:02 am Post subject: |
|
|
mohrt wrote: | use {ldelim} and {rdelim} in place of the curly braces. |
if i do something like that and i change the delimiters in future, the wholy code will be "destroyed"...
is there a better way to do it? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Aug 11, 2004 1:51 pm Post subject: |
|
|
If you change your delimiters in the future, you'll have much bigger problems than having to change the {delim} tags. I would recommend picking a delimiter syntax and sticking to it. If you don't use the default "{" "}" delmiters, then escaping the javascript braces would likely not be necessary.
Another alternative is to create your own custom template functions:
{lbrace}
{rbrace}
Which would echo "{" and "}" repectively, regardless of what template delimiters you are using. |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Wed Aug 11, 2004 3:50 pm Post subject: |
|
|
mohrt wrote: | If you change your delimiters in the future, you'll have much bigger problems than having to change the {delim} tags. I would recommend picking a delimiter syntax and sticking to it. If you don't use the default "{" "}" delmiters, then escaping the javascript braces would likely not be necessary.
Another alternative is to create your own custom template functions:
{lbrace}
{rbrace}
Which would echo "{" and "}" repectively, regardless of what template delimiters you are using. |
nice idear. thanks |
|
Back to top |
|
SordidWarrior Smarty Rookie
Joined: 03 Sep 2004 Posts: 6 Location: Paris, France
|
Posted: Fri Sep 10, 2004 1:07 am Post subject: |
|
|
I think that an even better way is:
[php:1:ca76f150c7]<?php
class MySmarty extends Smarty {
function MySmarty() {
$this->left_delimiter = '{{';
$this->right_delimiter = '}}';
//...
}
}
?>[/php:1:ca76f150c7]
This allows to use the curly braces in javascript and styles, in your templates if it speaks about php, C, C++ or Java programing. It is very rare to need 2 successive curly braces...
Or, you coul use other rarely used sequence of strings. I just wonder if using the same delimiter for start and end delimiters would work. _________________ Vincent Vandemeulebrouck
Coding expert, smarty newbie
http://www.leguerriersorde.com/ |
|
Back to top |
|
kilimangaro Smarty n00b
Joined: 10 May 2004 Posts: 2 Location: Québec, Canada
|
Posted: Wed Apr 06, 2005 3:44 pm Post subject: Reg Exp to the rescue ! |
|
|
Try these prefilters functions, they work perfectly for us:
[php:1:c6ca584d79]function smarty_prefilter_literal_script_and_style(&$tpl_source, &$smarty) {
$pattern[] = '~<script\b(?![^>]*smarty)(.*)</script>~siU';
$replace[] = '<!-- {literal} --><script$1 $2</script><!-- {/literal} -->';
$pattern[] = '~<style\b(?![^>]*smarty)>(.*)</style>~siU';
$replace[] = '<!--{literal}--><style$1>$2</style><!--{/literal}-->';
return preg_replace($pattern, $replace, $tpl_source);
}
class MySmarty extends Smarty {
function MySmarty() {
$this->register_prefilter('smarty_prefilter_literal_script_and_style');
}
}[/php:1:c6ca584d79]
It brace all <script> and <style> tags whith {litteral} so that
Code: | <script language="JavaScript" type="text/JavaScript">
<!--
function doSomeJS() {
// blabla ...
}
-->
</script> |
... give ...
Code: | <!--{literal}--><script language="JavaScript" type="text/JavaScript">
<!--
function doSomeJS() {
// blabla ...
}
-->
</script><!--{/literal}--> |
... but ...
Code: | <script language="JavaScript" type="text/JavaScript" smarty>
<!--
showMyLayer1 = {$smarty.get.id|intval}
-->
</script> |
... give ...
Code: | <script language="JavaScript" type="text/JavaScript" smarty>
<!--
showMyLayer1 = {$smarty.get.id|intval}
-->
</script> |
Wondefull! isn't?  |
|
Back to top |
|
Rupom Smarty Rookie
Joined: 20 Jul 2005 Posts: 6 Location: Dhaka, Bangladesh
|
Posted: Sun Jul 31, 2005 1:37 pm Post subject: |
|
|
Here is an example:
{literal}
<script language="javascript">
function ab()
{
////What you need
}
</script>
{/literal}
<a href="javascript:ab();">Something you need</a>
OR
for any other needs. _________________ MA Razzaque (Rupom)
http://www.rupom.tk
http://www.rupom.info |
|
Back to top |
|
whizz_kid Smarty Rookie
Joined: 01 Aug 2007 Posts: 5
|
Posted: Wed Aug 01, 2007 11:05 am Post subject: |
|
|
does anyone have information on how to insert .php files into .tpl files? |
|
Back to top |
|
DaKaLKa Smarty Regular

Joined: 23 Apr 2006 Posts: 99 Location: Ketsch, Germany
|
|
Back to top |
|
|