templatetags.render_static

Template tags and filters available when the render_static app is installed.

render_static.templatetags.render_static.split(to_split, sep=None)[source]

Django template for python’s standard split function. Splits a string into a list of strings around a separator.

Parameters:
  • to_split (str) – The string to split

  • sep (str | None) – The separator characters to use as split markers.

Returns:

A list of strings

Return type:

List[str]

render_static.templatetags.render_static.defines_to_js(defines, transpiler=<class 'render_static.transpilers.defines_to_js.DefaultDefineTranspiler'>, indent='\t', depth=0, **kwargs)[source]

Transpile defines from the given modules or classes into javascript.

Parameters:
  • defines (ModuleType | Type[Any] | str | Collection[ModuleType | Type[Any] | str]) – A module, class or import string to either.

  • transpiler (Type[Transpiler] | str) – The transpiler class or import string for the transpiler class that will perform the conversion, default: DefaultDefineTranspiler

  • indent (str) – The indent string to use

  • depth (int) – The depth of the initial indent

  • kwargs – Any other kwargs to pass to the transpiler.

Returns:

SafeString of rendered transpiled code.

Return type:

str

render_static.templatetags.render_static.urls_to_js(transpiler=<class 'render_static.transpilers.urls_to_js.ClassURLWriter'>, url_conf=None, indent='\t', depth=0, include=None, exclude=('admin', ), **kwargs)[source]

Dump reversible URLs to javascript. The javascript generated provides functions for each fully qualified URL name that perform the same service as Django’s URL reverse() function. The javascript output by this tag isn’t standalone. It is up to the caller to embed it in another object. For instance, given the following urls.py:

from django.urls import include, path
from views import MyView

urlpatterns = [
    path('my/url/', MyView.as_view(), name='my_url'),
    path('url/with/arg/<int:arg1>', MyView.as_view(), name='my_url'),
    path('sub/', include('other_app.urls', namespace='sub')),
]

And the other app’s urls.py:

from django.urls import path
from views import MyView

urlpatterns = [
    path('detail/<uuid:id>', MyView.as_view(), name='detail'),
]

And the following template:

{% urls_to_js %}

You can now reverse code in the javascript like this:

const urls = URLResolver():

# /my/url/
console.log(urls.reverse('my_url));

# /url/with/arg/143
console.log(urls.reverse('my_url', {kwargs: {'arg1': 143}}));

# /sub/detail/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
console.log(
    urls.reverse(
        'other:detail',
        {kwargs: {'id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'}}
    )
);

Note

Care has been taken to harden this process to changes to the Django url resolution source and to ensure that it just works with minimal intervention.

The general strategy of this process is two staged. First a tree structure is generated by walking the urlpatterns structure that contains the url patterns and resolves all their fully qualified names including all parent namespaces. URLs and namespaces are included and excluded at this stage based on the include/exclude parameters. The branches of the tree are namespaces and the leaves are fully qualified URL names containing lists of corresponding URLPatterns.

The second stage recursively walks the tree, writing javascript as it enters namespaces and encounters URLPatterns. Multiple URLs may be registered against the same fully qualified name, but may be distinguished by the named parameters they accept. One javascript function is generated for each fully qualified URL name, that will select the correct URL reversal based on the names of the parameters passed in and map those parameter values to the correct placeholders in the URL. To ensure the outputs of the javascript match Django’s reverse() the strategy is to use the results of the reverse() call for the fully qualified name. Placeholder values are passed into reverse() and then overwritten with javascript substitution code based on the regex grouping information. This strategy avoids as much error prone regex/string processing as possible. The concession here is that placeholder values must be supplied by the user wherever we cant infer them. When using path instead of re_path we can use default placeholders for all the known converters. When using re_path or custom path converters users must register placeholders by parameter name, converter type, or app_name. Libraries exist for generating string patterns that match regex’s but none seem reliable or stable enough to include as a dependency.

Parameters:
  • transpiler (Type[Transpiler] | str) – The transpiler class that will generate the JavaScript, as either a class or an import string. May be one of the built-ins or a user defined transpiler.

  • url_conf (ModuleType | str | None) – The root url module to dump urls from, default: ROOT_URLCONF

  • indent (str) – string to use for indentation in javascript, default: ‘ ‘

  • depth (int) – the starting indentation depth, default: 0

  • include (Iterable[str] | None) – A list of path names to include, namespaces without path names will be treated as every path under the namespace. Default: include everything

  • exclude (Iterable[str] | None) – A list of path names to exclude, namespaces without path names will be treated as every path under the namespace. Default: exclude nothing

  • kwargs – Extra kwargs that will be passed to the visitor class on construction. All visitors are passed indent, and depth.

Returns:

A javascript object containing functions that generate urls with and without parameters

Return type:

str

render_static.templatetags.render_static.enums_to_js(enums, transpiler=<class 'render_static.transpilers.enums_to_js.EnumClassWriter'>, indent='\t', depth=0, **kwargs)[source]

Transpile the given enumeration(s).

Parameters:
  • enums (ModuleType | Type[Enum] | str | Collection[ModuleType | Type[Enum] | str]) – An enum class or import string or a collection of either to transpile.

  • transpiler (Type[Transpiler] | str) – A transpiler class or import string of the transpiler class to use for the transpilation.

  • indent (str) – The indent string to use

  • depth (int) – The depth of the initial indent

  • kwargs – Any other parameters to pass to the configured transpiler. See transpiler docs for details.

Returns:

SafeString of rendered transpiled code.

Return type:

str