React template inside Django template? #277
-
Hello everyone and Silvio thanks once again for the amazing effort you made creating Reactivated!
I'm asking this because Django template engine allows me to use the template's extension mechanism ( Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Great question. Not in the official package. I actually created the inverse code for a project of mine. You can replace top level templates with React, but the "inner" blocks-only templates continue being rendered by Django. To ease transition. The idea being that rewriting one "base.html" once to React but leaving dozens of "widget_detail.html", "trinket_detail.html" etc made more sense. FYI you could just inject the "static/dist/index.js" file in there and render React into I'll have to explore what common "transition" scenarios are. |
Beta Was this translation helpful? Give feedback.
-
@Adamcina For a non-foss project at @thelabnyc, I actually wrote a Django template tag to more-or-less allow this. Usage looks something like this: # server/FOO/views.py
@template
class MixedBackendExampleReactTemplate(NamedTuple):
title: str // client/templates/MixedBackendExampleReactTemplate.tsx
import React from "react";
import { templates } from "@reactivated";
import { Counter } from "../components/Counter";
export default (props: templates.MixedBackendExampleReactTemplate) => (
<React.StrictMode>
<section>
<h1>{props.title}</h1>
<p>
This section is rendered by a React component. It's
server-side rendered and rehydrated on the client side. To prove
that, try out the interactive counter below.
</p>
<Counter />
</section>
</React.StrictMode>
); {# Any normal Django template #}
{% load include_react from FOO_tags %}
<!DOCTYPE html>
<html>
<body>
{% include_react "server.FOO.views.MixedBackendExampleReactTemplate" title="Mixed Backend Example" %}
</body>
</html> The implementation isn't exactly ideal, since (1) every instance of the @silviogutierrez I think reactivated is probably better off without including functionality. But if you think that tag would make sense here, let me know and I'll see what I can do. |
Beta Was this translation helpful? Give feedback.
Great question. Not in the official package.
I actually created the inverse code for a project of mine. You can replace top level templates with React, but the "inner" blocks-only templates continue being rendered by Django. To ease transition. The idea being that rewriting one "base.html" once to React but leaving dozens of "widget_detail.html", "trinket_detail.html" etc made more sense.
FYI you could just inject the "static/dist/index.js" file in there and render React into
#app
without React.render, but obviously you lose SSR and at this point are just using the Reactivated bundling tools and not the renderer.I'll have to explore what common "transition" scenarios are.