Fetching Data

Dynamic Fetch URLs

Fetch URLs can include TemplateX values from the current view context.

Use Current Post Values#

php
{{ query:posts limit="3" }}  {{ fetch "https://api.example.com/reviews?post={{ slug }}" as review }}    <p>{{ review.title }}</p>  {{ /fetch }}{{ /query:posts }}

In this example, {{ slug }} comes from the current post in the query loop.

Each post creates its own API URL:

  • first post: https://api.example.com/reviews?post=first-post
  • second post: https://api.example.com/reviews?post=second-post

Use Modifiers#

You can use modifiers inside the URL:

php
{{ fetch "https://api.example.com/search?q={{ title | lower | urlencode }}" as result }}  <p>{{ result.name }}</p>{{ /fetch }}

This is useful when API parameters need URL-safe text.

Keep URLs Predictable#

Dynamic URLs are powerful, but each different URL may create a separate API request and cache entry.

Use stable values such as post slugs, IDs, or sanitized field values. Avoid putting private user data or secrets in a fetch URL.