# Twig

# What is Twig?

**Storefront pages are built using the [Twig](https://twig.symfony.com/) template engine**.

Twig is a fast, secure and flexible PHP templating solution. Before developing with Aero, we recommend that you become acquainted with the core concepts of Twig. The official Twig documentation is a great place to start:

[Click here for the official Twig documentation](https://twig.symfony.com/doc)

# What are the available twig functions?

<table border="1" id="bkmrk-twig-function-descri" style="border-collapse: collapse; width: 100%;"><colgroup><col style="width: 33.3731%;"></col><col style="width: 33.3731%;"></col><col style="width: 33.3731%;"></col></colgroup><tbody><tr><td>**Twig function**  
</td><td>**Description**  
</td><td>**Typical Usage / Notes**  
</td></tr><tr><td data-col-size="md" data-end="257" data-start="243">`aero(key)`</td><td>Retrieves a value from the current store using the supplied key.</td><td>Useful for accessing store-level properties dynamically.</td></tr><tr><td data-col-size="md" data-end="339" data-start="326">`locale()`</td><td>Returns the current store locale as a two-letter language code, e.g. `en`.</td><td>Useful when language-specific behaviour or markup is required.</td></tr><tr><td data-col-size="md" data-end="424" data-start="407">`page_title()`</td><td>Returns the current page's SEO title.</td><td>Used for page titles and SEO-related markup.</td></tr><tr><td data-col-size="md" data-end="483" data-start="466">`store_name()`</td><td>Returns the current store's name.</td><td>Useful for displaying the store name in templates.</td></tr><tr><td data-col-size="md" data-end="538" data-start="521">`store_logo()`</td><td>Returns the URL of the current store's logo, or an empty string if no logo is configured.</td><td>Useful for displaying the store logo.</td></tr><tr><td data-col-size="md" data-end="649" data-start="632">`email_logo()`</td><td>Returns the URL of the store's email logo, or an empty string if none is configured.</td><td>Intended for email templates.</td></tr><tr><td data-col-size="md" data-end="761" data-start="741">`store_address()`</td><td>Returns the current store's address as an array.</td><td>Useful when rendering store address information.</td></tr><tr><td data-col-size="md" data-end="830" data-start="814">`active(...)`</td><td>Determines whether a given item/route is currently active using the application's active service.</td><td>Commonly used to apply active states to navigation elements.</td></tr><tr><td data-col-size="md" data-end="1012" data-start="971">`blocks(key, limit = null, data = [])`</td><td>Returns the block items belonging to a specified block/group.</td><td>Can optionally limit the number of items or filter by block type.</td></tr><tr><td data-col-size="md" data-end="1179" data-start="1133">`block_items(key, limit = null, data = [])`</td><td>Alias of `blocks()`. Returns block items for a specified block/group.</td><td>Provides an alternative, more descriptive name when working with block collections.</td></tr><tr><td data-col-size="md" data-end="1301" data-start="1259">`block_item(key, index = 0, data = [])`</td><td>Returns a specific block item from a block/group by its index.</td><td>Useful when a template needs a particular block item rather than the whole collection.</td></tr><tr><td data-col-size="md" data-end="1412" data-start="1376">`block_exists(key, count = null)`</td><td>Checks whether a block/group contains any items. Can also check whether it contains at least a specified number of items.</td><td>Useful for conditionally rendering block-based content.</td></tr><tr><td data-col-size="md" data-end="1574" data-start="1536">`block_item_exists(key, index = 0)`</td><td>Checks whether a block contains an item at a specified index.</td><td>Useful for safely checking whether a particular block item exists before rendering it.</td></tr><tr><td data-col-size="md" data-end="1662" data-start="1642">`breadcrumb(...)`</td><td>Returns the current breadcrumb/crumb collection.</td><td>Used to render breadcrumb navigation.</td></tr><tr><td data-col-size="md" data-end="1802" data-start="1775">`base64_encode(content)`</td><td>Base64-encodes the supplied content.</td><td>Useful when content needs to be represented as Base64 within a template.</td></tr><tr><td data-col-size="md" data-end="1887" data-start="1843">`image_factory(width, height, path = '')`</td><td>Creates an `ImageFactory` instance for generating/manipulating an image with the specified dimensions and optional path.</td><td>Used when template-level image generation or processing is required.</td></tr><tr><td data-col-size="md" data-end="2003" data-start="1966">`theme_asset(path, secure = null)`</td><td>Generates the URL for an asset within the current theme.</td><td>Useful for referencing theme CSS, JS, images, fonts, etc.</td></tr><tr><td data-col-size="md" data-end="2084" data-start="2064">`theme_mix(path)`</td><td>Generates the URL for a theme asset processed through Laravel Mix.</td><td>Used for versioned/compiled theme assets.</td></tr><tr><td data-col-size="md" data-end="2204" data-start="2155">`theme_vite(entrypoints, directory = 'build')`</td><td>Generates Vite asset tags/URLs for entry points within the current theme.</td><td>Used to load Vite-built theme assets.</td></tr><tr><td data-col-size="md" data-end="2322" data-start="2279">`vite(entrypoints, directory = 'build')`</td><td>Generates Vite asset tags/URLs for the supplied entry points.</td><td>Used to load application-level Vite assets.</td></tr><tr><td data-col-size="md" data-end="2405" data-start="2385">`theme_css(path)`</td><td>Generates a stylesheet reference for a CSS file within the current theme.</td><td>Handles theme CSS and can render it inline where appropriate.</td></tr><tr><td data-col-size="md" data-end="2509" data-start="2487">`view_exists(view)`</td><td>Checks whether a specified view exists.</td><td>Useful for conditionally rendering optional views.</td></tr><tr><td data-col-size="md" data-end="2572" data-start="2555">`setting(...)`</td><td>Retrieves an application/store setting using its key.</td><td>Provides access to configured settings from Twig.</td></tr><tr><td data-col-size="md" data-end="2706" data-start="2692">`cart(...)`</td><td>Retrieves the current cart from the request.</td><td>Used to access cart information in storefront templates.</td></tr><tr><td data-col-size="md" data-end="2818" data-start="2790">`currency(amount = null)`</td><td>Returns the current currency, or formats a supplied amount using the current currency.</td><td>Useful for displaying correctly formatted prices.</td></tr><tr><td data-col-size="md" data-end="2957" data-start="2938">`css(path, url)`</td><td>Generates CSS output for a stylesheet, either as an inline `<style>` block or a `<link>` element depending on the context.</td><td>Used to include CSS dynamically in templates.</td></tr><tr><td data-col-size="md" data-end="3082" data-start="3047">`section(name, properties = [])`</td><td>Renders a named view from the `sections` view directory and passes it the supplied properties.</td><td>Useful for reusable page sections.</td></tr><tr><td data-col-size="md" data-end="3195" data-start="3160">`snippet(name, properties = [])`</td><td>Renders a named view from the `snippets` view directory and passes it the supplied properties.</td><td>Useful for small, reusable pieces of template markup.</td></tr><tr><td data-col-size="md" data-end="3305" data-start="3273">`form(name, properties = [])`</td><td>Renders a named form view and processes the resulting template through Twig.</td><td>Used to render reusable forms from Twig. Renders `forms.{name}` view.</td></tr><tr><td data-col-size="md" data-end="3415" data-start="3395">`has_route(name)`</td><td>Checks whether a named Laravel route exists.</td><td>Useful when conditionally generating links or navigation based on route availability.</td></tr></tbody></table>

# How do I add a custom twig function?

You can use the `add()` method to add a custom twig function:

```php
\Aero\Store\Twig\Extensions\TwigFunctions::add([
    new \Twig\TwigFunction('foo', function () {
        return 'bar';
    }),
]);
```

And then use it in twig:

```twig
{{ foo() }}
```

# What are twig functions?

Twig functions are reusable functions that can be called from within Twig templates to perform a specific task or return a value.

They are typically used to keep template logic simple and readable by moving more complex or reusable operations into functions. A Twig function can accept parameters, perform some processing, and return a result that can then be used in the template.

For example, you can use `{{ store_name() }}` in `twig` to output your store's name.

<p class="callout info">See [here](https://support.aerocommerce.com/books/storefront/page/what-are-the-available-twig-functions) for a list of all available twig functions, and [here](https://support.aerocommerce.com/books/storefront/page/how-do-i-add-a-custom-twig-function) for how to add your own.</p>