Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #135: client-side-templates: Example for Resolving Nunjucks #136

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/client-side-templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ a [`<template>` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/t
<p id="content">Start</p>

<template id="foo">
<p> {{userID}} and {{id}} and {{title}} and {{completed}}</p>
<p> {{userId}} and {{id}} and {{title}} and {{completed}}</p>
</template>
</div>
</body>
Expand Down Expand Up @@ -190,6 +190,63 @@ Some styling is needed to keep the object visible while not taking any space.
[demo (external link)](https://barakplasma.github.io/htmx-weather/)


### Nunjucks Integration

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:

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.
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.


```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Nunjucks Example</title>
<!-- Include HTMX, the extension, and Nunjucks -->
<meta name="htmx-config" content='{"selfRequestsOnly":false}'>
<script src="https://unpkg.com/htmx.org"></script>
Telroshan marked this conversation as resolved.
Show resolved Hide resolved
<script src="https://unpkg.com/htmx-ext-client-side-templates@2.0.0/client-side-templates.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nunjucks/3.2.4/nunjucks.min.js"></script>
</head>
<body>
<div hx-ext="client-side-templates">
<h1>Nunjucks Template Example</h1>

<!-- Button to render using a client-side template -->
<button hx-get="https://jsonplaceholder.typicode.com/todos/1"
hx-swap="innerHTML"
hx-target="#content"
nunjucks-template="dynamic-template">
Render with Client-Side Template
</button>

<!-- Placeholder for rendering -->
<div id="content">Waiting for data...</div>

<!-- Client-side template defined here -->
<script type="text/template" id="dynamic-template">
<ul>
<li><strong>User ID:</strong> {{ userId }}</li>
<li><strong>Title:</strong> {{ title }}</li>
<li><strong>Completed:</strong> {{ completed }}</li>
</ul>
</script>

<!-- Button to render using a server-side template -->
<button hx-get="https://jsonplaceholder.typicode.com/todos/2"
hx-swap="innerHTML"
hx-target="#content"
nunjucks-template="user-template.html">
Render with Server-Side Template
</button>
</div>
</body>
</html>
```

## CORS and REST/JSON

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