@soleil-se/app-util 2.1.4 → 2.2.0

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
@@ -123,7 +123,8 @@ Get URI for a resource.
123
123
  const resourceUri = getResourceUri('file/in/resource.png');
124
124
  ```
125
125
  ### `renderTemplate(template, [values])` ⇒ `String`
126
- Renders a Underscore template and returns a string.
126
+ Renders a Underscore template and returns a string.
127
+ This function is also available inside the template.
127
128
 
128
129
  **Returns**: `String` - Rendered template
129
130
 
@@ -133,11 +134,38 @@ Renders a Underscore template and returns a string.
133
134
  | [values] | `Object` | `{}` | Values. |
134
135
 
135
136
  #### Example
137
+ **Simple example**
136
138
  ```javascript
137
139
  const string = renderTemplate('<div><%= foo %></div>', {
138
140
  foo: 'bar',
139
141
  });
140
142
  ```
143
+ **Multiple templates**
144
+ ```html
145
+ /* views/item.html */
146
+ <li>
147
+ <%- name %>
148
+ </li>
149
+ ```
150
+ ```html
151
+ /* views/main.html */
152
+ <ul>
153
+ <% items.forEach(function(item) { %>
154
+ <%= renderTemplate(itemTemplate, item) %>
155
+ <% }); %>
156
+ </ul>
157
+ ```
158
+
159
+ ```javascript
160
+ import { renderTemplate } from '@soleil-se/webapp-util';
161
+ import mainTemplate from './views/main.html';
162
+ import itemTemplate from './views/item.html';
163
+
164
+ const items = [{ name: 'Foo' }, { name: 'Bar' }, { name: 'Baz' }];
165
+ const string = renderTemplate(mainTemplate, { items, itemTemplate });
166
+ ```
167
+ > **NOTE**
168
+ > Remember that the second argument must be an object and that objects properties are accessed directly in any child templates!
141
169
  ### `isOffline`
142
170
  If the webapp is running in offline mode or not.
143
171
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "2.1.4",
3
+ "version": "2.2.0",
4
4
  "description": "Utility functions for Webapps.",
5
5
  "main": "./src/index.js",
6
6
  "author": "Soleil AB",
package/src/index.js CHANGED
@@ -31,9 +31,9 @@ export function getResourceUri(resource) {
31
31
  */
32
32
  export function renderTemplate(template, values = {}) {
33
33
  if (typeof template === 'function') {
34
- return template(values);
34
+ return template({ ...values, renderTemplate });
35
35
  }
36
- return _.template(template)(values);
36
+ return _.template(template)(({ ...values, renderTemplate }));
37
37
  }
38
38
 
39
39
  /**