PHPackages                             thelia/twig-module - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. thelia/twig-module

AbandonedArchivedThelia-module[Templating &amp; Views](/categories/templating)

thelia/twig-module
==================

0.1(10y ago)2663[2 PRs](https://github.com/thelia-modules/TheliaTwig/pulls)LGPL-3.0+PHP

Since Jun 4Pushed 2y ago5 watchersCompare

[ Source](https://github.com/thelia-modules/TheliaTwig)[ Packagist](https://packagist.org/packages/thelia/twig-module)[ RSS](/packages/thelia-twig-module/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Twig integration for Thelia
---------------------------

[](#twig-integration-for-thelia)

This module use [Twig](http://twig.sensiolabs.org) template engine as parser for Thelia and replace Smarty.

**This module is not stable and is still in development. See the RoadMap if you want to know which features are missing**

\###Summary :

- [Installation](#installation)
- [Activation](#activation)
- [Usage](#usage)
- [Syntax](#syntax)
    - [Loop](#loop)
    - [Conditional loop](#conditional-loop)
    - [Paginated loop](#paginated-loop)
    - [URL management](#url-management)
    - [Translation](#translation)
    - [Security](#security)
    - [Data access functions](#data-access-functions)
    - [Cart postage](#cart-postage)
    - [Format functions](#format-functions)
    - [Flash messages](#flash-messages)
    - [Hooks](#hooks)
- [Add your own twig extension](#how-to-add-your-own-extension)
- [Roadmap](#roadmap)

### Installation

[](#installation)

You can only install this module with composer :

```
$ composer require thelia/twig-module:dev-master

```

### Activation

[](#activation)

It is required to enable this module with the cli tools and then disable TheliaSmarty module :

```
$ php Thelia module:refresh
$ php Thelia module:activate TheliaTwig
$ php Thelia module:deactivate TheliaSmarty

```

### Usage

[](#usage)

Template files must be suffixed by `.twig`, for example `index.html.twig`

The template structure is the same as the actual structure, so you can referer to the actual [documentation](http://doc.thelia.net/en/documentation/templates/introduction.html#structure-of-a-template)

You can test the module with this module :

### Syntax

[](#syntax)

#### Loop

[](#loop)

loop feature is a Twig tag, you have to use it like a block. All loop's parameters use [literals](http://twig.sensiolabs.org/doc/templates.html#literals) syntax and are the same as the acutal parameters. The tag start with `loop` and finish with `endloop`

**example** :

```

{% loop {type:"category", name:"cat", limit:"2"} %}
    {{ ID }} : {{ TITLE }}

    {% loop {type:"product", name:"prod", category: ID} %}
        Title : {{ TITLE }}
    {% endloop %}

{% endloop %}

```

#### Conditional loop

[](#conditional-loop)

Conditional loops are implemented. As for Smarty a `ifloop` can wrap a `loop` and can be used after the related loop. `elseloop` must be used after the related `loop`

```
{% ifloop {rel:"cat"} %}
    Before categories

    {% loop {type:"category", name:"cat", limit:"2"} %}
        {{ ID }} : {{ TITLE }}

        {% loop {type:"product", name:"prod", category: ID} %}
            Title : {{ TITLE }}
        {% endloop %}

    {% endloop %}

After categories
{% endifloop %}
{% elseloop {rel:"cat"} %}
    there is no category
{% endelseloop %}

```

#### Paginated loop

[](#paginated-loop)

Paginated loop works exactly like paginated loop for Smarty, just the syntax change. See the official documentation for all parameters :

Syntax example :

```
Products Loop

{% loop {type:"product", name:"pagination", limit:"5", page:"3"} %}
    {{ TITLE }}
{% endloop %}

Pagination

{% pageloop {rel: "pagination"} %}
    {{ PAGE }} {% if CURRENT == PAGE %} current {% endif %} / last : {{ END }}
{% endpageloop %}

```

### Url management

[](#url-management)

#### url

[](#url)

url is a function. It generates an absolute url for a given path or file.

```
url($path, $parameters = array(), $current = false, $file = null, $noAmp = false, $target = null)

```

parameters :

ParametersDescriptionexamplepathThe value of the path parameter is the route path you want to get as an URL`url("/product/")`fileThe value of the file parameter is the absolute path (from /web) of a real file, that will be served by your web server, and not processed by Thelia`url(null,[], false, "file.pdf")`parametersparemeters added to the query string`url("/product" ,{arg1:"val1", arg2:"val2"})`currentgenerate absolute URL grom the current URL`url(null ,[], true)`noAmpescape all `&` as `&amp;` that may be present in the generated URL.`url("/product" ,[], false, null, true)`targetAdd an anchor to the URL`url("/product" ,[], false, null, false, "cart")`Complete example :

```

    my link

```

generated link : [http://domain.tld?id=2&amp;arg1=val1](http://domain.tld?id=2&arg1=val1)

#### url\_token

[](#url_token)

same as `url` function. This function just add a token paremeter in the url to prevent CSRF security issue.

**example** :

```
my tokenized link

```

generated link : [http://domain.tld?id=2&amp;arg1=val1&amp;\_token=UniqueToken](http://domain.tld?id=2&arg1=val1&_token=UniqueToken)

#### current\_url

[](#current_url)

return the current url

```
current_url()

```

**example** :

```
current link

```

#### previous\_url

[](#previous_url)

return the previous url saved in session

```
previous_url

```

**example** :

```
previous link

```

#### index\_url

[](#index_url)

return the homepage url

```
index_url()

```

**example** :

```
index link

```

### Translation

[](#translation)

#### default\_domain

[](#default_domain)

default\_domain is a tag for defining the default translation domain. If defined you don't need to specify it when you want to translation a string in the current template.

**Usage** :

```
{% default_domain "fo.default" %}

```

#### default\_locale

[](#default_locale)

tag for defining a locale and don't use the locale stored in session.

**Usage** :

```
{% default_locale "fr_FR" %}

```

#### intl

[](#intl)

function for string translation

```
intl($id, $parameters = [], $domain = null, $locale = null)

```

parameters :

ParametersDescriptionExampleidthe string to translate`intl('secure payment')`parametersvariable use if a placeholder is used in the string to translate`intl('secure payment %payment', {'%payment' => 'atos'})` =&gt; secure payment atosdomainmessage domain, will override domain defined with tag `default_domain``{{ intl('secure payment', [], 'front') }}`localespecific locale to use for this translation. Will override locale defined with tag `default_locale` and the locale defined in session`{{ intl('Secure payment', [], null, 'en_US') }}`**Complete example** :

```
{% default_domain "fo.default" %}
{% default_locale "fr_FR" %}

    translation : {{ intl('Secure payment', [], null, 'en_US') }}
    translation 2 : {{ intl('Secure payment') }}
    translation 3 : {{ intl('Sorry, an error occurred: %error', {'%error': 'foo'}, 'front') }}

```

### Security

[](#security)

#### auth

[](#auth)

tag checking if a user has access granted.

example :

```
{% auth {role: "CUSTOMER", login_tpl:"login"} %}

```

Parameters :

ParametersDescriptionroleIn Thelia 2, a user can only have one of these two roles: ADMIN and/or CUSTOMERresourceif a user can access to a specific resource. See : moduleName of the module(s) which the user must have accessaccessaccess mode : CREATE, VIEW, UPDATE, DELETElogin\_tplThis argument is the name of the view name (the login page is "login"). If the user is not granted and this argument is defined, it redirects to this view.#### check\_cart\_not\_empty

[](#check_cart_not_empty)

This tag checks if the customer's cart is empty, and redirects to the route "cart.view" if it is.

```
{% check_cart_not_empty %}

```

#### check\_valid\_delivery

[](#check_valid_delivery)

Check if the delivery module and address are valid, redirects to the route "order.delivery" if not.

```
{% check_valid_delivery %}

```

### data access functions

[](#data-access-functions)

All data access function allow to access to a specific property of an entity. For some of them through an id present in the query string, for others through data saved in session

#### admin

[](#admin)

Provides access to the current logged administrator attributes using the accessors.

```

    admin firstname : {{ admin('firstname') }}

```

#### brand

[](#brand)

Provides access to an attribute of the current brand (brand\_id parameter in the query string). If the product\_id is in the query string, the brand function will find the brand linked to the product.

```

    brand title : {{ brand('title') }}

```

#### cart

[](#cart)

list of implemented parameters :

- count\_product : number of products in the current cart
- count\_item : addition off all quantity for each products
- total\_price : total amount without taxes
- total\_taxed\_price : total amount with taxes
- total\_taxed\_price\_without\_discount : total amount with taxes and without discount
- is\_virtual : if cart contains or not virutal products
- total\_vat : tax amount

example :

```

    number of products : {{ cart('count_product') }}

```

#### category

[](#category)

Provides access to an attribute of the current category (category\_id parameter is the query string). If the product\_id is in the query string, the default category linked to this product is used.

```

    Category title : {{ category('title') }}

```

#### config

[](#config)

return the value of a wanted configuration key. Default as second parameter if the key does not exists.

```

    default front office template : {{ config('active-front-template', 'default') }}

```

#### content

[](#content)

Provides access to an attribute of the current content (content\_id in the query string).

```

    content title : {{ content('title') }}

```

#### country

[](#country)

Provides access to an attribute for the default country

```

    iso alpha2 code : {{ country('isoalpha2') }}

```

#### currency

[](#currency)

Provides access to an attribute of the current currency (saved in session)

```

    currency symbol : {{ currency('symbol') }}

```

#### customer

[](#customer)

Provides access to an attribute of the logged customer

```

    customer first name : {{ customer('firstname') }}

```

#### folder

[](#folder)

Provides access to an attribute of the current folder (folder\_id in the query string). If the content\_id parameter is in the query string, the default linked folder will be used.

```

   folder title : {{ folder('title') }}

```

#### lang

[](#lang)

Provides access to an attribute of the current lang saved in session

```

    locale : {{ lang('locale') }}

```

#### order

[](#order)

Provides access to an attribute of the current order

list of implemented parameters :

- untaxed\_postage : postage cost without taxes
- postage : postage cost
- postage\_tax : postage tax amount
- discount : discount amount
- delivery\_address : delivery address id
- invoice\_address : invoice address id
- delivery\_module : delivery module id
- payment\_module : payment module id
- has\_virtual\_product : if order contains at least one virtual product

example :

```

    discount amount : {{ order('discount') }}

```

#### product

[](#product)

Provides access to an attribute of the current product (product\_id parameter in query string)

```

    product title : {{ product('title') }}

```

### Cart postage

[](#cart-postage)

retrieves the postage amount of the current cart if it exists.

Thelia uses the following rules to select the country :

- the country of the delivery address of the customer related to the cart if it exists
- the country saved in cookie if customer have changed the default country
- the default country for the shop if it exists

Inside the `postage` block this variables are defined :

- country\_id: the country id or null
- delivery\_id: the delivery id or null
- postage: the postage amount or 0.0
- is\_customizable: indicate if the postage can be customized. False When customer is signed and have a valid delivery address

```
{% postage %}
    postage : {{ postage ~ currency('symbol') }}
{% endpostage %}

```

### format functions

[](#format-functions)

#### format\_date

[](#format_date)

return date in expected format

available parameters :

- params =&gt; Array :
    - date : `DateTime` object (mandatory if timestamp is not present)
    - timestamp : a Unix timestamp (mandatory if date is not present)
    - format =&gt; will output the format with specific format (see date() function)
    - output =&gt; list of default system format. Values available :
        - date =&gt; date format
        - time =&gt; time format
        - datetime =&gt; datetime format (default)
    - locale =&gt; format the date according to a specific locale (eg. fr\_FR)

```
{% set myDate = date() %}
{# format the date in datetime format for the current locale #}
{{ format_date({date: myDate}) }}
{# format the date in date format for the current locale #}
{{ format_date({date: myDate, output:"date"}) }}
{# format the date with a specific format (with the default locale on your system) #}
{{ format_date({date: myDate, format:"Y-m-d H:i:s"}) }}
{# format the date with a specific format with a specific locale #}
{{ format_date({date: myDate, format:"D l F j", locale:"en_US"}) }}
{{ format_date({date: myDate, format:"l F j", locale:"fr_FR"}) }}
{# using a timestamp instead of a date #}
{{ format_date({timestamp: myDate|date('U'), output:"datetime"}) }}
```

#### format\_number

[](#format_number)

return numbers in expected format

available parameters :

- params =&gt; Array :
    - number =&gt; int or float number (mandatory)
    - decimals =&gt; how many decimals format expected
    - dec\_point =&gt; separator for the decimal point
    - thousands\_sep =&gt; thousands separator

```
{# specific format #}
{{ format_number({number:"1246.12", decimals:"1", dec_point:",", thousands_sep:" "}) }}
{# format for the current locale #}
{{ format_number({number:"1246.12"}) }}
```

#### format\_money

[](#format_money)

return money in expected format

available parameters :

- params =&gt; Array :
    - number =&gt; int or float number (mandatory)
    - decimals =&gt; how many decimals format expected
    - dec\_point =&gt; separator for the decimal point
    - thousands\_sep =&gt; thousands separator
    - symbol =&gt; Currency symbol

```
{#  will output "1 246,1 €" #}
{{ format_number({number:"1246.12", decimals:"1", dec_point:",", thousands_sep:" ", symbol:"€"}) }}
```

### flash messages

[](#flash-messages)

#### has\_flash

[](#has_flash)

Test if message exists for the given type.

available parameters :

- type (mandatory)

```
{% if has_flash('test') %}
    {# do something #}
{% endif %}
```

#### flash

[](#flash)

Get all messages or messages for the given type. After the call of the function flash messages are deleted.

available parameter :

- type : a specific type (string or null)
    - if provided, get all messages for the given type and return an array of messages
    - if not provided, get all flash messages. It will return an array. The key will be the type and the value an array of associated messages

```
{% if has_flash('notice') %}

    {% for message in flash('notice') %}
        {{ message }}
    {%  endfor %}

{% endif %}

{% for type, messages in flash() %}

    {% for message in messages %}
        {{ message }}
    {%  endfor %}

{%  endfor %}
```

### Hooks

[](#hooks)

#### `hook` tags

[](#hook-tags)

The tag `hook` allows you to get the content related to a specific hook specified by its name.

available parameters :

- params =&gt; Array :
    - name =&gt; the hook name (mandatory)
    - ... You can add as many parameters as you want. they will be available in the hook event

```
{% hook {name: "hook_code", var1: "value1", var2: "value2", ... } %}
```

#### `hookblock` and `forhook` tags

[](#hookblock-and-forhook-tags)

The tag `hookblock` allows you to get the content related to a specific hook specified by its name. The content is not injected directly, but has to be manipulated by a `forhook` tag.

available parameters :

- params =&gt; Array :
    - name =&gt; the hook name (mandatory)
    - fields =&gt; indicates the fields that you can add to the `hookblock` event
    - ... You can add as many parameters as you want. they will be available in the hook event

The tag `forhook` iterates on the results of a `hookblock` tag. You should set the `rel` attribute to establish the link. *You can use the `forhook` multiple times*.

```
{% hookblock {name: "hookblock_code", fields: "id,title, content", var1: "value1", ... } %}
{% forhook {rel: 'hookblock_code'} %}

        {{ title }}
        {{ content|raw }}

{% endforhook %}
```

#### `ifhook` and `elsehook` tags

[](#ifhook-and-elsehook-tags)

These tags will test if `hook` or `hookblock` are empty or not.

```
{% ifhook {rel:"main.content-bottom"} %}
    {# displayed if main.content-bottom is not empty #}

    {% hook {name: "main.content-bottom"} %}

{% endifhook %}

{% elsehook {rel:"main.content-bottom"} %}
    {# displayed if main.content-bottom is empty #}
    Back to top
{% endelsehook %}
```

### How to add your own extension

[](#how-to-add-your-own-extension)

The tag `thelia.parser.add_extension` allows you to add your own twig extension.

**example** :

```

```

### Roadmap

[](#roadmap)

- loop
- conditional loop
- paginated loop
- Assetic integration
- I18n support
- Form support
- URL management
    - url function
    - token\_url function
    - navigate function
    - set\_previous\_url function
- Hook support
- date and money format
- Flash messages
- cart postage
- DataAccessFunction
    - admin
    - brand
    - cart
    - category
    - config
    - content
    - country
    - currency
    - customer
    - folder
    - lang
    - order
    - product
- Security
    - checkAuth
    - checkCartNotEmpty
    - checkValidDelivery

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4002d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2196919?v=4)[thelia](/maintainers/thelia)[@thelia](https://github.com/thelia)

---

Top Contributors

[![lunika](https://avatars.githubusercontent.com/u/767834?v=4)](https://github.com/lunika "lunika (21 commits)")[![bibich](https://avatars.githubusercontent.com/u/1215555?v=4)](https://github.com/bibich "bibich (1 commits)")

### Embed Badge

![Health badge](/badges/thelia-twig-module/health.svg)

```
[![Health](https://phpackages.com/badges/thelia-twig-module/health.svg)](https://phpackages.com/packages/thelia-twig-module)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91492.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36763.2M221](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

22046.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22918.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21914.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
