Skip to main content

Easy react components within your django templates

Project description

Build Status PyPI version

Django-React-Templatetags

This extension allows you to add React components into your django templates.

Requirements

  • Python 2.7 / Python 3.4 / PyPy

  • Django 1.8+

Installation

Install the library with pip:

$ pip install django_react_templatetags

Quick Setup

Make sure django_react_templatetags is added to your INSTALLED_APPS.

INSTALLED_APPS = (
    # ...
    'django_react_templatetags',
)

You also need to add the react_context_processor into the context_middleware:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            'templates...',
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': True,
            'context_processors': [
                ...
                'django_react_templatetags.context_processors.react_context_processor',
            ],
        },
    },
]

This should be enough to get started.

Usage

  1. Load the {% load react %}

  2. Insert component anywhere in your template: {% react_render component="Component" data=my_data %}. This will create a placeholder.

  3. Put {% react_print %} in the end of your template. (This will output the ReactDom.render javascript).

Full example

This template:

{% load react %}
<html>
    <head>...</head>

    <body>
        <nav>
            {% react_render component="Menu" data=menu_data %}
        </nav>
    </body>

    {% react_print %}
</html>

Will transform into this:

{% load react %}
<html>
    <head>...</head>

    <body>
        <nav>
            <div id="Menu_1"></div>
        </nav>
    </body>

    <script>
        ReactDOM.render(
            React.createElement(Menu, {'example', 1}),
            document.getElementById('Menu_1')
        );
    </script>
</html>

Settings

  • REACT_COMPONENT_PREFIX: Adds a prefix to your React.createElement include.

    • Example using (REACT_COMPONENT_PREFIX="Cookie.")

    • …Becomes: React.createElement(Cookie.MenuComponent, {})

Q&A

  • How do I override the markup generated by react_print?

    • Simple! Just override the template react_print.html

  • This library only contains templatetags, where is the react js library?

    • This library only covers the template parts (that is: placeholder and js render).

  • I dont like the autogenerated element id, can I supply my own element?

    • Sure! Just add the param identifier="yourid" in react_render

  • How do I apply my own css class to the autogenerated element?

    • Add class="yourclassname" to your react_render

Tests

This library include tests, just run python runtests.py.

You can also run separate test cases: runtests.py tests.ReactIncludeComponentTest

Contributing

Want to contribute? Awesome. Just send a pull request.

License

Django-React-Templatetags is released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_react_templatetags-2.4.0.tar.gz (7.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page