Fetch content from external URL in themes
Guide to fetching content from external URLs in Shoporama themes with the Smarty fetch feature.
Shoporama themes allow you to pull content from external URLs directly into your templates. This is useful if you want to display data from an external API, load a JSON feed or include content from another server.
Note that
The fetch_file function is available in Smarty 2 themes. In Smarty 4, use Smarty's built-in fetch function instead.
Syntax and parameters
The fetch_file function takes three parameters:
| Parameter | Required | Description |
|---|---|---|
url | Yes | The URL to retrieve (http/https only) |
format | No Set to json to parse as array. | Set to json to parse as array. Omit for plain text |
assign | No Set to json to parse as array. | Variable name to which the content is assigned |
Example: Get JSON data
Here we fetch a JSON file and use the data in the template:
<{fetch_file url="https://example.com/data.json" format="json" assign="data"}>
If the JSON file contains:
{ "name": "Billy Bob", "email": "billy@example.com" }
you can now use the data like this:
<p>Name: <{$data.name}></p> <p>Email: <{$data.email}></p>
Example: Fetch as plain text
If you omit the format parameter, the content will be returned as plain text:
<{fetch_file url="https://example.com/snippet.html" assign="snippet"}> <div><{$snippet}></div>
Smarty 4: Use fetch instead
In Smarty 4 themes, you use Smarty's built-in fetch function:
<{fetch file="https://example.com/data.txt" assign="content"}> <p><{$content}></p>
Tip to keep in mind
Be aware that external calls can affect your page load time. Consider caching the result or using asynchronous calls via JavaScript if the data is not critical for the initial page view.
Do you need help? Contact us at support@shoporama.dk.