Who this is for. The developer who writes template code in an admin field rather than in the repository, and whoever approves that code. Afterwards you will know which variables your code has, why it cannot always do what a template file does, where its errors go, and why changes to it deserve the same review as a commit.
A template that is not a file
Both engines can render a template handed to them as a string instead of a path. The platform uses this wherever an admin screen accepts template code. The string is rendered by the site's engine, so a Smarty site's template code is Smarty and a Twig site's is Twig, with the same syntax, the same platform helpers and, on Smarty, the same security policy as the templates in the repository.
Where the platform renders strings
| Where | What the code receives | What the code is for | When it fails |
|---|---|---|---|
| Jobs (Orchestration): template conditions, loops, and action fields that take template code | hook (the job; the variable keeps the module's old name), batch (the job run), event, site, runData, customData, siteData, query; inside a loop also loopElement and loopRecord; a condition also result | Building a request body, record data or a value; deciding a branch | The action is marked as an error, with the message in its output in the job run's log |
| Discounts: template conditions | items, cart, checkout, rule, discount, site, user, siteData, query, request, result | Deciding whether the discount applies, by calling result's setMatching(true) | The condition does not match, and the error is logged |
| Shipping: template methods | fulfillment, site, user, siteData, query, rates | Creating shipping rates | No rates are offered for the method, and the error is logged |
| Search: the facets template of the product index | index, productDescription | Adding facet values for a product | The product stays indexed without its custom facets, and the error is logged |
| Mailer emails, on a Twig site only | The email's merge data | The subject and body of a mail-out | The platform falls back to plain placeholder substitution (below) |
The underscore helpers of a page render (_request, _query, _templateContext and the rest) are not given to template code: each place passes its own short list, as above, and the code sees nothing else. Code copied from a page template that uses $_query therefore fails here; use query, where the list has it. Guide 9 owns the job side: Job action reference lists which fields take template code and what each must output.
How it differs from a template file
- Twig template code cannot include or extend a file. The string is the only template Twig is given, so
{% include 'partials/address.html' %}fails withTemplate "partials/address.html" is not defined. Smarty template code can include a file from the site's templates by its full name. - It is not in the repository. It is part of the record that holds it, changes when an editor saves that record, takes effect at once with no deploy, and is not in your version history. A site's code is therefore partly in the admin; write down where.
- It runs in the background as often as in a page request. A job or a search reindex has no visitor and no page, which is one reason the page helpers are absent.
- Output usually feeds a machine. A job's request body must be valid JSON once rendered, so every value goes through
|json_encodein either engine, never into the JSON by hand.