miracle2k/webassets — pull request #283 by metagriffin (2013-12-22 22:30:26+00:00)
Title: adding a "renderer" framework so assets can render themselves

first, thanks for creating a great tool for managing assets!

one of the things that i found odd about webassets was the fact that when referencing an asset in a template, the template needs to be sensitive to the internals of the assets to be able to reference it correctly. for example, it had to know if it was css or javascript, if the less output was being compiled or not, etc.

this merge request is the beginnings of a solution that allows assets to render themselves using a renderer registry. for example, given the following bundle setup (notice that the bundle contains both css and javascript):

``` python
all_assets = Bundle(
Bundle('style.css', output='app.css', renderer='css'),
Bundle('script.js', output='app.js', renderer='js'))
env.register('app', all_assets)
```

knowing that the 'css' and 'js' renderers come baked-in, you can have the following mako template:

``` mako

% for asset in env['app'].renderers():
${asset.render()|n}
% endfor

...

```

which will generate the following output:

``` html

...

```

asset contents can also be rendered inline via ``asset.render(inline=True)``.

existing renderers can be overridden and custom renderers added, eg:

``` python
env.register_renderer('meta-url', '')
```

the merge request has:

* the full implementation for HTML referencing and inlining
* unit tests for css and js renderers
* documentation for how to use the renderer framework

please let me know if you agree that this would be a nice addition and/or what needs improvement.

thanks!