Skip to content

Commit

Permalink
fix return type of renderTemplate, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerwyn committed May 15, 2024
1 parent 8ae403e commit 48726d2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion dist/context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
4 changes: 2 additions & 2 deletions dist/renderTemplate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion dist/renderTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand Down
2 changes: 1 addition & 1 deletion dist/utils/iif.d.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/renderTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('}}')) ||
Expand Down

0 comments on commit 48726d2

Please sign in to comment.