> For the complete documentation index, see [llms.txt](https://docs.zigpoll.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zigpoll.com/questions/template-variables.md).

# Template Variables

Make your questions dynamic using template variables

Template variables put dynamic text into a question: a customer's name, a value from your site, or an answer they gave earlier in the same survey. Write them inside double curly braces, like `{{name}}`. Zigpoll uses [Handlebars](https://handlebarsjs.com) syntax, so conditionals and loops work too.

Variables work in a question's title, subtitle, copy, disclaimer, scale labels, placeholders, form field labels, and **Actions** button titles and links. They don't work in answer options.

### Where the values come from

A variable can be any of these, in order of priority:

1. **`window.Zigpoll.templateVariables`** set by JavaScript on your page.
2. **An earlier answer in the same survey**, by that question's handle.
3. **Metadata**, such as values you pass in or that Zigpoll records (for example `utm_source`).

### Use a value from your page

For example, say you want to greet your customer by name in your survey:

> Welcome Jason, do you have a minute to fill out our survey?

**1.** Add a template variable to your page with JavaScript. In this example it's called **name**, and PHP fills it in from a user object. Replace this with whatever fits how your website is built.

```javascript
// Add this code right below your Zigpoll embed code.
window.Zigpoll.templateVariables = {
      name: <?php echo $user.name ?>
}
```

**2.** Use the **name** variable in your question. In this example it goes in the question's copy, so enter something like this in the copy box:

```javascript
Hey, {{#if name}}{{name}}{{else}}Mysterious Person{{/if}} do you have a minute to fill out our survey?
```

The question now shows the customer's name if it exists, or "Mysterious Person" if it doesn't.

You can also use the shorter `print` helper for a fallback: `Hey {{print name "there"}}!`

### Pipe an earlier answer into a later question

Each question has a **handle**. By default it is the question's title. To pipe an answer, give the earlier question a short handle with no spaces:

1. Open the earlier question, for example *Which product did you buy?*
2. Turn on **Custom Handle** and set the **Handle** to `product`.
3. Click **Update Question**.
4. In a later question, write `How would you rate the {{product}}?`

A customer who picked *Travel Mug* sees *How would you rate the Travel Mug?* For a Multiple Choice question, the selected answers are joined with commas. The value updates as soon as the earlier question is answered.

{% hint style="info" %}
Use letters, numbers and underscores in handles you pipe. A handle with spaces or punctuation, like a full question title, doesn't work as `{{handle}}`.
{% endhint %}

### Why do I see `{{ }}` instead of a value?

* **A variable with no value renders as blank, not as braces.** Use `{{#if}}` or `{{print name "fallback"}}` to show something instead.
* **Literal braces mean the template couldn't be read.** Common causes are a typo such as a missing closing brace, quotes or spaces inside the braces (`{{ "product" }}`), or a handle with spaces. Fix the syntax and click **Update Question**.
* **The field doesn't support variables.** Answer options show text exactly as written.
* **The value is set too late.** Set `window.Zigpoll.templateVariables` right after your embed code, before the survey loads.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.zigpoll.com/questions/template-variables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
