2

how can I return some javascript code or a javascript file with view callables in Pyramid/Pylon framework?

Jensen
  • 1,653
  • 4
  • 26
  • 42

2 Answers2

7

You can do that as you'd do with any template/view callable. It's not that different. I'd still recommend setting the content type as such.

from pyramid.view import view_config

@view_config(name='javascript', renderer='templates/javascript.mako')
def my_js_view(request):
    request.response.content_type = 'application/javascript'
    return {... params ...}

It doesn't really differ from other views.

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
  • request.response_content_type was an attribute for pyramid 1.0 but its deprecated – Loïc Faure-Lacroix Jun 29 '12 at 10:08
  • Right, didn'n know that! My apologies for editing that then; but since the OP didn't specify a version we'd best stick to current methods. :-) – Martijn Pieters Jun 29 '12 at 13:11
  • @MartijnPieters Sorry, I didn't make it clear enough. I am actually writing a bookmarklet, when you click the bookmarklet, it get some javascript code from server, and run it immediately. Should I still use template as well? – Jensen Jun 29 '12 at 17:02
  • @Jensen: Does the javascript returned change based on the request or on what is stored in the database? If so, use a template, otherwise return static JS. – Martijn Pieters Jun 29 '12 at 17:04
  • 1
    actually as I understand, you could actually try looking at the jsonp renderer. Instead of plain javascript. If your calls only request informations, you could return json and pass it to a callback in your application with jsonp or use plain json. http://docs.pylonsproject.org/projects/pyramid/en/latest/api/renderers.html?awesome#pyramid.renderers.JSONP – Loïc Faure-Lacroix Jun 29 '12 at 19:27
  • @MartijnPieters Yeah, it's gonna depend on the request, but I want the browser to stay on that page instead of redirected to another page. – Jensen Jun 29 '12 at 23:11
  • @Jensen: just make your bookmarklet part of the javascript embed a script tag in the current document pointing to the pyramid view url. And also: the proper mime type for javascript is "application/javascript". RFC 4329. – Antti Haapala -- Слава Україні Jul 01 '12 at 11:52
  • @AnttiHaapala Yeah, that's what I did, I've get it working. The template returned by view can be pure javascript code. – Jensen Jul 02 '12 at 14:56
3

What kind of javascript? Dynamically generated, or static?

When static, just serve it like CSS and images, using a static view.

If dynamic, use a template to generate the javascript file and serve with the correct content type; here is an example using the Chameleon text renderer, but you can use your own preferred templating engine of course:

@view_config(name='generated_javascript', renderer='templates/generated_javascript.txt')
def generated_javascript(request):
    request.response.content_type = 'text/javascript'
    # The returned dict holds items your template can access when
    # generating the javascript.
    return dict(foo='bar', spam='eggs')

Note that I set the content_type attribute on the Response object to make sure browsers recognize your generated JavaScript.

Your template then can access the values in the dict you returned plus some additional system values, just like any other template.

Note that best practice is to minimize the use of dynamic JavaScript, and to serve as much of it as possible statically. Any (dynamic) context information should really be included in the HTML you generate which your static JavaScript can then load and use. Your static JavaScript can then be cached by all your visitors, improving performance considerably. See Chameleon templates for javascript files? for some tips on how to achieve that.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343