Skip to content

Commit 35dd601

Browse files
authored
Merge branch 'bigskysoftware:main' into main
2 parents 0151fe9 + 1b89491 commit 35dd601

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ Corresponding issue:
1212

1313
## Checklist
1414

15-
* [ ] I have read the [contribution guidelines](/CONTRIBUTING.md)
15+
* [ ] I have read the [contribution guidelines](https://github.com/bigskysoftware/htmx-extensions/blob/main/CONTRIBUTING.md)
1616
* [ ] I ran the test suite locally (`npm run test`) and verified that it succeeded

src/client-side-templates/README.md

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
This extension supports transforming a JSON/XML request response into HTML via a client-side template before it is
32
swapped into the DOM. Currently four client-side templating engines are supported:
43

@@ -63,7 +62,7 @@ a [`<template>` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/t
6362
<meta name="viewport" content="width=device-width">
6463
<meta name="htmx-config" content='{"selfRequestsOnly":false}'>
6564
<title>JS Bin</title>
66-
<script src="https://unpkg.com/htmx.org@2.0.0"></script>
65+
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
6766
<script src="https://unpkg.com/htmx-ext-client-side-templates@2.0.0/client-side-templates.js"></script>
6867
<script src="https://unpkg.com/mustache@latest"></script>
6968
</head>
@@ -79,7 +78,7 @@ a [`<template>` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/t
7978
<p id="content">Start</p>
8079

8180
<template id="foo">
82-
<p> {{userID}} and {{id}} and {{title}} and {{completed}}</p>
81+
<p> {{userId}} and {{id}} and {{title}} and {{completed}}</p>
8382
</template>
8483
</div>
8584
</body>
@@ -96,7 +95,7 @@ Here's a working example using the `mustache-array-template` working against an
9695
<meta name="viewport" content="width=device-width">
9796
<meta name="htmx-config" content='{"selfRequestsOnly":false}'>
9897
<title>JS Bin</title>
99-
<script src="https://unpkg.com/htmx.org"></script>
98+
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
10099
<script src="https://unpkg.com/htmx-ext-client-side-templates@2.0.0/client-side-templates.js"></script>
101100
<script src="https://unpkg.com/mustache@latest"></script>
102101
</head>
@@ -141,8 +140,8 @@ Some styling is needed to keep the object visible while not taking any space.
141140
<meta name="htmx-config" content='{"selfRequestsOnly":false}'>
142141
<title>Weather with htmx</title>
143142
<link rel="stylesheet" href="https://unpkg.com/mvp.css@1.15.0/mvp.css" />
144-
<script src="https://unpkg.com/htmx.org@2.0.2"></script>
145-
<script src="https://unpkg.com/htmx-ext-client-side-templates@2.0.2/client-side-templates.js"></script>
143+
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
144+
<script src="https://unpkg.com/htmx-ext-client-side-templates@2.0.0/client-side-templates.js"></script>
146145
</head>
147146

148147
<body>
@@ -190,6 +189,63 @@ Some styling is needed to keep the object visible while not taking any space.
190189
[demo (external link)](https://barakplasma.github.io/htmx-weather/)
191190

192191

192+
### Nunjucks Integration
193+
194+
The **Nunjucks** template engine offers a highly versatile option for transforming JSON/XML responses into HTML. Nunjucks provides additional functionality: if the specified template is not found in the document (e.g., via a `<script>` tag), it will send an AJAX request to fetch the template from the server dynamically. How Nunjucks Resolves Templates:
195+
196+
1. **Client-Side Template Resolution**: If a [`<script>` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script) or [`<template>` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template) with the specified template ID (e.g., `dynamic-template`) exists, Nunjucks will render the data using this template.
197+
2. **Server-Side Template Resolution**: If the template is not found in the DOM, Nunjucks will automatically make an HTTP request to fetch the template file from the server using the name as the file path. For example, specifying `nunjucks-template="user-template.html"` will prompt Nunjucks to request `/user-template.html` from the server. This capability is particularly useful for creating dynamic, server-generated templates that aren't hardcoded into the page.
198+
199+
200+
```html
201+
<!DOCTYPE html>
202+
<html lang="en">
203+
<head>
204+
<meta charset="utf-8">
205+
<meta name="viewport" content="width=device-width">
206+
<title>Nunjucks Example</title>
207+
<!-- Include HTMX, the extension, and Nunjucks -->
208+
<meta name="htmx-config" content='{"selfRequestsOnly":false}'>
209+
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
210+
<script src="https://unpkg.com/htmx-ext-client-side-templates@2.0.0/client-side-templates.js"></script>
211+
<script src="https://cdnjs.cloudflare.com/ajax/libs/nunjucks/3.2.4/nunjucks.min.js"></script>
212+
</head>
213+
<body>
214+
<div hx-ext="client-side-templates">
215+
<h1>Nunjucks Template Example</h1>
216+
217+
<!-- Button to render using a client-side template -->
218+
<button hx-get="https://jsonplaceholder.typicode.com/todos/1"
219+
hx-swap="innerHTML"
220+
hx-target="#content"
221+
nunjucks-template="dynamic-template">
222+
Render with Client-Side Template
223+
</button>
224+
225+
<!-- Placeholder for rendering -->
226+
<div id="content">Waiting for data...</div>
227+
228+
<!-- Client-side template defined here -->
229+
<script type="text/template" id="dynamic-template">
230+
<ul>
231+
<li><strong>User ID:</strong> {{ userId }}</li>
232+
<li><strong>Title:</strong> {{ title }}</li>
233+
<li><strong>Completed:</strong> {{ completed }}</li>
234+
</ul>
235+
</script>
236+
237+
<!-- Button to render using a server-side template -->
238+
<button hx-get="https://jsonplaceholder.typicode.com/todos/2"
239+
hx-swap="innerHTML"
240+
hx-target="#content"
241+
nunjucks-template="user-template.html">
242+
Render with Server-Side Template
243+
</button>
244+
</div>
245+
</body>
246+
</html>
247+
```
248+
193249
## CORS and REST/JSON
194250

195251
As a warning, many web services use CORS protection and/or other protection schemes to reject a

0 commit comments

Comments
 (0)