Template tags

class djangocodemirror.templatetags.djangocodemirror_tags.CodemirrorAssetTagRender[source]

A manifest extend to render Codemirror assets tags HTML.

resolve_widget(field)[source]

Given a Field or BoundField, return widget instance.

Parameters:

field (Field or BoundField) – A field instance.

Returns:

Retrieved widget from given field.

Return type:

django.forms.widgets.Widget

register_from_fields(*args)[source]

Register config name from field widgets

Parameters:

*args – Fields that contains widget djangocodemirror.widget.CodeMirrorWidget.

Returns:

List of registered config names from fields.

Return type:

list

render_asset_html(path, tag_template)[source]

Render HTML tag for a given path.

Parameters:
  • path (string) – Relative path from static directory.

  • tag_template (string) – Template string for HTML tag.

Returns:

HTML tag with url from given path.

Return type:

string

css_html()[source]

Render HTML tags for CSS assets.

Returns:

HTML for CSS assets from every registered config.

Return type:

string

js_html()[source]

Render HTML tags for Javascript assets.

Returns:

HTML for Javascript assets from every registered config.

Return type:

string

codemirror_html(config_name, varname, element_id)[source]

Render HTML for a CodeMirror instance.

Since a CodeMirror instance have to be attached to a HTML element, this method requires a HTML element identifier with or without the # prefix, it depends from template in settings.CODEMIRROR_FIELD_INIT_JS (default one require to not prefix with #).

Parameters:
  • config_name (string) – A registred config name.

  • varname (string) – A Javascript variable name.

  • element_id (string) – An HTML element identifier (without leading #) to attach to a CodeMirror instance.

Returns:

HTML to instanciate CodeMirror for a field input.

Return type:

string

djangocodemirror.templatetags.djangocodemirror_tags.codemirror_field_js_assets(*args)[source]

Tag to render CodeMirror Javascript assets needed for all given fields.

Example

{% load djangocodemirror_tags %}
{% codemirror_field_js_assets form.myfield1 form.myfield2 %}
djangocodemirror.templatetags.djangocodemirror_tags.codemirror_field_css_assets(*args)[source]

Tag to render CodeMirror CSS assets needed for all given fields.

Example

{% load djangocodemirror_tags %}
{% codemirror_field_css_assets form.myfield1 form.myfield2 %}
djangocodemirror.templatetags.djangocodemirror_tags.codemirror_field_js_bundle(field)[source]

Filter to get CodeMirror Javascript bundle name needed for a single field.

Example

{% load djangocodemirror_tags %}
{{ form.myfield|codemirror_field_js_bundle }}
Parameters:

field (django.forms.fields.Field) – A form field that contains a widget djangocodemirror.widget.CodeMirrorWidget.

Raises:
  • CodeMirrorFieldBundleError – If Codemirror configuration form field

  • does not have a bundle name.

Returns:

Bundle name to load with webassets.

Return type:

string

djangocodemirror.templatetags.djangocodemirror_tags.codemirror_field_css_bundle(field)[source]

Filter to get CodeMirror CSS bundle name needed for a single field.

Example

{% load djangocodemirror_tags %}
{{ form.myfield|codemirror_field_css_bundle }}
Parameters:

field (djangocodemirror.fields.CodeMirrorField) – A form field.

Raises:
  • CodeMirrorFieldBundleError – Raised if Codemirror configuration from

  • field does not have a bundle name.

Returns:

Bundle name to load with webassets.

Return type:

string

djangocodemirror.templatetags.djangocodemirror_tags.codemirror_parameters(field)[source]

Filter to include CodeMirror parameters as a JSON string for a single field.

This must be called only on an allready rendered field, meaning you must not use this filter on a field before a form. Else, the field widget won’t be correctly initialized.

Example

{% load djangocodemirror_tags %}
{{ form.myfield|codemirror_parameters }}
Parameters:

field (djangocodemirror.fields.CodeMirrorField) – A form field.

Returns:

JSON object for parameters, marked safe for Django templates.

Return type:

string

djangocodemirror.templatetags.djangocodemirror_tags.codemirror_instance(config_name, varname, element_id, assets=True)[source]

Return HTML to init a CodeMirror instance for an element.

This will output the whole HTML needed to initialize a CodeMirror instance with needed assets loading. Assets can be omitted with the assets option.

Example

{% load djangocodemirror_tags %}
{% codemirror_instance "a-config-name" "foo_codemirror" "foo" %}
Parameters:
  • config_name (string) – A registred config name.

  • varname (string) – A Javascript variable name.

  • element_id (string) – An HTML element identifier (without leading #) to attach to a CodeMirror instance.

Keyword Arguments:

assets (Bool) – Adds needed assets before the HTML if True, else only CodeMirror instance will be outputed. Default value is True.

Returns:

HTML.

Return type:

string