Download the PHP package thelia/twig-module without Composer
On this page you can find all versions of the php package thelia/twig-module. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package twig-module
Twig integration for Thelia
This module use Twig 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
- Activation
- Usage
- Syntax
- Loop
- Conditional loop
- Paginated loop
- URL management
- Translation
- Security
- Data access functions
- Cart postage
- Format functions
- Flash messages
- Hooks
- Add your own twig extension
- Roadmap
Installation
You can only install this module with composer :
Activation
It is required to enable this module with the cli tools and then disable TheliaSmarty module :
Usage
Template files must be suffixed by , for example
The template structure is the same as the actual structure, so you can referer to the actual documentation
You can test the module with this module : https://github.com/bibich/TheliaTwigTest
Syntax
Loop
loop feature is a Twig tag, you have to use it like a block. All loop's parameters use literals syntax and are the same as the acutal parameters. The tag start with and finish with
example :
Conditional loop
Conditional loops are implemented. As for Smarty a can wrap a and can be used after the related loop. must be used after the related
Paginated loop
Paginated loop works exactly like paginated loop for Smarty, just the syntax change. See the official documentation for all parameters : http://doc.thelia.net/en/documentation/loop/index.html#page-loop
Syntax example :
Url management
url
url is a function. It generates an absolute url for a given path or file.
parameters :
Parameters | Description | example |
---|---|---|
path | The value of the path parameter is the route path you want to get as an URL | |
file | The 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 | |
parameters | paremeters added to the query string | |
current | generate absolute URL grom the current URL | |
noAmp | escape all as that may be present in the generated URL. | |
target | Add an anchor to the URL |
Complete example :
generated link : http://domain.tld?id=2&arg1=val1
url_token
same as function. This function just add a token paremeter in the url to prevent CSRF security issue.
example :
generated link : http://domain.tld?id=2&arg1=val1&_token=UniqueToken
current_url
return the current url
example :
previous_url
return the previous url saved in session
example :
index_url
return the homepage url
example :
Translation
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_locale
tag for defining a locale and don't use the locale stored in session.
Usage :
intl
function for string translation
parameters :
Parameters | Description | Example |
---|---|---|
id | the string to translate | |
parameters | variable use if a placeholder is used in the string to translate | => secure payment atos |
domain | message domain, will override domain defined with tag | |
locale | specific locale to use for this translation. Will override locale defined with tag and the locale defined in session |
Complete example :
Security
auth
tag checking if a user has access granted.
example :
Parameters :
Parameters | Description |
---|---|
role | In Thelia 2, a user can only have one of these two roles: ADMIN and/or CUSTOMER |
resource | if a user can access to a specific resource. See : http://doc.thelia.net/en/documentation/templates/security.html#resource |
module | Name of the module(s) which the user must have access |
access | access mode : CREATE, VIEW, UPDATE, DELETE |
login_tpl | This 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
This tag checks if the customer's cart is empty, and redirects to the route "cart.view" if it is.
check_valid_delivery
Check if the delivery module and address are valid, redirects to the route "order.delivery" if not.
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
Provides access to the current logged administrator attributes using the accessors.
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.
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 :
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.
config
return the value of a wanted configuration key. Default as second parameter if the key does not exists.
content
Provides access to an attribute of the current content (content_id in the query string).
country
Provides access to an attribute for the default country
currency
Provides access to an attribute of the current currency (saved in session)
customer
Provides access to an attribute of the logged customer
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.
lang
Provides access to an attribute of the current lang saved in session
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 :
product
Provides access to an attribute of the current product (product_id parameter in query string)
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 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
format functions
format_date
return date in expected format
available parameters :
- params => Array :
- date :
DateTime
object (mandatory if timestamp is not present) - timestamp : a Unix timestamp (mandatory if date is not present)
- format => will output the format with specific format (see date() function)
- output => list of default system format. Values available :
- date => date format
- time => time format
- datetime => datetime format (default)
- locale => format the date according to a specific locale (eg. fr_FR)
- date :
format_number
return numbers in expected format
available parameters :
- params => Array :
- number => int or float number (mandatory)
- decimals => how many decimals format expected
- dec_point => separator for the decimal point
- thousands_sep => thousands separator
format_money
return money in expected format
available parameters :
- params => Array :
- number => int or float number (mandatory)
- decimals => how many decimals format expected
- dec_point => separator for the decimal point
- thousands_sep => thousands separator
- symbol => Currency symbol
flash messages
has_flash
Test if message exists for the given type.
available parameters :
- type (mandatory)
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
Hooks
hook
tags
The tag allows you to get the content related to a specific hook specified by its name.
available parameters :
- params => Array :
- name => the hook name (mandatory)
- ... You can add as many parameters as you want. they will be available in the hook event
hookblock
and forhook
tags
The tag 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 => Array :
- name => the hook name (mandatory)
- fields => 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 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.
ifhook
and elsehook
tags
These tags will test if hook
or hookblock
are empty or not.
How to add your own extension
The tag allows you to add your own twig extension.
example :
Roadmap
loopconditional looppaginated loop- Assetic integration
I18n support- Form support
URL managementurl functiontoken_url functionnavigate functionset_previous_url function
- Hook support
date and money formatFlash messagescart postageDataAccessFunctionadminbrandcartcategoryconfigcontentcountrycurrencycustomerfolderlangorderproduct
SecuritycheckAuthcheckCartNotEmptycheckValidDelivery