@nubond/posthtml-value-interpolation
v1.0.4
Published
PostHTML plugin for Parcel that compiles mustache-style {{ expression }} templates into nuBond nb-value bindings at build time
Maintainers
Readme
What it does
Write Mustache-style templates:
<h1>Hello, {{ this.name }}!</h1>The plugin emits nuBond-native bindings:
<h1>Hello, <span nb-value=" this.name "></span>!</h1>Installation
If you scaffolded your project from
npm create nubond, this plugin is already wired up — no need to do anything.
- Install the package:
npm install --save-dev @nubond/posthtml-value-interpolationAdd a
.posthtmlrcfile to the root of your Parcel project.Place the following content in the file:
{
"plugins": {
"@nubond/posthtml-value-interpolation": {}
}
}Or use it directly with PostHTML:
import posthtml from 'posthtml';
import valueInterpolation from '@nubond/posthtml-value-interpolation';
const { html } = await posthtml([valueInterpolation()])
.process('<div>{{ this.user.name }}</div>');Supported expressions
Anything between {{ … }} is passed verbatim into nb-value, so the full nuBond expression grammar works — member access, method calls, ternaries, template literals, repeat-scope vars, expression prefixes (#, @), etc.
The plugin only transforms text nodes — HTML attributes (<div class="{{ x }}">) and unmatched braces are left untouched.
W3C-compliant mode
If your app runs nuBond in W3C compliance mode (complyWithW3C: true), the bindings must use the data-nb- prefix. Switch the plugin over in the root package.json of your project:
{
"nubond": {
"compliantWithW3CInterpolation": true
}
}The plugin then emits data-nb-value instead of nb-value:
<h1>Hello, <span data-nb-value=" this.name "></span>!</h1>The flag is read from the nearest package.json, searching upwards from the current working directory (the project root when Parcel runs). A missing flag means nb-value. A non-boolean value or an unreadable package.json logs an error, and the plugin falls back to nb-value.
You can also set the mode in the plugin options, which take precedence over package.json. Use root to change the directory the search starts from:
{
"plugins": {
"@nubond/posthtml-value-interpolation": { "compliantWithW3CInterpolation": true }
}
}The plugin reports
package.jsonas a dependency of each template, so after you change the flag, Parcel transforms the templates again with no need to clear.parcel-cache.
License
MIT © Dmytro Tomayly
