diff --git a/README.md b/README.md index c05d960..f50cdfc 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,13 @@ [![Github][github]][github] -A simple wrapper for [nunjucks](https://www.npmjs.com/package/nunjucks) for use with Home Assistant frontend custom components to render [templates](https://www.home-assistant.io/docs/configuration/templating/). This repository offers an easy way for developers to add templating support to Home Assistant custom cards. +A simple wrapper for [nunjucks](https://www.npmjs.com/package/nunjucks) for use with Home Assistant frontend custom components to render [templates](https://www.home-assistant.io/docs/configuration/templating/) instanteneously at HTML render time. This repository offers an easy way for developers to add templating support to Home Assistant custom cards. ## What is nunjucks? [Nunjucks](https://mozilla.github.io/nunjucks/) is a templating engine for JavaScript that is heavily inspired by jinja2. Home Assistant uses jinja2 to process templates in card configurations on the backend, so the syntax of jinja2 and Nunjucks is virtually the same. This makes it an excellent alternative for Home Assistant templating for custom cards. -While some Home Assistant native cards support templating for certain fields, implementing proper Home Assistant jinja2 template support in custom cards is difficult. The only custom module that manages to do so (AFAIK) is card-mod, and it's implementation is hard to port to other projects. This project offers a much easier plug and play solution to adding template support to custom cards. +While some Home Assistant native cards support templating for certain fields, implementing proper Home Assistant jinja2 template support in custom cards can be difficult. Additionally Home Assistant jinja2 templates are processed by the Python backend, and can take several seconds to render. Nunjucks templates are processed by the frontend using the frontend [hass](https://developers.home-assistant.io/docs/frontend/data/) object before your custom card's HTML is rendered, making nunjucks templating instanteous and much faster than traditional jinja2 templates. ## Usage @@ -48,6 +48,14 @@ const context = { const renderedString = renderTemplate(this.hass, templateString, context); ``` +### Return Types + +`renderTemplate` will return a string unless the result is `true` or `false` (*not* case sensitive), in which case it will return a boolean. + +When the return type is expected to be a number, end users should cast these values using the nunjucks `int` or `float` filters to prevent undesired behavior caused by JavaScript forcing operations between disparate variable types. Numbers are not returned by default to prevent leading and zeroes from being truncated from numerical strings. + +`renderTemplate` will return an empty string for strings that may have been cast from nullish non-numerical values, such as `undefined`, `null`, and `None` (case sensitive). + ## Available Extensions The catch to this approach of rendering jinja2/nunjucks templates is that we have to reimplement all of the [Home Assistant template extension](https://www.home-assistant.io/docs/configuration/templating/#home-assistant-template-extensions) functions and filters. If there are functions or filters that you use that are not currently supported, please make a feature request or try adding it to the project yourself and create a pull request. diff --git a/dist/context.d.ts b/dist/context.d.ts index 3790df5..44dd321 100644 --- a/dist/context.d.ts +++ b/dist/context.d.ts @@ -9,6 +9,6 @@ export declare const CONTEXT: (hass: HomeAssistant) => { state_attr(entity_id: string, attribute: string): any; is_state_attr(entity_id: string, attribute: string, value: string): boolean; has_value(entity_id: string): boolean; - iif(condition: string, if_true: string, if_false?: string, if_none?: string): string | number | boolean; + iif(condition: string, if_true: string, if_false?: string, if_none?: string): string | boolean; match_media(mediaquery: string): boolean; }; diff --git a/dist/renderTemplate.d.ts b/dist/renderTemplate.d.ts index a4a68fd..cca7d59 100644 --- a/dist/renderTemplate.d.ts +++ b/dist/renderTemplate.d.ts @@ -4,6 +4,6 @@ import { HomeAssistant } from 'custom-card-helpers'; * @param {HomeAssistant} hass The Home Assistant object * @param {string} str The template string to render * @param {object} [context] Additional context to expose to nunjucks - * @returns {string} The rendered template string if a string was provided, otherwise the unaltered input + * @returns {string | boolean} The rendered template string if a string was provided, otherwise the unaltered input */ -export declare function renderTemplate(hass: HomeAssistant, str: string, context?: object): string | number | boolean; +export declare function renderTemplate(hass: HomeAssistant, str: string, context?: object): string | boolean; diff --git a/dist/renderTemplate.js b/dist/renderTemplate.js index 4c859a1..3ca418f 100644 --- a/dist/renderTemplate.js +++ b/dist/renderTemplate.js @@ -8,7 +8,7 @@ const context_1 = require("./context"); * @param {HomeAssistant} hass The Home Assistant object * @param {string} str The template string to render * @param {object} [context] Additional context to expose to nunjucks - * @returns {string} The rendered template string if a string was provided, otherwise the unaltered input + * @returns {string | boolean} The rendered template string if a string was provided, otherwise the unaltered input */ function renderTemplate(hass, str, context) { if (typeof str == 'string' && diff --git a/dist/utils/iif.d.ts b/dist/utils/iif.d.ts index 1fbe7a4..26685d3 100644 --- a/dist/utils/iif.d.ts +++ b/dist/utils/iif.d.ts @@ -1,2 +1,2 @@ import { HomeAssistant } from 'custom-card-helpers'; -export declare function iif(hass: HomeAssistant, condition: string, if_true?: string, if_false?: string, if_none?: string): string | number | boolean; +export declare function iif(hass: HomeAssistant, condition: string, if_true?: string, if_false?: string, if_none?: string): string | boolean; diff --git a/src/renderTemplate.ts b/src/renderTemplate.ts index a279110..c0180ee 100644 --- a/src/renderTemplate.ts +++ b/src/renderTemplate.ts @@ -8,13 +8,13 @@ import { CONTEXT } from './context'; * @param {HomeAssistant} hass The Home Assistant object * @param {string} str The template string to render * @param {object} [context] Additional context to expose to nunjucks - * @returns {string} The rendered template string if a string was provided, otherwise the unaltered input + * @returns {string | boolean} The rendered template string if a string was provided, otherwise the unaltered input */ export function renderTemplate( hass: HomeAssistant, str: string, context?: object, -): string | number | boolean { +): string | boolean { if ( typeof str == 'string' && ((str.includes('{{') && str.includes('}}')) ||