Contributte Latte
Extra contribution to nette/latte
(opens new window).
# Content
- Setup
- VersionExtension - revision macros for assets
- FiltersExtension - install filters easily
- RuntimeFilters - collection of prepared filters
- Formatters - collection of prepared formatters
# Setup
composer require contributte/latte
1
# Version(s) Extension
This extension adds 3 macros: {rev}
, {build}
, {v}
.
# Install
extensions:
version: Contributte\Latte\DI\VersionExtension
1
2
2
# Configuration
version:
rev: e3203c85a9b84ee866132f371ba0b238b6a5b245
build: 2016
v: 2.12
1
2
3
4
2
3
4
Alternatively, you can use the generated
property. It generates rev/build/v for each container compilation, i.e. each time in the debug mode and once in the production mode. Very useful.
version:
generated: %debugMode%
# rev: auto-generated
# build: auto-generated
# v: auto-generated
1
2
3
4
5
2
3
4
5
<link rel="stylesheet" href="{$basePath}/assets/theme.css?v={rev}">
<link rel="stylesheet" href="{$basePath}/assets/theme.css?v={build}">
<link rel="stylesheet" href="{$basePath}/assets/theme.css?v={v}">
1
2
3
2
3
# Filters Extension
Install filters by single extension and simple FiltersProvider
implementation.
# Installation
extensions:
filters: Contributte\Latte\DI\FiltersExtension
1
2
2
# Usage
First of all, you have to define your own filters provider. It's key => value
, that means name => callback
array.
use Contributte\Latte\Filters\FiltersProvider;
final class MyFilters implements FiltersProvider
{
/**
* @return callable[]
*/
public function getFilters(): array
{
return [
'say' => function ($hi) {
return sprintf('Hi %s!', $hi);
},
];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
After that, add you filters provider as a service to your config (neon) file.
services:
- MyFilters
1
2
2
That's all.
# RuntimeFilters
# Filectime
<link rel="stylesheet" href="{=$basePath/assets/theme.css|filectime}">
1
<link rel="stylesheet" href=/assets/theme.css?v=123456789">
1
# Email
{var $email = "my@email.net"}
{$email|email:"javascript"}
{$email|email:"javascript_charcode"}
{$email|email:"hex"|noescape}
{$email|email:"javascript":"link to my email here"}
{$email|email:"drupal"}
{$email|email:"texy"}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
This is my email: <a href="mailto: my[at]email.org">my[at]email.org</a>
1
# Supported encoding methods
- javascript
- javascript_charcode
- hex
- drupal
- texy
# Nette DI setup
services:
nette.latteFactory:
setup:
- addFilter('email', ['Contributte\Latte\Filters\EmailFilter', 'filter'])
1
2
3
4
2
3
4
# Gravatar
Create link to gravatar image (opens new window)
<img src="{lorem@ipsum.com|gravatar}"/>
<img src="https://www.gravatar.com/avatar/067398c3f23785981cd8672e21643405.jpg?default=retro&size=80"/>
1
2
2
<img src="{lorem@ipsum.com|gravatar:[size=>48, style=>robohash, format=>png]}"/>
<img src="https://www.gravatar.com/avatar/067398c3f23785981cd8672e21643405.png?default=robohash&size=48"/>
1
2
2
# Formatters
# NumberFormatter
Number formatter is simple wrapper class over the number_format
function.
services:
formatter.money:
# with defined prefix
class: Contributte\Latte\Formatters\NumberFormatter('Kč')
formatter.weight:
# with defined prefix and suffix
class: Contributte\Latte\Formatters\NumberFormatter('kg', '~')
setup:
- setThousands('')
- setDecimals(0)
nette.latteFactory:
setup:
# used as latte filter
- addFilter(money, [@formatter.number, format])
- addFilter(weight, [@formatter.weight, format])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Methods:
setDecimals(int $decimals)
setPoint(string $separator)
setThousands(string $separator)
setZeros(bool $display)
setSuffix(string $suffix)
setPrefix(string $prefix)
setString(bool $throw)
setSpaces(bool $display)
setCallback(callable $callback)