I have a beginner question. I have some small js scripts that need to
be altered a bit before being served.
For now I use a clumsy inline in-code multiline string and string
replacement in the python code itself to return a string, which I then
use the webhelpers tag function to enclose in a script tag.
And I was wondering if I could separate these small snippets into
separate js files, but these files would include placeholders for the
strings ( ${string_replace} ) to be replaced from within my code with
custom strings.
I wonder what is the best practice for my case? And if using a
template renderer is the best solution, how is that best made? For
example is the Chameleon text template suitable for this job?
Chris
I already have the static files jquery etc. as separate static
resources.
However, I have these functions that however need to be altered a bit
in the js code itself.
Also, another disadvantage is that inline code in script tags does not
get run when it is loaded via a jquery AJAX. The opposite (as in a
separate js file) however is true. And I wonder if it is better to be
able to serve these small snippets as separate js files rather than
inline, so that they get loaded even if downloaded via AJAX. I guess
then I will have to assign a special view/route to these dynamic
snippets, and render via a template.
Ahmed
On Dec 19, 9:32 am, Wyatt Baldwin <wyatt.lee.bald...@gmail.com> wrote:
> On Sunday, December 18, 2011 9:39:28 AM UTC-8, Chris Rossi wrote:
>
- In your templates, you can just set a JS variable/object in JSON ,
and then have your external JS files reference it on load.
- Between using that, and a lot of callbacks in your JS code, you can
keep most of your client side scripting as a "library" that can be
aggressively cached.
Interesting Wyatt. What do you mean by a JSON/JS approach? So you
factor it in a way that you won't have to use templates for doing
string replacements? Can you give a small example?I already have the static files jquery etc. as separate static
resources.
However, I have these functions that however need to be altered a bit
in the js code itself.Also, another disadvantage is that inline code in script tags does not
get run when it is loaded via a jquery AJAX. The opposite (as in a
separate js file) however is true. And I wonder if it is better to be
able to serve these small snippets as separate js files rather than
inline, so that they get loaded even if downloaded via AJAX. I guess
then I will have to assign a special view/route to these dynamic
snippets, and render via a template.Ahmed
On Dec 19, 9:32 am, Wyatt Baldwin <wyatt.le...@gmail.com> wrote:
> On Sunday, December 18, 2011 9:39:28 AM UTC-8, Chris Rossi wrote:
>
json.dumps() is insufficient quoting, if you're letting user-provided
content into your config dict.
Consider what happens when it tries to quote a string containing
"</script><script>alert('0wn y00');</script>"
> I don't know if that is be applicable to your situation, but maybe you
> could save the config object as a global that you can reference in
> subsequent AJAX calls.
>
> If I were going to go with the template approach, I'd set one config var
> and reference that in the rest of the code rather than doing a bunch of
> interpolation:
>
> # snippet.js
> (function () {
> var config = ${js_config}; // js_config is a JSON string
> // ...
> if (config.xyz) {
> // do stuff
> }
> }());
>
Marius Gedminas
--
Emacs can be like an autistic child, refusing to accept change but showing
more than a touch of genius in how it handles the familiar routine.
-- Stephen J. Turnbull