Serving Dynamic/Template js files

63 views
Skip to first unread message

Ahmed

unread,
Dec 18, 2011, 7:55:13 AM12/18/11
to pylons-discuss
Hello,

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 Rossi

unread,
Dec 18, 2011, 12:39:28 PM12/18/11
to pylons-...@googlegroups.com
On Sun, Dec 18, 2011 at 7:55 AM, Ahmed <ahmed...@gmail.com> wrote:
> 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?
>
That's what I would use. I'm usually able to factor my js in such a
way, though, that my js files can be served statically and only depend
a few lines of dynamic data injected into the html page in a <script>
tag.

Chris

Wyatt Baldwin

unread,
Dec 18, 2011, 6:32:26 PM12/18/11
to pylons-...@googlegroups.com

Similarly, I create a single JSON config object in a helper function and inject it into the template; it gets passed to a main JavaScript function (defined in a separate script file), which sort-of reflects how a Pyramid app is initialized.

For a while, I did some dynamic JS setup using Mako, but it was a bit messy, and I prefer the straight JSON/JS approach.


Ahmed

unread,
Dec 19, 2011, 12:32:38 AM12/19/11
to pylons-discuss
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.lee.bald...@gmail.com> wrote:
> On Sunday, December 18, 2011 9:39:28 AM UTC-8, Chris Rossi wrote:
>

hanhan

unread,
Dec 19, 2011, 3:20:15 AM12/19/11
to pylons-discuss
Hi, Neat post. There’s an issue together with your web site in web
explorer, would check this? IE nonetheless is the marketplace chief
and a large section of folks will pass over your magnificent writing
because of this problem.<a href="http://www.bagsshops.net/">Cheap
chanel 2.55 handbags</a>

hanhan

unread,
Dec 19, 2011, 3:19:16 AM12/19/11
to pylons-discuss

Chris McDonough

unread,
Dec 19, 2011, 5:55:19 AM12/19/11
to pylons-...@googlegroups.com
Ugh my fault.. I let this message through without reviewing it closely.
User has been banned.

Jonathan Vanasco

unread,
Dec 19, 2011, 1:10:42 PM12/19/11
to pylons-discuss

I *think* wyatt might be referencing a technique similar to how
Facebook Connect works.

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

Wyatt Baldwin

unread,
Dec 20, 2011, 12:07:58 PM12/20/11
to pylons-...@googlegroups.com
In an earlier version of my code, I did some dynamic JavaScript configuration using Mako constructs:

    # app.mako
    <script>
        % if something:
            // set some JS var
        % endif
        var x = '${some_var_from_view}';
        var y = ${something_that_is_specific_to_this_request(request)};
    </script>

Now I do something like this:

    # helpers.py
    def get_js_config(request):
        config = {
            # whatever request-specific config you need
        }
        return literal(json.dumps(config))

    # app.mako
    <script src="main.js"></src>  ## Load main JS function
    <script>
        main(${h.get_js_config()});
    </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
        }
    }());



On Sunday, December 18, 2011 9:32:38 PM UTC-8, Ahmed wrote:
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:
>

Marius Gedminas

unread,
Dec 21, 2011, 6:28:25 AM12/21/11
to pylons-...@googlegroups.com
On Tue, Dec 20, 2011 at 09:07:58AM -0800, Wyatt Baldwin wrote:
> In an earlier version of my code, I did some dynamic JavaScript
> configuration using Mako constructs:
>
> # app.mako
> <script>
> % if something:
> // set some JS var
> % endif
> var x = '${some_var_from_view}';
> var y = ${something_that_is_specific_to_this_request(request)};
> </script>
>
> Now I do something like this:
>
> # helpers.py
> def get_js_config(request):
> config = {
> # whatever request-specific config you need
> }
> return literal(json.dumps(config))
>
> # app.mako
> <script src="main.js"></src> ## Load main JS function
> <script>
> main(${h.get_js_config()});
> </script>

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

signature.asc

Wyatt Baldwin

unread,
Dec 21, 2011, 11:50:07 AM12/21/11
to pylons-...@googlegroups.com
In my case, there's no user-provided data, but that is a good point to bring up.
Reply all
Reply to author
Forward
0 new messages