mumba-handlebars
v1.1.0
Published
A Handlebars service with several helpers.
Downloads
74
Readme
Mumba Handlebars renderer
A Handlebars service with several helpers.
Helpers
ARRAY
Examples
Deprecated. Please progressively move examples to docblocks and move references to the list above.
Dates
<p>Format a date {{date created_at format="YYYY/MM/DD HH:mm:ss Z"}} (automatically parse the zone if provided)</p>
<p>Format a date with fixed timezone {{date created_at format="YYYY/MM/DD HH:mm:ss Z" tz="Australia/Perth"}}</p>
<p>Format a date with timezone in `this` {{date created_at format="YYYY/MM/DD HH:mm:ss Z" tz="timezone"}}</p>
<p>Format a date with timezone in another object {{date created_at format="YYYY/MM/DD HH:mm:ss Z" tz=user.timezone}}</p>
<p>Format as 'some time ago' {{dateFromNow created_at}}</p>
<p>Check if a date in the past {{#if (datePast publish_start)}}past{{/if}}</p>
<p>Check if a date in the future {{#if (dateFuture publish_start)}}future{{/if}}</p>
<p>Add to a date {{dateAdd created_at '1' 'month'}} (uses moment.add)</p>
<p>Subtract from a date {{dateSubtract created_at '1' 'month'}} (uses moment.subtract)</p>
<p>Set to the end of a period {{endOf created_at 'month'}} (uses moment.endOf)</p>
<p>Set to the start of a period {{startOf created_at 'month'}} (uses moment.startOf)</p>
<p>Get now {{now}}</p>
Equality
<p>Equal {{#if (eq foo "bar")}}true{{else}}false{{/if}}</p>
<p>Equal to null {{#if (eq foo null)}}true{{else}}false{{/if}}</p>
<p>Not equal {{#if (ne foo "bar")}}true{{else}}false{{/if}}</p>
<p>Less than or equal to {{#if (lte foo 2)}}true{{else}}false{{/if}}</p>
<p>Greater than {{#if (gt foo 2)}}true{{else}}false{{/if}}</p>
<p>Greater than or equal to {{#if (gte foo 2)}}true{{else}}false{{/if}}</p>
<p>Chaining and {{#if (and (gt foo 1) (lt foo 3))}}true{{else}}false{{/if}}</p>
<p>Chaining or {{#if (or (eq foo false) (eq foo true))}}true{{else}}false{{/if}}</p>
import { ObjectBuilder } from 'mumba-handlebars';
const source = {
// equalities
eq: '{{#if (eq foo "bar")}}true{{else}}false{{/if}}'
};
const data = {
foo: 'bar'
};
const builder = new ObjectBuilder(source);
const result = builder.build(source, data);
// result.eq == 'true'
Internationalisation
See Number.prototype.toLocaleString() .
<p>Number format {{numf num}}</p>
<p>Accepts any options for `toLocaleString`, for example:</p>
<p>Number format {{numf num maximumFractionDigits="1"}}</p>
<p>Currency format {{curf num}}</p>
<p>- Defaults to AUD</p>
<p>- Accepts any options for `toLocaleString`, for example:</p>
<p>Currency format {{curf num locale="fr-DE" currency="EUR"}}</p>
Markdown
{{{markdown body}}}
Math
<p>Any Math object method {{math "sign" num}}</p>
<p>Add {{add num -0.05}}</p>
<p>Subtract {{subtract num 0.05}}</p>
<p>Multiply {{multiply num 2}}</p>
Regular expression
<p>{{rxmatch str "^ESCT ([-\\d.\']+)"}}</p>
<p>- If str = 'ESCT -17.5%', returns '-17.5'</p>
{{first (rxsplit "full name")}}
<!-- full -->
{{last (rxsplit "waz-up" "-")}}
<!-- up -->
Strings
<p>Formated printing {{sprintf "%s %.2f" str num }}</p>
<p>Concat {{concat "foo" foo 123}}</p>
<p>Contains any of the string {{#if (in foo "ptx" "lsl")}}YES{{else}}NO{{/if}}</p>
<p>Extract initials {{initials name}}</p>
<p>Convert JSON string to object {{#with (or value (json \'{"test":"no"}\'))}}{{test}}{{/with}}</p>
<p>String starts with {{#if (startswith foo "ptx")}}YES{{else}}NO{{/if}}</p>
<p>
Time (in minutes) to read at 265 WPM {{timeToRead wordCount}}, see
https://help.medium.com/hc/en-us/articles/214991667-Read-time
</p>
<p>Inspect (JSON stringify) {{inspect foo}} - Good for debugging!</p>
URLs
<p>{{encodeUriComponent "http://localhost/test #1" }}</p>
<p>{{decodeUriComponent "http%3A%2F%2Flocalhost%2Ftest%20%231" }}</p>
License
Apache-2.0
See working with microservices for more information on how to test this package.
© 2019 Mumba Pty Ltd. All rights reserved.