The compatibility issue is a good point, and it's one reason I primarily use
JSON (YUI's JSON parser client-side paired with the @jsonify controller
server-side is a powerful combo). I think this goes back to the issue of
what you're trying to accomplish. For a minor task in a controlled setting
you can get by with a lot of things, but for anything that needs to scale,
it can start to break down if you don't keep a clean separation between all
the components.

Viewing it just in terms of maintainability, it can very quickly become hard
to manage a controller that renders a template that has embedded Python code
to generate Javascript code that in turn is used to generate the HTML... And
while I doubt any of us are planning to migrate all our Pylons projects
elsewhere, it's still nice to know that a solid client-side interface using
JSON or XML could actually function independent of Pylons, and could be
easily adapted into other frameworks or even other scripting languages.

On Mon, Nov 1, 2010 at 7:42 AM, Marius Gedminas <[email protected]> wrote:

> On Sun, Oct 31, 2010 at 07:53:46PM -0700, Eric Rasmussen wrote:
> > 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}';
>
> This breaks down when book.title contains an apostrophe.
>
> Also, this doesn't suppress HTML quoting, and IIRC browsers will interpret
> things like '&amp;' like a string of 5 characters, not a string of one
> character.
>
> I ended up writing a function to quote javascript string literals:
>
>    def qjs(s):
>        r"""Quote a string for Javascript.
>
>            >>> print qjs('hi')
>            'hi'
>            >>> print qjs("I'm back!")
>            'I\x27m back!'
>
>        """
>        s = (s.replace('\\', '\\\\')
>              .replace('\'', '\\x27')
>              .replace('"', '\\x22')
>              .replace('<', '\\x3c')
>              .replace('>', '\\x3e')
>              .replace('&', '\\x26'))
>        return literal("'%s'") % s
>
> I use it like this:
>
>  <script>
>  titles[${book.index}] = ${book.title|qjs};
>  </script>
>
> or
>
>  <a href="#" onclick="some_function(${param|qjs})">Do something</a>
>
> which explains why I need to be so careful with ampersands and quotes.
>
> I've no idea how portable this way of quoting is across browsers.
>
> > How does everyone else handle this? Maybe we can get something up on the
> > wiki about the best way to handle it in Pylons.
>
> Go for it!
>
> Consider my qjs to be public domain, if that helps.
>
> Marius Gedminas
> --
> "What's the name of the new OO COBOL -- an equivalent of C++?"
> "ADD 1 TO COBOL GIVING COBOL"
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
>
> iEYEARECAAYFAkzO0bsACgkQkVdEXeem14+gNgCgjgKRn4WAbNY7pKZtO+fgPTzx
> ikEAnjGdYGeXf46OBxc/r7ZKwweby9q2
> =qp2a
> -----END PGP SIGNATURE-----
>
>

-- 
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.

Reply via email to