First be sure you have added micawber.contrib.mcdjango to INSTALLED_APPS so that we can use the template filters it defines.
# settings.py
INSTALLED_APPS = [
# ...
'micawber.contrib.mcdjango',
]
micawber provides 4 template filters for converting URLs contained within text or HTML to rich content:
These filters are registered in the micawber_tags library, which can be invoked in your templates:
{% load micawber_tags %}
<p>{{ object.body|oembed:"600x600" }}</p>
Each filter accepts one argument and one optional argument, due to django’s template filters being wack.
Piping a string through the oembed filter (or oembed_html) will convert URLs to things like youtube videos into flash players. A couple things to understand about the parsers:
Note
You can control how things are rendered – check out the default templates for reference implementations.
The following filters are exposed via the micawber.contrib.mcdjango module:
Parse the given text, rendering URLs as rich media
Usage within a django template:
{{ blog_entry.body|oembed:"600x600" }}
| Parameters: |
|
|---|---|
| Return type: | parsed text with rich content embedded |
Exactly the same as above except for usage with html
Usage within a django template:
{{ blog_entry.body|markdown|oembed_html:"600x600" }}
Parse the given text, returning a list of 2-tuples containing url and metadata about the url.
Usage within a django template:
{% for url, metadata in blog_entry.body|extract_oembed:"600x600" %}
<img src="{{ metadata.thumbnail_url }}" />
{% endfor %}
| Parameters: |
|
|---|---|
| Return type: | 2-tuples containing the URL and a dictionary of metadata |
Exactly the same as above except for usage with html
For simplicity, micawber provides a setting allowing you to create custom template filters. An example use case would be to add a template filter that could embed rich content, but did not automatically “urlize” all links.
Extensions are configured in the settings module and take the form of a list of 2-tuples containing:
MICAWBER_TEMPLATE_EXTENSIONS = [
('oembed_no_urlize', {'urlize_all': False}),
]
Assume this is our template:
{% load micawber_tags %}
DEFAULT:
{{ "http://foo.com/ and http://bar.com/"|oembed }}
CUSTOM:
{{ "http://foo.com/ and http://bar.com/"|oembed_no_urlize }}
Rendering the above template will produce the following output:
DEFAULT:
<a href="http://foo.com/">http://foo.com/</a> and <a href="http://bar.com/">http://bar.com/</a>
CUSTOM:
http://foo.com/ and http://bar.com/
Some examples of keyword arguments to override are:
The magic happens in micawber.contrib.mcdjango.extension() – check out the source code for more details.
Note
The MICAWBER_EXTENSIONS setting can also be a string path to a module and an attribute containing a similar data structure.
The most important setting to configure is the module / attribute path to the providers you wish to use. The attribute can either be a ProviderRegistry instance or a callable. The default is:
MICAWBER_PROVIDERS = 'micawber.contrib.mcdjango.providers.bootstrap_basic'
You can use the bootstrap embedly function, but beware this may take a few seconds to load up:
MICAWBER_PROVIDERS = 'micawber.contrib.mcdjango.providers.bootstrap_embedly'
If you want to use the embedly endpoints and have an API key, you can specify that in the settings:
MICAWBER_EMBEDLY_KEY = 'foo'
You can also customize this with your own set of providers. This must be either
Here is a quick example showing a custom ProviderRegistry:
# settings.py
MICAWBER_PROVIDERS = 'my_app.micawber_providers.oembed_providers'
# my_app/micawber_providers.py
from django.core.cache import cache
from micawber.providers import Provider, bootstrap_basic
oembed_providers = boostrap_basic(cache)
# add a custom provider
oembed_providers.register('http://example.com/\S*', Provider('http://example.com/oembed/'))
Because of the limitations of django’s template filters, we do not have the flexibility to pass in multiple arguments to the filters. Default arguments need to be specified in the settings:
MICAWBER_DEFAULT_SETTINGS = {
'key': 'your-embedly-api-key',
'maxwidth': 600,
'maxheight': 600,
}
>>> from django.template import Template, Context
>>> t = Template('{% load micawber_tags %}{{ "http://www.youtube.com/watch?v=mQEWI1cn7HY"|oembed }}')
>>> t.render(Context())
u'<iframe width="480" height="270" src="http://www.youtube.com/embed/mQEWI1cn7HY?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe>'