Template Syntax¶
Placeholders in Word templates use the ${...} syntax. The package replaces them with values from your export class or model.
Simple variables¶
The key must match a token key from your export (e.g. from GlobalTokens::values() or itemTokens()).
Global variables¶
Set via GlobalTokens::values() or GlobalVariables::setVariable() / setVariables():
Blocks (loops)¶
Blocks repeat a section for each item in a collection:
blockNamemust match the name returned byblockName()(e.g.TokensFromCollection).- Inside the block, use keys from
itemTokens($item)(e.g.field1,field2). - Nested relations use dot notation:
nestedRelation.field.
Example template:
${customer}
${name}, ${email}
${deliveryAddress.street}, ${deliveryAddress.city} ${deliveryAddress.postcode}
${/customer}
Relation variables¶
For Eloquent models (Exportable trait), access relations with dot notation:
Condition-based relation variables¶
When using the Exportable trait, you can reference a single related record by ID or by a condition:
By ID (single record):
By condition (field, operator, value):
Supported operators: =, !=, >, <, >=, <=. If no record matches, the value is null.
Block naming conventions¶
- Block names must match between template and code (e.g.
blockName()and${blockName}...${/blockName}). - Use lowercase and underscores (e.g.
customer,order_items). - No spaces or special characters in block names.