I've seen this question come up a lot and I'm not sure there's a standard
Pylons approach. It really depends on what you're trying to do.
One method is to transfer the data to Javascript separately. Once the page
loads (or some other event fires), send a request to the server and have it
return the data (via a separate controller action) in XML or JSON or some
other exchange format. This lets you keep a clean separation between the
HTML templates, the static Javascript files, and the dynamic data that the
Javascript acts on. However, there are situations in which it makes no sense
to use that approach.
Another option is to load part of it in a <script> tag in the HTML template.
For instance (using Mako):
<script>
var titles = [];
% for book in c.books:
titles[${book.index}] = '${book.title}';
% endfor
</script>
You can still load the Javascript code that uses that array in a separate
file, but the array itself will be loaded into the HTML document you send to
the user (keeping in mind that you can still find clever ways to use
namespaces to keep the Javascript relatively clean).
How does everyone else handle this? Maybe we can get something up on the
wiki about the best way to handle it in Pylons.
On Sun, Oct 31, 2010 at 10:56 AM, Alexandre Conrad <
[email protected]> wrote:
> It's not possible. tmpl_context lives on the server side whereas
> Javascript runs on the client site. Unless you serialize your object
> and send it over the wire, which is probably very insecure, there is
> not way to do that.
>
> What do you mean by "I found the method to use in html" ?
>
> 2010/10/30 anusha <[email protected]>:
> > Hello all,
> >
> > Is there any method to access c or tmpl_context in javascript. I found
> > the method to use in html but i cant find whether it is possible or
> > not in js and if possible how it is.
> >
> > Thanks in advance
> > Anusha
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> [email protected]<pylons-discuss%[email protected]>
> .
> > For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
> >
> >
>
>
>
> --
> Alex | twitter.com/alexconrad
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<pylons-discuss%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.