Re: cheetah and jquery

Michael Higgins <[email protected]>
Newsgroups gmane.comp.python.cheetah
Message-ID <[email protected]>
Personally, I'd recommend against doing a lot of Javascript code  
generation inside a Cheetah template.  Instead, keep your Javascript  
in separate files.  There are a couple of reasons for this:

* Editing convenience.  You can use an editor mode optimized for  
Javascript to edit your Javascript, and optimized for Cheetah or HTML  
or Python to edit your other code.  Also, it's easier to write unit  
tests and to use tools like JSLint if things are separated out.

* Avoids code generation bugs.  When you use a templating system to  
generate source code, debugging the generated source code can be  
difficult because bugs can live both in the template that generated  
the source code, and the source code itself.  The simplest example of  
such bugs is quoting problems: you try to say something like:

var foo = "$myVar";

and things seem to work fine, until $myVar evaluates to something like:

He said "hello, world!"

and then the generated code reads

var foo = "He said "hello world!"";

which is not valid Javascript!

* Encourages modularity.  It's a lot easier to think about your  
Javascript objects if you keep them structurally split out from your  
Cheetah and Python objects.

* Encourages secure code practices.  If you're generating Javascript  
in a template, you're going to be tempted to just shove values from a  
database or from user input right into the code.  This can be unsafe  
if you're not careful --- a clever user could inject Javascript code  
into your system if you don't do the right clean-up.  Separating  
things out a bit more makes it clearer where and when that kind of  
clean-up needs to happen.

* And, yeah, it avoids the $ collision problem.  (Which happens with a  
ton of Javascript toolkits, not just JQuery.)

Obviously, you will need to do a little Javascript in the template,  
but this can usually be minimized to a couple of functions or  
initialization routines that simply call out to the real code.

So, to actually answer your question, there are two ways I typically  
pass values for variables.  One is to use AJAX, so the value is sent  
as XML or JSON after the page is loaded.  The other is to create a  
hidden form element and store the value as URL-encoded JSON in the  
value of the form, which the Javascript can then fetch and decode when  
the page is loaded.

I find that doing it that way keeps my interfaces clearer, makes it  
more obvious where values need to be cleaned up, and makes it easier  
to edit the code.

I'm sure other people have other "best practices" as well.

Mike Higgins
CTO, Rhiza Labs

P.S.  This is not to say that template-driven program code generation  
is a bad thing: used well, it's extremely powerful.  But I don't think  
it's the right approach for everyday chores.

On Apr 25, 2009, at 6:36 AM, [email protected] wrote:

> Cheetah doesnt play well along with jquery
> as Jquery uses $ to find variables or ids
> But since cheetah doesnt find the variables in the search list it  
> throws an error
>
> But since we need to fill some javascript with variables from  
> cheetah we cannot do
>
> #raw
> Jquery code
> #end raw
>
> How do you guys deal with this situation
> thanks
>
> -- 
> Bidegg worlds best auction site
> http://bidegg.com
> ------------------------------------------------------------------------------
> Crystal Reports &#45; New Free Runtime and 30 Day Trial
> Check out the new simplified licensign option that enables unlimited
> royalty&#45;free distribution of the report engine for externally  
> facing
> server and web deployment.
> http://p.sf.net/sfu/businessobjects_______________________________________________
> Cheetahtemplate-discuss mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss


------------------------------------------------------------------------------
Crystal Reports &#45; New Free Runtime and 30 Day Trial
Check out the new simplified licensign option that enables unlimited
royalty&#45;free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.