You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please notice that not all characters are supported in the default character map. It covers mainly
222
223
e-mail addresses and phone numbers. However, you can pass your own character set to the obfuscate methods
223
-
as third argument. Please consult the [source code](https://github.com/technicalguru/php-utils/blob/src/TgUtils/Obfuscation.php) for more details.
224
+
as third argument. Please consult the [source code](https://github.com/technicalguru/php-utils/blob/main/src/TgUtils/Obfuscation.php) for more details.
225
+
226
+
## Text Templating
227
+
228
+
To ease the generation of dynamic texts, a template processor is provided. This processor can work on texts that contain variables in
229
+
curly brackets `{{variable-definition}}`. The processor knows objects, snippets and formatters.
230
+
231
+
**Objects** are application objects that hold attributes that you want to be replaced. An object's attribute will be referenced in a template
232
+
with `{{objectKey.attributeName}}`, e.g. `{{user.name}}`.
233
+
234
+
**Snippets** are more complex replacements that will be inserted in your template. This is useful when you need the same complex
235
+
text structure in multiple template generations, e.g. for a footer or a header text. Snippets are references in a template by
236
+
their keys only: `{{snippetKey}}`. A snippet is implemented by the interface [Snippet](https://github.com/technicalguru/php-utils/blob/main/src/TgUtils/Templating/Snippet.php).
237
+
238
+
**Formatters** can be used to format an object's attribute. Formatters can take parameters to further customize the formatting. A good example
239
+
is the [`DateFormatter`](https://github.com/technicalguru/php-utils/blob/main/src/TgUtils/Templating/DateFormatter.php). The formatter
240
+
is referenced with the object's attribute by `{{objectKey.attribute:formatterKey:param1:param2...}}`, e.g. `{{user.created_on:date:rfc822}}`.
241
+
242
+
All three elements - objects, snippets and formatters - are given to the [Processor](https://github.com/technicalguru/php-utils/blob/main/src/TgUtils/Templating/Processor.php) in its constructor:
243
+
244
+
```
245
+
$objects = array('user' => $myUser);
246
+
$snippets = array('header' => new HeaderSnippet(), 'footer' => $new FooterSnippet());
247
+
$formatters = array('date' => new DateFormatter();
248
+
$language = 'en';
249
+
$processor = new Processor($objects, $snippets, $formatters, $language);
250
+
```
251
+
252
+
The language is for information and can be used in snippets or formatters to select the right text.
253
+
254
+
Finally you can process a template:
255
+
256
+
```
257
+
$template = '{{header}} Hello {{user.name}}! Your account was created on {{user.created_on:date:d/m/Y}}.{{footer}}';
258
+
echo $processor->process($template);
259
+
260
+
// Output is:
261
+
// IMPORTANT MESSAGE! Hello John Doe! Your account was created on 02/03/2017. Best regards!
262
+
```
224
263
225
264
## Other Utils
226
265
There are some daily tasks that need to be done in applications. The `Utils` class addresses a few of them:
0 commit comments