CoffeeScript middleware for TurboGears2
tgext.coffeescript is based on tgext.scss by Alessandro Molina and is under the same license (MIT).
tgext.coffeescript can be installed both from pypi or from bitbucket:
You will also need to install the CoffeeScript compiler, for instructions on this check their website.
Using tgext.coffeescript is really simple, you edit your config/middeware.py and just after the #Wrap your base TurboGears 2 application with custom middleware here comment wrap app with CoffeeScriptMiddleware:
from tgext.coffeescript import CoffeeScriptMiddleware make_base_app = base_config.setup_tg_wsgi_app(load_environment) def make_app(global_conf, full_stack=True, **app_conf): app = make_base_app(global_conf, full_stack=True, **app_conf) # Wrap your base TurboGears 2 application with custom middleware here app = CoffeeScriptMiddleware(app) return app
app = CoffeeScriptMiddleware(app, minify=True)
You can change the cache backend storage into any dict like object that can serialize a dict object, for example, you can use beaker cache by passing the cache object to the middleware constructor:
from tgext.coffeescript import CoffeeScriptMiddleware from tg import cache make_base_app = base_config.setup_tg_wsgi_app(load_environment) def make_app(global_conf, full_stack=True, **app_conf): app = make_base_app(global_conf, full_stack=True, **app_conf) # CoffeeScript with beaker cache backend app = CoffeeScriptMiddleware(app, cache=cache) return app
app = CoffeeScriptMiddleware(app, bare=False)
This will disable the bare options for all files served with tgext.coffeescript, more granular control of this option may be available later depending on the need for it.
tgext.coffeescript provides an extension for jinja2 templates to compile CoffeeScript embedded directly on your templates. To activate it just add to your config/app_config.py file the following:
from tgext.coffeescript.jinja import CoffeeExtension base_config.jinja_extensions = [CoffeeExtension]
Now you can use the coffee tag in your templates:
Where “main” is a unique identifier for that CoffeeScript section, this should output nicely as: