Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
How to use Javascript codes in template files?
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
hbilgen
Smarty n00b


Joined: 12 Sep 2003
Posts: 4

PostPosted: Fri Sep 12, 2003 1:54 pm    Post subject: How to use Javascript codes in template files? Reply with quote

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
View user's profile Send private message
boots
Administrator


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

PostPosted: Fri Sep 12, 2003 2:05 pm    Post subject: Reply with quote

SEE:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=150

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=345


Last edited by boots on Sat Nov 29, 2003 1:58 pm; edited 1 time in total
Back to top
View user's profile Send private message
hbilgen
Smarty n00b


Joined: 12 Sep 2003
Posts: 4

PostPosted: Fri Sep 12, 2003 2:44 pm    Post subject: Thanks Reply with quote

Thanks a lot,
I solved my problem by your answer.
Back to top
View user's profile Send private message
irbrian
Smarty Rookie


Joined: 08 Oct 2003
Posts: 32
Location: USA

PostPosted: Tue Feb 24, 2004 1:33 am    Post subject: HTML Style tags: Using { } curly braces in Style Header Reply with quote

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
View user's profile Send private message Visit poster's website
aearon
Smarty n00b


Joined: 03 Aug 2004
Posts: 2

PostPosted: Tue Aug 03, 2004 7:50 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Tue Aug 03, 2004 2:15 pm    Post subject: Reply with quote

use {ldelim} and {rdelim} in place of the curly braces.
Back to top
View user's profile Send private message Visit poster's website
aearon
Smarty n00b


Joined: 03 Aug 2004
Posts: 2

PostPosted: Tue Aug 03, 2004 3:59 pm    Post subject: Reply with quote

better! thanks
Back to top
View user's profile Send private message Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Wed Aug 11, 2004 10:02 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Aug 11, 2004 1:51 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Wed Aug 11, 2004 3:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
SordidWarrior
Smarty Rookie


Joined: 03 Sep 2004
Posts: 6
Location: Paris, France

PostPosted: Fri Sep 10, 2004 1:07 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
kilimangaro
Smarty n00b


Joined: 10 May 2004
Posts: 2
Location: Québec, Canada

PostPosted: Wed Apr 06, 2005 3:44 pm    Post subject: Reg Exp to the rescue ! Reply with quote

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? Wink
Back to top
View user's profile Send private message Visit poster's website
Rupom
Smarty Rookie


Joined: 20 Jul 2005
Posts: 6
Location: Dhaka, Bangladesh

PostPosted: Sun Jul 31, 2005 1:37 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
whizz_kid
Smarty Rookie


Joined: 01 Aug 2007
Posts: 5

PostPosted: Wed Aug 01, 2007 11:05 am    Post subject: Reply with quote

does anyone have information on how to insert .php files into .tpl files?
Back to top
View user's profile Send private message
DaKaLKa
Smarty Regular


Joined: 23 Apr 2006
Posts: 99
Location: Ketsch, Germany

PostPosted: Wed Aug 01, 2007 11:24 am    Post subject: Reply with quote

see : http://www.phpinsider.com/smarty-forum/viewtopic.php?p=43834#43834
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
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