[reactjs] Loading basic templates / includes from HTML-syntaxed files (in addition to existing inlining in JSX) #34037
Replies: 4 comments 4 replies
-
Beta Was this translation helpful? Give feedback.
-
Yes, there are a few libraries and tools that can parse HTML-based templates (like Liquid, Jinja2, Vue-style templates) into React elements, though with some limitations, especially around logic like loops and conditionals. Such Libraries
the Approach I Suggest
import { Liquid } from 'liquidjs';
import parse from 'html-react-parser';
const engine = new Liquid();
const template = 'Hello, {{ name }}!';
const rendered = await engine.parseAndRender(template, { name: 'Akshay' });
const reactComponent = parse(rendered); |
Beta Was this translation helpful? Give feedback.
-
This feels like trying to bring back templates into an architecture that deliberately killed them. |
Beta Was this translation helpful? Give feedback.
-
React itself doesn’t natively load or import raw .html templates the way server-side templating engines do. Import as a string (build-time) With Webpack/Vite, you can use an HTML loader or raw-loader to import the file as a string, then set it with dangerouslySetInnerHTML. import myTemplate from './template.html?raw'; export default function MyComponent() { Fetch at runtime Store your HTML files in public/ and fetch them dynamically. const [html, setHtml] = useState(''); Use a templating library If you need includes/partials, you could preprocess HTML using a tool like Handlebars, Nunjucks, or EJS before feeding it into your React component. Important: This bypasses React’s JSX compilation benefits and could introduce XSS risks, so sanitize HTML when dealing with user-generated content. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there an existing feature request / issue to vote it up?
I guess this must have been discussed in the past, I'm looking for a way to store the HTML templates in separate HTML files (that is without import / export directives, function definitions etc), like asked at https://stackoverflow.com/questions/45600891/reactjs-how-to-put-the-html-template-in-a-separate-file
This is would be for converting existing templated static HTML templates from SSG engines (like Jekyll) to React/Nextjs. And also probably a better fit for HTML/CSS-generating LLMs.
Where we first ask to generate HTML/CSS with some bogus data, and then minimally convert these HTMLs into Reactjs templates / components, while still keeping them in HTML syntax without turning it fully into JSX
In other words, is there any support (or extension points for that) for defining rendering templates in formats other than JSX/JS? E.g. jinja2 / liquid / mdx / vuejs templating formats?
Maybe using https://react.dev/reference/react/createElement#creating-an-element-without-jsx under the hood for parsing the loaded HTML (like in https://stackoverflow.com/a/50526629/445810)
Also seems related: https://github.com/roonie007/react-sfc
Beta Was this translation helpful? Give feedback.
All reactions