defines_to_js

Built-in transpilers for python classes. Only one is provided that transpiles plain old data found on classes and their ancestors.

class render_static.transpilers.defines_to_js.DefaultDefineTranspiler[source]

Bases: Transpiler

A Transpiler that transpiles plain old data in python classes into simple JavaScript structures. For example if you have a model with choices:

class MyModel(models.Model):

    FIELD_CHOICES = (
        ('A', 'Choice A'),
        ('B', 'Choice B'),
        ('C', 'Choice C')
    )

    field = models.CharField(max_length=1, choices=FIELD_CHOICES)

Your template might look like:

{% defines_to_js defines='package.MyModel' %}

This will produce JavaScript you may invoke like so:

const defines = {
    FIELD_CHOICES: [
        ["A", "Choice A"],
        ["B", "Choice B"],
        ["C", "Choice C"]
    ]
};

The code produced will nest depending on the level at which it was targeted. For instance if instead of the above we had done:

{% defines_to_js defines='package' %}

The transpilation would be:

const defines = {
    MyModel: {
        FIELD_CHOICES: [
            ["A", "Choice A"],
            ["B", "Choice B"],
            ["C", "Choice C"]
        ]
    }
};
__init__(include_member=<function DefaultDefineTranspiler.<lambda>>, const_name='defines', **kwargs)[source]
Parameters:
  • include_member (Callable[[Any], bool]) – A function that accepts a member name and member instance of a class and returns if the member should be written. By default this will include any member that is all upper case.

  • const_name (str) – The name to use for the const variable containing the transpiled defines.

  • kwargs – Set of configuration parameters, see also Transpiler params

Return type:

None

classmethod __new__(*args, **kwargs)
property DefaultDefineTranspiler.context: Dict[str, Any]

The template render context passed to overrides. In addition to render_static.transpilers.Transpiler.context. This includes:

  • const_name: The name of the const variable