ha-nunjucks 1.1.0-dev.2 → 1.2.0-alpha.1

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 CHANGED
@@ -33,6 +33,21 @@ const renderedString = renderTemplate(this.hass, templateString);
33
33
 
34
34
  Rather than rendering templates on the backend, nunjucks renders templates on the frontend. This repository uses the Home Assistant object present in all custom cards to read entity state data.
35
35
 
36
+ You can also provide additional context to the `renderTemplate` function to pass to nunjucks if you want to make additional variables or project specific functions available to your users for use in templates.
37
+
38
+ ```typescript
39
+ import { renderTemplate } from 'ha-nunjucks';
40
+
41
+ const context = {
42
+ foo: 'bar',
43
+ doThing(thing: string) {
44
+ return `doing ${thing}!`;
45
+ },
46
+ };
47
+
48
+ const renderedString = renderTemplate(this.hass, templateString, context);
49
+ ```
50
+
36
51
  ## Available Extensions
37
52
 
38
53
  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.
@@ -49,6 +64,18 @@ These variables just remap Python built-in constants to JavaScript ones.
49
64
  | False | false |
50
65
  | None | null |
51
66
 
67
+ ### [Frontend Data Hass Object](https://developers.home-assistant.io/docs/frontend/data/)
68
+
69
+ The frontend data `hass` object has been exposed to users to call upon.
70
+
71
+ Because entity IDs contain periods in them, it's better to access it using bracket notation like so:
72
+
73
+ `{{ hass["states"]["light.sunroom_ceiling"]["state"] }}`
74
+
75
+ You can also use dot notation for everything but the entity ID.
76
+
77
+ `{{ hass.states["light.sunroom_ceiling"].state }}`
78
+
52
79
  ### [States](https://www.home-assistant.io/docs/configuration/templating/#states)
53
80
 
54
81
  Functions used to determine an entity's state or an attribute.
@@ -80,7 +107,7 @@ Functions that are not from the Home Assistant templating documentation
80
107
  [last-commit-shield]: https://img.shields.io/github/last-commit/Nerwyn/ha-nunjucks?style=for-the-badge
81
108
  [commits]: https://github.com/Nerwyn/service-call-tile-feature/commits/main
82
109
  [forum-shield]: https://img.shields.io/badge/community-forum-brightgreen.svg?style=for-the-badge
83
- [forum]: https://community.home-assistant.io/t/ha-nunjucks/
110
+ [forum]: https://community.home-assistant.io/t/an-easy-way-to-add-templating-support-to-your-custom-front-end-projects/651621
84
111
  [license-shield]: https://img.shields.io/github/license/Nerwyn/service-call-tile-feature.svg?style=for-the-badge
85
112
  [maintenance-shield]: https://img.shields.io/badge/maintainer-Nerwyn-blue.svg?style=for-the-badge
86
113
  [github]: https://img.shields.io/github/followers/Nerwyn.svg?style=social
@@ -3,6 +3,7 @@ import { HomeAssistant } from 'custom-card-helpers';
3
3
  * Render a Home Assistant template string using nunjucks
4
4
  * @param {HomeAssistant} hass The Home Assistant object
5
5
  * @param {string} str The template string to render
6
+ * @param {object} [context] Additional context to expose to nunjucks
6
7
  * @returns {string} The rendered template string if a string was provided, otherwise the unaltered input
7
8
  */
8
- export declare function renderTemplate(hass: HomeAssistant, str: string): string | number | boolean;
9
+ export declare function renderTemplate(hass: HomeAssistant, str: string, context?: object): string | number | boolean;
@@ -7,13 +7,14 @@ const context_1 = require("./context");
7
7
  * Render a Home Assistant template string using nunjucks
8
8
  * @param {HomeAssistant} hass The Home Assistant object
9
9
  * @param {string} str The template string to render
10
+ * @param {object} [context] Additional context to expose to nunjucks
10
11
  * @returns {string} The rendered template string if a string was provided, otherwise the unaltered input
11
12
  */
12
- function renderTemplate(hass, str) {
13
+ function renderTemplate(hass, str, context) {
13
14
  if (typeof str == 'string' &&
14
15
  ((str.includes('{{') && str.includes('}}')) ||
15
16
  (str.includes('{%') && str.includes('%}')))) {
16
- str = (0, nunjucks_1.renderString)(structuredClone(str), (0, context_1.CONTEXT)(hass)).trim();
17
+ str = (0, nunjucks_1.renderString)(structuredClone(str), Object.assign(Object.assign({}, (0, context_1.CONTEXT)(hass)), context)).trim();
17
18
  if ([undefined, null, 'undefined', 'null', 'None'].includes(str)) {
18
19
  return '';
19
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ha-nunjucks",
3
- "version": "1.1.0-dev.2",
3
+ "version": "1.2.0-alpha.1",
4
4
  "description": "Wrapper for nunjucks for use with Home Assistant frontend custom components to render templates",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {