thelia / twig-module
Requires
- thelia/installer: ~1.1
- twig/twig: ~1.0
This package is auto-updated.
Last update: 2024-07-14 22:00:46 UTC
README
此模块使用 Twig 模板引擎作为 Thelia 的解析器,并替代了 Smarty。
此模块尚不稳定,仍在开发中。如需了解哪些功能缺失,请参阅路线图。
### 摘要
安装
您只能使用 composer 安装此模块
$ composer require thelia/twig-module:dev-master
激活
需要使用 CLI 工具启用此模块,然后禁用 TheliaSmarty 模块
$ php Thelia module:refresh
$ php Thelia module:activate TheliaTwig
$ php Thelia module:deactivate TheliaSmarty
使用
模板文件必须以 .twig
结尾,例如 index.html.twig
模板结构与实际结构相同,因此您可以参考实际的 文档
您可以使用此模块测试模块: https://github.com/bibich/TheliaTwigTest
语法
循环
循环功能是一个 Twig 标签,您必须像使用块一样使用它。所有循环的参数都使用 字面量 语法,与实际参数相同。标签以 loop
开头,以 endloop
结尾
示例 :
<ul>
{% loop {type:"category", name:"cat", limit:"2"} %}
<li>{{ ID }} : {{ TITLE }}
<ul>
{% loop {type:"product", name:"prod", category: ID} %}
<li>Title : {{ TITLE }} </li>
{% endloop %}
</ul>
</li>
{% endloop %}
</ul>
条件循环
条件循环已实现。对于 Smarty,ifloop
可以包裹 loop
并在相关循环之后使用。elseloop
必须在相关 loop
之后使用
{% ifloop {rel:"cat"} %}
<p>Before categories</p>
<ul>
{% loop {type:"category", name:"cat", limit:"2"} %}
<li>{{ ID }} : {{ TITLE }}
<ul>
{% loop {type:"product", name:"prod", category: ID} %}
<li>Title : {{ TITLE }} </li>
{% endloop %}
</ul>
</li>
{% endloop %}
</ul>
<p>After categories</p>
{% endifloop %}
{% elseloop {rel:"cat"} %}
<p>there is no category</p>
{% endelseloop %}
分页循环
分页循环的工作方式与 Smarty 的分页循环完全相同,只是语法有所变化。有关所有参数,请参阅官方文档: http://doc.thelia.net/en/documentation/loop/index.html#page-loop
语法示例
<p>Products Loop</p>
<ul>
{% loop {type:"product", name:"pagination", limit:"5", page:"3"} %}
<li>{{ TITLE }}</li>
{% endloop %}
</ul>
<p>Pagination</p>
<ul>
{% pageloop {rel: "pagination"} %}
<li>{{ PAGE }} {% if CURRENT == PAGE %} current {% endif %} / last : {{ END }}</li>
{% endpageloop %}
</ul>
URL 管理
url
url 是一个函数。它为给定的路径或文件生成一个绝对 URL。
url($path, $parameters = array(), $current = false, $file = null, $noAmp = false, $target = null)
参数
完整示例
<p>
<a href="{{ url("/product/", {id: 2, arg1: "val1"}) }}">my link</a>
</p>
生成的链接: http://domain.tld?id=2&arg1=val1
url_token
与 url
函数相同。此函数仅向 URL 添加一个令牌参数,以防止 CSRF 安全问题。
示例 :
<a href="{{ url_token("/product/", {id: 2, arg1: "val1"}) }}">my tokenized link</a>
生成的链接: http://domain.tld?id=2&arg1=val1&_token=UniqueToken
current_url
返回当前 URL
current_url()
示例 :
<a href="{{ current_url() }}">current link</a>
previous_url
返回保存在会话中的上一个 URL
previous_url
示例 :
<a href="{{ previous_url() }}">previous link</a>
index_url
返回主页 URL
index_url()
示例 :
<a href="{{ index_url() }}">index link</a>
翻译
default_domain
default_domain 是用于定义默认翻译域的标签。如果已定义,则当您想要在当前模板中翻译字符串时,无需指定它。
使用 :
{% default_domain "fo.default" %}
default_locale
用于定义区域设置的标签,不使用会话中存储的区域设置。
使用 :
{% default_locale "fr_FR" %}
intl
用于字符串翻译的函数
intl($id, $parameters = [], $domain = null, $locale = null)
参数
完整示例 :
{% default_domain "fo.default" %}
{% default_locale "fr_FR" %}
<p>
translation : {{ intl('Secure payment', [], null, 'en_US') }} <br>
translation 2 : {{ intl('Secure payment') }} <br>
translation 3 : {{ intl('Sorry, an error occurred: %error', {'%error': 'foo'}, 'front') }} <br>
</p>
安全
auth
标签检查用户是否拥有授权访问。
示例
{% auth {role: "CUSTOMER", login_tpl:"login"} %}
参数
check_cart_not_empty
此标签检查顾客的购物车是否为空,如果为空则重定向到“cart.view”路由。
{% check_cart_not_empty %}
check_valid_delivery
检查配送模块和地址是否有效,如果不有效则重定向到“order.delivery”路由。
{% check_valid_delivery %}
数据访问函数
所有数据访问函数允许访问实体的一个特定属性。其中一些通过查询字符串中的id访问,另一些通过会话中保存的数据访问。
admin
通过访问器提供对当前登录管理员的属性访问。
<p>
admin firstname : {{ admin('firstname') }}
</p>
brand
提供对当前品牌属性(查询字符串中的brand_id参数)的访问。如果查询字符串中包含product_id,品牌函数将找到与产品关联的品牌。
<p>
brand title : {{ brand('title') }}
</p>
cart
实现参数列表
- count_product : 当前购物车中产品数量
- count_item : 每个产品的数量之和
- total_price : 总金额(不含税)
- total_taxed_price : 含税总金额
- total_taxed_price_without_discount : 含税总金额(不含折扣)
- is_virtual : 购物车是否包含虚拟产品
- total_vat : 税额
示例
<p>
number of products : {{ cart('count_product') }}
</p>
category
提供对当前类别属性(查询字符串中的category_id参数)的访问。如果查询字符串中包含product_id,则使用与此产品关联的默认类别。
<p>
Category title : {{ category('title') }}
</p>
config
返回所需配置键的值。如果键不存在,则使用第二个参数作为默认值。
<p>
default front office template : {{ config('active-front-template', 'default') }}
</p>
content
提供对当前内容属性(查询字符串中的content_id)的访问。
<p>
content title : {{ content('title') }}
</p>
country
提供对默认国家属性的访问。
<p>
iso alpha2 code : {{ country('isoalpha2') }}
</p>
currency
提供对当前货币属性(保存在会话中)的访问。
<p>
currency symbol : {{ currency('symbol') }}
</p>
customer
提供对当前登录顾客属性的访问。
<p>
customer first name : {{ customer('firstname') }}
</p>
folder
提供对当前文件夹属性(查询字符串中的folder_id)的访问。如果查询字符串中包含content_id参数,则使用默认关联文件夹。
<p>
folder title : {{ folder('title') }}
</p>
lang
提供对当前语言属性(保存在会话中)的访问。
<p>
locale : {{ lang('locale') }}
</p>
order
提供对当前订单属性的访问。
实现参数列表
- untaxed_postage : 不含税的邮费
- postage : 邮费
- postage_tax : 邮费税额
- discount : 折扣金额
- delivery_address : 配送地址ID
- invoice_address : 发票地址ID
- delivery_module : 配送模块ID
- payment_module : 支付模块ID
- has_virtual_product : 订单是否包含至少一个虚拟产品
示例
<p>
discount amount : {{ order('discount') }}
</p>
product
提供对当前产品属性(查询字符串中的product_id参数)的访问。
<p>
product title : {{ product('title') }}
</p>
购物车运费
检索当前购物车中存在的邮费金额。
Thelia使用以下规则来选择国家
- 如果存在,则使用与购物车相关的顾客的配送地址的国家
- 如果顾客已更改默认国家,则使用保存在cookie中的国家
- 如果存在,则使用商店的默认国家
在postage
块中定义了以下变量
- country_id: 国家ID或null
- delivery_id: 配送ID或null
- postage: 邮费金额或0.0
- is_customizable: 指示邮费是否可以自定义。当顾客已登录并且有一个有效的配送地址时为False
{% postage %}
postage : {{ postage ~ currency('symbol') }}
{% endpostage %}
格式化函数
format_date
以期望的格式返回日期
可用参数
- params => Array
- date :
DateTime
对象(如果不存在时间戳则必须提供) - timestamp : Unix时间戳(如果不存在日期则必须提供)
- format => 输出具有特定格式的格式(参见date()函数)
- output => 默认系统格式的列表。可用值
- date => 日期格式
- time => 时间格式
- datetime => 日期时间格式(默认)
- locale => 根据特定区域设置(例如fr_FR)格式化日期
- date :
{% 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
以期望的格式返回数字
可用参数
- params => Array
- 数字 => 整数或浮点数(必填)
- decimals => 预期的小数位数格式
- dec_point => 小数点的分隔符
- thousands_sep => 千位分隔符
{# 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
以预期格式返回货币
可用参数
- params => Array
- 数字 => 整数或浮点数(必填)
- decimals => 预期的小数位数格式
- dec_point => 小数点的分隔符
- thousands_sep => 千位分隔符
- symbol => 货币符号
{# will output "1 246,1 €" #} {{ format_number({number:"1246.12", decimals:"1", dec_point:",", thousands_sep:" ", symbol:"€"}) }}
闪现消息
has_flash
测试是否存在给定类型的消息。
可用参数
- type (必填)
{% if has_flash('test') %} {# do something #} {% endif %}
flash
获取所有消息或给定类型的消息。函数调用后,闪现消息将被删除。
可用参数
- type : 指定类型(字符串或null)
- 如果提供,获取给定类型的所有消息并返回消息数组
- 如果不提供,获取所有闪现消息。它将返回一个数组。键将是类型,值是与关联消息的数组
{% if has_flash('notice') %} <div class="alert alert-notice"> {% for message in flash('notice') %} {{ message }}<br> {% endfor %} </div> {% endif %} {% for type, messages in flash() %} <div class="alert alert-{{ type }}"> {% for message in messages %} {{ message }}<br> {% endfor %} </div> {% endfor %}
钩子
hook
标签
标签 hook
允许您获取与通过其名称指定的特定钩子相关的内容。
可用参数
- params => Array
- name => 钩子名称(必填)
- ... 您可以添加任意多的参数。它们将在钩子事件中可用
{% hook {name: "hook_code", var1: "value1", var2: "value2", ... } %}
hookblock
和 forhook
标签
标签 hookblock
允许您获取与通过其名称指定的特定钩子相关的内容。内容不是直接注入,而必须通过 forhook
标签进行处理。
可用参数
- params => Array
- name => 钩子名称(必填)
- fields => 指示可以添加到
hookblock
事件中的字段 - ... 您可以添加任意多的参数。它们将在钩子事件中可用
标签 forhook
在 hookblock
标签的结果上迭代。您应设置 rel
属性以建立链接。 您可以使用 forhook
多次。
{% hookblock {name: "hookblock_code", fields: "id,title, content", var1: "value1", ... } %} {% forhook {rel: 'hookblock_code'} %} <div id="{{ id }}"> <h2>{{ title }}</h2> <p>{{ content|raw }}</p> </div> {% endforhook %}
ifhook
和 elsehook
标签
这些标签将测试 hook
或 hookblock
是否为空或不为空。
{% ifhook {rel:"main.content-bottom"} %} {# displayed if main.content-bottom is not empty #} <hr class="space"> {% hook {name: "main.content-bottom"} %} <hr class="space"> {% endifhook %} {% elsehook {rel:"main.content-bottom"} %} {# displayed if main.content-bottom is empty #} <p><a href="#top">Back to top</a></p> {% endelsehook %}
如何添加自己的扩展
标签 thelia.parser.add_extension
允许您添加自己的twig扩展。
示例 :
<service id="thelia.parser.loop_extension" class="TheliaTwig\Template\Extension\Loop">
<argument type="service" id="thelia.parser.loop_handler" />
<tag name="thelia.parser.add_extension" />
</service>
路线图
循环条件循环分页循环- Assetic集成
I18n支持- 表单支持
URL 管理url函数token_url函数navigate函数set_previous_url函数
- 钩子支持
日期和货币格式闪存消息购物车邮费DataAccessFunctionadminbrandcartcategoryconfigcontentcountrycurrencycustomerfolderlangorderproduct
安全checkAuthcheckCartNotEmptycheckValidDelivery