nedryse/latte-template-macroset (cc)
Pavel Železný (2bfree), 2014 (pavelzelezny.cz), (*1)
Requirements
Nette Framework 2.2.0 or higher, (*2)
Documentation
Placeholder replacement Latte macro provides simple way to put the content of variables into the right places., (*3)
Macroset provides {template ...}
macro and overwrites {_ ...}
macro with same mechanism.
First argument of these macros can contain any number of {:placeholders}
in the string that will be replaced by the same named variables., (*4)
Replacement variables are taken from the template like $template->placeholder
, are overwritten by local variable like {var $placeholder}
and are overwritten by arguments of macros like {template ..., 'placeholder' => 'replacement', ...}
or {template ..., array('placeholder' => 'replacement', ...)}
.
Replacement variables can contain {:nested_placeholders}
, that will be replaced also., (*5)
The functionality of {_ ...}
macro is maintained. All arguments of the macro are sended to the translator unchanged. Result of the translator will be used as template that can contain {:placeholders}
. With this functionality you can change the possition of given arguments to provide propper number of translator plurals as the first argument of the macro., (*6)
Instalation
Prefered way to intall is by Composer, (*7)
composer require nedryse/latte-template-macroset:~1.0.0
Or by manualy adding into the composer.json, (*8)
{
"require":{
"nedryse/latte-template-macroset": "~1.0.0"
}
}
Setup
Add following code into the config.neon, (*9)
common:
extensions:
latteTemplateMacroSet: Nedryse\Latte\Macros\TemplateMacroSetExtension
Usage
The simpliest usage is like the following code:, (*10)
{template '<strong>{:userName}</strong>', 'userName' => $user->getIdentity()->getId()}
Replacement variables can be given by the array also like in the following code:, (*11)
{var $replacements = array('userName' => $user->getIdentity()->getId())}
{template '<strong>{:userName}</strong>', $replacements}
Replacement variables can be given from {var ...}
or from $template
like in the following code:, (*12)
{var $userName => $user->getIdentity()->getId()}
{template '<strong>{:userName}</strong>'}
Replacements can be overwritten in the order $template
, {var ...}
, {template ..., replacements}
like in the following code:, (*13)
{var $userName => $user->getIdentity()->getId()}
{template '<strong>{:userName}</strong>', 'userName' => 'test'}
Both macros also support filters like in the following code:, (*14)
{template '<strong>{:userName}</strong>', 'userName' => 'test'|upper}
Replacements can contain {:nested_placeholders}
to be used as customizable template mechanism like in the following code:, (*15)
{var $idTpl = '<strong>{:userId}</strong>'}
{var $nameTpl = '{:userName}'}
{var $pairTpl = '<li>{:idTpl} - {:nameTpl}</li>'}
{var $usersTpl = '<ul>{:usersListTpl}</ul>'}
{capture $usersListTpl}
{foreach $users as $userId => $userName}
{template $pairTpl}
{/foreach}
{/capture}
{_'User (%s): {:usersTpl}', count($users)}