Skip to content

Commit 75b6d4c

Browse files
committed
#16 - Documenting
1 parent fe08ce0 commit 75b6d4c

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This is a collection of useful classes and functions for every day PHP life. It
1010
* Slugifying strings for usage in URLs
1111
* Generating random strings
1212
* Formatting prices and units
13+
* Simple text templating
1314

1415
These classes are no rocket science, just simple helpers that prevent from wiriting the
1516
same code in various flavours over and over again.
@@ -220,7 +221,45 @@ $emailJavascript = Obfuscation::obfuscateEmail('[email protected]', $id);
220221

221222
Please notice that not all characters are supported in the default character map. It covers mainly
222223
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+
```
224263

225264
## Other Utils
226265
There are some daily tasks that need to be done in applications. The `Utils` class addresses a few of them:

0 commit comments

Comments
 (0)