hof-template-mixins v6.0.2
hof-template-mixins
A middleware that exposes a series of Mustache mixins on res.locals to ease usage of forms, translations, and some other things.
Installation
npm install [--save] hof-template-mixins;Usage
var express = require('express');
var i18n = require('i18n-future');
var mixins = require('hof-template-mixins');
app.set('view engine', 'html');
app.set('views', path.join(__dirname, '/views'));
app.use(i18n.middleware());
app.use(mixins());
app.use(function (req, res) {
// NOTE: res.locals.partials has been set.
res.render('example-template');
});If rendering as part of a hof controller's middleware chain then the field configuration will automatically be set to res.locals.options.fields by the controller, and will be loaded from here by the mixins.
Alternatively, if not using hof controllers, you can explicitly set field configuration with instantiating the middleware by passing a fields option. This should not be used for dynamic field configuration.
Translation
By default any function set to req.translate will be used for translation if it exists. For example, that generated using i18n-future middleware.
Options
viewsDirectory
Allows you override the directory that the module checks for partials in - Default: the root of this project
viewEngine
Allows you to alter the file extension of the templates - Default: 'html'
sharedTranslationsKey
Prefixes keys for translation - Default: ''
translate
Defines a custom translation method - Default: req.translate
fields
Allows for manually setting static field configuration - Default: null
Mustache mixins available
t
time
selected
lowercase
uppercase
hyphenate
date
currency
select
input-text
input-date
input-text-compound
input-text-code
input-number
input-phone
radio-group
checkbox
checkbox-compound
checkbox-required
checkbox-group
input-submit
textarea
qs
renderFieldqs
This mixin takes a key=value query string and returns a query string with the extra params appended. If the key is already present in the query string, the value passed to the mixin is used
<a href="{{#qs}}key=value{{/qs}}">Click to append query</a>renderField
The renderField mixin can be called in your template to render all fields. This will lookup the field.mixin in res.locals and call it passing the field key.
{{#fields}}
{{#renderField}}{{/renderField}}
{{/fields}}fields.js
module.exports = {
'my-field': {
mixin: 'input-text'
}
}If mixin is omitted input-text will be used
To disable auto-rendering of a field, set disableRender: true in the field config. This is required when using the child element rendering functionality to prevent the field being rendered multiple times.
Render a single field
To render a specific fields in your templates use the mixin name (matching those above) and field name like so...
{{#input-text}}myTextField{{/input-text}}
{{#select}}mySelectMenu{{/select}}
{{#radio-group}}myRadioGroup{{/radio-group}}Options
className: A string or array of string class names.label: The intended value of the HTMLlabelattribute.type: The value of the HTML inputtypeattribute.required: Value applied toaria-requiredHTML attribute.hint: This adds context to the label, which it is a part of, for input text, radio groups and textarea. It is used within the input by aria-describedby for screen readers.maxlength: Applicable to text-based fields and mapped to themaxlengthHTML attribute.options: Applicable to HTMLselectandradiocontrols and used to generate the items of either HTML element.selected: Applicable toselect,checkbox, andradiocontrols. Will render the selected HTML option/element selected or checked.legend: Applicable toradiobutton controls, which are wrapped in a HTMLfieldsetwith alegendelement.legendClassName: Applied as a class name to HTMLlegendattribute.toggle: Can be used to toggle the display of the HTML element with a matchingid. See hof-frontend-toolkit for details.attributes: A hash of key/value pairs applicable to a HTMLtextareafield. Each key/value is assigned as an attribute of thetextarea. For examplespellcheck="true".child: Render a child partial beneath each option in anoptionGroup. Accepts a custom mustache template string, a custom partial in the formatpartials/{your-partial-name},'html'which is used to specify the html for the field has already been prerendered, such as in hof-component-date or a template mixin key which will be rendered within a panel element partial.
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
7 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago