react-html-template-loader
A Webpack loader allowing imports of HTML templates as if they were React components.
This project is a Proof of Concept that it is possible to write React pure functional components like HTML5 templates, almost as a Web Component.
Usage
./click-me-view.jsx.html
Clicked {{ props.clicks }} time(s)
./click-me-container.jsx
; // Import the HTML template as if it was a React component.; { superprops; thisstate = clicks: 0 ; thisbuttonProps = onMouseDown: thishandleMouseDown ; } { e; this; } { return <ClickMeView buttonProps= thisbuttonProps clicks= thisstateclicks /> ; }
Add the react-html-template-loader to your webpack.config.js:
module: loaders: test: /\.jsx\.html$/ exclude: /node_modules/ loader: 'babel!react-html-template' resolve: extensions: '.jsx' '.jsx.html'
Features
- Default and named imports/exports,
- Multiple template definitions in the same file,
- Explicit conditional and loop rendering,
- Props spreading,
- CSS modules.
Installation
npm install --save-dev react-html-template-loader
Background
Why this POC?
React provides a great developing experience, you finally have a strong integration between the JavaScript code and the template syntax, it feels natural to write.
But this merge isn't that good for designers who just know enough HTML and, depending on the requirements, it can be a disqualifying criteria for React.
Thanks to the pure functional components and the Presentational and Container pattern, most components are simply templates having data as input and some UI as output. What if those pure functional templates could simply be written in HTML to be easily created and modified by designers?
The purpose of this POC is to show that it is possible to use HTML components as a React pure functional component.
react-html-template-loader reconcile developers and designers. It is a Webpack loader compiling HTML templates into pure functional React components.
Demo
Some demos can be found under the demo/
folder, to launch one type in a
console:
npm run demo -- <demo-path>
For example:
npm run demo -- demo/todo-list
API
Imports
Component import
Default import
Import the default export of a file.
Usage
Attributes
rel
: Must be set toimport
for this kind of relation,href
: Path of the file to import,id
: Name to use to reference the default export of the file.
Example
Is equivalent in ES2015 to:
;
Named imports
Import an export by its name. The <link>
tag for a named import must be
child of an other <link>
tag having a href
attribute.
Usage
Attributes
rel
: Must be set toimport
for this kind of relation,href
: Path of the file to import,name
(Optional): Name of the component to import, can be omitted if it is the same asid
,id
: Name to use to reference the export.
Example
Is equivalent in ES2015 to:
;
Default and named imports
Import the default and some named exports from the same file.
Usage
Attributes
- See default imports and named imports.
Example
Is equivalent in ES2015 to:
;
Stylesheet import (CSS Modules)
Global stylesheet
Import a global stylesheet.
Usage
Attributes
rel
: Must be set tostylesheet
for this kind of relation,href
: Path of the file to import.
Example
Is equivalent in ES2015 to:
;
Named stylesheet
Import a stylesheet and name it.
Usage
Attributes
rel
: Must be set tostylesheet
for this kind of relation,href
: Path of the file to import,id
: Value to use to reference the stylesheet.
Example
Is equivalent in ES2015 to:
;
It can be used this way:
Templates
A template is an HTML tag containing a single root child.
Default template
Each file must contain at most one default template. A default template is the main template of the file.
Usage
<!-- Content -->
Attributes
default
: Flag the default template,id
(Optional): Tag name to use to reference this template. Also used to set thedisplayName
of the template for debug purpose.
Example
Hello World
Is equivalent in React to:
{ return <div>Hello World</div> ;}HelloWorlddisplayName = 'HelloWorld';
Named templates
A named template is simply a template with an id
attribute, which means it
can be used by referencing its name. All named templates will be exported under
their given name.
Usage
<!-- Content -->
Attributes
id
: Tag name to use to reference this template. Also used to set thedisplayName
of the template for debug purpose.
Example
<!-- ... --> <!-- ... --> <!-- ... -->
Is equivalent in React to:
{ return // ... ;}NamedTemplatedisplayName = 'NamedTemplate'; { return // ... <NamedTemplate /> // ... ;}
Loops
A loop will render its content for each element in the array. The render
tag
can only have one child. When looping over an array, an id
tag must be set on
each child tag.
Usage
<!-- Content -->
Attributes
for-each
: Array of data,as
: Name of the variable to use for each element.
Example
{{ user.name }}
Is equivalent in React to:
{ return <div className="users"> propsusers </div> ;}
Conditionals
A conditional will render its content depending on a condition. The render
tag
can only have one child.
Usage
<!-- Content -->
Attributes
if
: Condition to fulfill for the content to be rendered.
Example
{{ props.user.name }}
Is equivalent in React to:
{ return <div className="user"> propsuser && <div> propsusername </div> </div> ;}
Props spreading
Props spreading is used to simplify the template so the focus can be kept on the UI.
Usage
<!-- Content -->
Attributes
use-props
: Variable that will be spread in the corresponding tag.
Example
Instead of writing:
Clicked {{ props.clicks }} time(s)
Just write:
Clicked {{ props.clicks }} time(s)
Which is equivalent in React to:
{ return <button ...propsbuttonProps > Clicked propsclicks </button> ;}
License
MIT.