ha-nunjucks 1.2.1 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/dist/context.d.ts +1 -1
- package/dist/renderTemplate.d.ts +2 -2
- package/dist/renderTemplate.js +1 -1
- package/dist/utils/iif.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
|
|
8
8
|
[![Github][github]][github]
|
|
9
9
|
|
|
10
|
-
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.
|
|
10
|
+
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.
|
|
11
11
|
|
|
12
12
|
## What is nunjucks?
|
|
13
13
|
|
|
14
14
|
[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.
|
|
15
15
|
|
|
16
|
-
While some Home Assistant native cards support templating for certain fields, implementing proper Home Assistant jinja2 template support in custom cards
|
|
16
|
+
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.
|
|
17
17
|
|
|
18
18
|
## Usage
|
|
19
19
|
|
|
@@ -48,6 +48,14 @@ const context = {
|
|
|
48
48
|
const renderedString = renderTemplate(this.hass, templateString, context);
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
### Return Types
|
|
52
|
+
|
|
53
|
+
`renderTemplate` will return a string unless the result is `true` or `false` (*not* case sensitive), in which case it will return a boolean.
|
|
54
|
+
|
|
55
|
+
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.
|
|
56
|
+
|
|
57
|
+
`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).
|
|
58
|
+
|
|
51
59
|
## Available Extensions
|
|
52
60
|
|
|
53
61
|
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.
|
package/dist/context.d.ts
CHANGED
|
@@ -9,6 +9,6 @@ export declare const CONTEXT: (hass: HomeAssistant) => {
|
|
|
9
9
|
state_attr(entity_id: string, attribute: string): any;
|
|
10
10
|
is_state_attr(entity_id: string, attribute: string, value: string): boolean;
|
|
11
11
|
has_value(entity_id: string): boolean;
|
|
12
|
-
iif(condition: string, if_true: string, if_false?: string, if_none?: string): string |
|
|
12
|
+
iif(condition: string, if_true: string, if_false?: string, if_none?: string): string | boolean;
|
|
13
13
|
match_media(mediaquery: string): boolean;
|
|
14
14
|
};
|
package/dist/renderTemplate.d.ts
CHANGED
|
@@ -4,6 +4,6 @@ import { HomeAssistant } from 'custom-card-helpers';
|
|
|
4
4
|
* @param {HomeAssistant} hass The Home Assistant object
|
|
5
5
|
* @param {string} str The template string to render
|
|
6
6
|
* @param {object} [context] Additional context to expose to nunjucks
|
|
7
|
-
* @returns {string} The rendered template string if a string was provided, otherwise the unaltered input
|
|
7
|
+
* @returns {string | boolean} The rendered template string if a string was provided, otherwise the unaltered input
|
|
8
8
|
*/
|
|
9
|
-
export declare function renderTemplate(hass: HomeAssistant, str: string, context?: object): string |
|
|
9
|
+
export declare function renderTemplate(hass: HomeAssistant, str: string, context?: object): string | boolean;
|
package/dist/renderTemplate.js
CHANGED
|
@@ -8,7 +8,7 @@ const context_1 = require("./context");
|
|
|
8
8
|
* @param {HomeAssistant} hass The Home Assistant object
|
|
9
9
|
* @param {string} str The template string to render
|
|
10
10
|
* @param {object} [context] Additional context to expose to nunjucks
|
|
11
|
-
* @returns {string} The rendered template string if a string was provided, otherwise the unaltered input
|
|
11
|
+
* @returns {string | boolean} The rendered template string if a string was provided, otherwise the unaltered input
|
|
12
12
|
*/
|
|
13
13
|
function renderTemplate(hass, str, context) {
|
|
14
14
|
if (typeof str == 'string' &&
|
package/dist/utils/iif.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { HomeAssistant } from 'custom-card-helpers';
|
|
2
|
-
export declare function iif(hass: HomeAssistant, condition: string, if_true?: string, if_false?: string, if_none?: string): string |
|
|
2
|
+
export declare function iif(hass: HomeAssistant, condition: string, if_true?: string, if_false?: string, if_none?: string): string | boolean;
|