Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Event Handler

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
enginemike
Smarty Regular


Joined: 28 Feb 2006
Posts: 57

PostPosted: Mon Nov 05, 2007 10:26 pm    Post subject: Event Handler Reply with quote

How does one get an event handler inserted into code generated by smarty.

As an example I would like to end up with a piece of code that looks like

<input type="checkbox" name="box" onevent="doSomething" />

I can get the javascript "doSomething" to render but haven't figured out how to get the event handler into the input tag.

Thanks
Back to top
View user's profile Send private message
enginemike
Smarty Regular


Joined: 28 Feb 2006
Posts: 57

PostPosted: Tue Nov 06, 2007 11:58 pm    Post subject: [Solved] handling Events Reply with quote

Don't know if it's the right way but I solved the problem by using a config file.

I put the event handler as a global variable in the event file such as

event_handler_1 = 'onclick="clickThis()"'

and then loaded it as a variable in the applicable tag such as

<input type="button" value="Click Me" {#event_handler_1#} />

Seems to work fine
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Nov 07, 2007 1:51 am    Post subject: Reply with quote

You can put javascript right in the template if you want, you just have to escape curly braces.
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Mon Sep 08, 2008 6:28 am    Post subject: Just unoobstrusive javascripts only Reply with quote

Once upon a time, I used to write the inline javascripts with hte reusable the smarty template variables. Later I found it to be a bad and poorest idea: It is not managale all the times. Instead, you MUST write unobstrusive javascripts only.

This gives you a complete freedom to do whatever a javascript can do, withing the template. For example, make a list of the elements you would like to install the events on. After the page display completes, just before the </body>, instert a javascript in similar way as below: (Be sure to use your smarty loops, if you like)

<script>
function click_this()
{
// do something here
alert('You clicked me: {$click_message}!'+this.id);
return(true);
}

// You can make a smarty loop, if needed.
document.getElementById('click_element').onclick = click_this;
</script>

My point here is now:
You must loop over how you define the click_this() function and document.getElementById. Just note that, since, you are within the smarty template, it supports writing the javascripts using smarty loops for multiple elements.
Back to top
View user's profile Send private message Visit poster's website
slyydrr
Smarty n00b


Joined: 10 Nov 2008
Posts: 4

PostPosted: Mon Nov 10, 2008 4:57 pm    Post subject: Reply with quote

And just a friendly reminder too, don't forget your {literal}{/literal} tags Smile
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Sat Jan 17, 2009 7:37 am    Post subject: Use external java scripts, or, write literal tags in comment Reply with quote

Whenever you write all your javascripts in an external file, you never have to face problems with { and } delimiters. It is good, if you remember to use {literal} and {/literal} tags.

Here is my trick, although not necessary all the times:

// {literal}
..
// {/literal}

or,

/* {literal} */
/...
/* {/literal} */

This does not seem a big problem. But the trick is to fake the editors that they assume our {literal} tags within comments, and disregard coloring. For example, it works well while you are editing CSS and Javasciprts, using Dreamweaver.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks 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