Skip to content

Commit abdadd4

Browse files
committed
Documented new attachment token with CSV example and restructured examples
1 parent 2ac763c commit abdadd4

File tree

13 files changed

+245
-93
lines changed

13 files changed

+245
-93
lines changed

docs/notification-center-pro/custom-tokens/_index.de.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ weight: 200
66

77
Mit dem Notification Center Pro kannst du benutzerdefinierte Tokens erstellen, die du dann in deinen Benachrichtigungen verwenden kannst. Eigene Tokens basieren auf Twig-Templates, d.h. du kannst von allen Twig-Funktionen profitieren, die du dir vom Contao-Templating gewohnt bist.
88

9-
Da die Möglichkeiten praktisch endlos sind, sehen wir uns ein Beispiel an. Du kannst auch einen Blick auf unsere [Beispielsammlung]({{% ref "/custom-tokens/example-collection" %}}) werfen, in der wir gängige Anwendungsfälle sammeln. Wenn du etwas hast, von dem du denkst, dass andere es verwenden könnten, lass es uns bitte wissen!
9+
Da die Möglichkeiten praktisch endlos sind, sehen wir uns ein Beispiel an. Du kannst auch einen Blick auf unsere
10+
[Beispielsammlung]({{% ref "/custom-tokens/examples" %}}) werfen, in der wir gängige Anwendungsfälle sammeln. Wenn du etwas hast, von dem du denkst, dass andere es verwenden könnten, lass es uns bitte wissen!
1011

1112
## Beispiel Anwendungsfall
1213

docs/notification-center-pro/custom-tokens/_index.en.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ With Notification Center Pro, you can create custom tokens which you can then us
77
tokens are based on Twig templates which means you can access all the goodness you know and love from Contao
88
templating.
99

10-
As the possibilities are virtually endless, let's look at an example. You can also take a look at our [example collection]({{% ref "/custom-tokens/example-collection" %}}) where we collect common use cases. If you have something you think other users could use, please let us know!
10+
As the possibilities are virtually endless, let's look at an example. You can also take a look at our [example
11+
collection]({{% ref "/custom-tokens/examples" %}}) where we collect common use cases. If you have something you think other users could use, please let us know!
1112

1213
## Example use case
1314

docs/notification-center-pro/custom-tokens/example-collection/_index.de.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

docs/notification-center-pro/custom-tokens/example-collection/_index.en.md

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "Beispiele"
3+
url: "eigene-tokens/beispiele"
4+
weight: 20
5+
---
6+
7+
Hier findest du eine Samlung von Beispielen anderer Anwender:innen. Wenn du auch ein tolles Beispiel hast, das du gerne hier aufgelistet hättest, lass es uns bitte wissen!
8+
9+
{{% children %}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: "Examples"
3+
weight: 20
4+
---
5+
6+
Here you can find a collection of examples from other users. If you also have a great example that you would like to see listed here, please let us know!
7+
8+
{{% children %}}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: "Rechnen mit Twig"
3+
url: "eigene-tokens/beispiele/rechnen-mit-twig"
4+
---
5+
6+
Kennst du das? Ein einfaches Bestellformular, ohne grosse Webshop-Implementierung aber trotzdem hättest du gerne die
7+
MwSt. und das Total in der E-Mail ausgewiesen? Mit dem Notification Center Pro absolut kein Problem, wir können uns
8+
ja eigene Tokens für die MwSt. und das Total erstellen und diese dann nutzen. Wir nehmen an, wir haben ein Formularfeld, das heisst `anzahl_prospekte` und dort kann man als Besteller:in Prospekte bestellen. Jeder Prospekt kostet 4 Euro. Wenn jetzt jemand 5 Prospekte bestellt, möchten wir also eine Mail mit 20 Euro plus 3.80 Euro MwSt (19% in diesem Beispiel) und einem Gesamt-Total von 23.80 Euro schicken.
9+
10+
In unsere Mail schreiben wir bspw. Folgendes:
11+
12+
```none
13+
Hallo ##form_firstname##,
14+
15+
Du hast bei uns ##form_anzahl_prospekte## Prospekte bestellt. Hier ist deine Rechnung:
16+
17+
##form_anzahl_prospekte## Prospekte à € 4,-: ##subtotal##
18+
Mehrwertsteuer 19%: ##mwst##
19+
Gesamttotal: ##gesamttotal##
20+
```
21+
22+
So könnte unsere Twig-Logik dann aussehen:
23+
24+
```twig
25+
{% if rawTokens.has('form_anzahl_prospekte') %}
26+
{% set subtotal = rawTokens.byName('form_anzahl_prospekte').value * 4 %}
27+
{% set mwst = subtotal * 19 / 100 %}
28+
{% set gesamttotal = subtotal + mwst %}
29+
{% endif %}
30+
```
31+
32+
Diese Logik kopieren wir nun in 3 Tokens und geben jeweils die gewünschte Variable aus:
33+
34+
1. Token `subtotal` nutzt obengenannte Logik und `{{ subtotal }}`
35+
2. Token `mwst` nutzt obengenannte Logik und `{{ mwst }}`
36+
3. Token `gesamttotal` nutzt obengenannte Logik und `{{ gesamttotal }}`
37+
38+
Tada! 🎉
39+
40+
{{% notice tip %}}
41+
Verwende den [Twig-Filter `number_format`](https://twig.symfony.com/doc/3.x/filters/number_format.html), um die Zahlen nach deinen Wünschen zu formatieren!
42+
{{% /notice %}}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: "Calculating with Twig"
3+
---
4+
5+
Do you know this situation? A simple order form, without a large webshop implementation, but you would still like to
6+
have the VAT and the total shown in the e-mail? No problem at all with the Notification Center Pro, we can create our own tokens for the VAT and the total and then use them. Let's assume we have a form field called `number_of_brochures` where the customer can brochures. Each brochure costs 4 Euros. If someone now orders 5 brochures, we would like to send a mail with 20 Euro plus 3.80 Euro VAT (19% for Germany in this example) and a total amount of 23.80 Euro.
7+
8+
For example, we write the following in our mail:
9+
10+
```none
11+
Hello ##form_firstname##,
12+
13+
You have ordered ##form_number_of_brochures## brochures from us. Here is your invoice:
14+
15+
##form_number_of_brochures## brochures at € 4,- each: ##subtotal##
16+
Value added tax 19%: ##tax##
17+
Total amount: ##total##
18+
```
19+
20+
So könnte unsere Twig-Logik könnte dann wie folgt aussehen:
21+
22+
```twig
23+
{% if rawTokens.has('form_anzahl_prospekte') %}
24+
{% set subtotal = rawTokens.byName('form_number_of_brochures').value * 4 %}
25+
{% set tax = subtotal * 19 / 100 %}
26+
{% set total = subtotal + tax %}
27+
{% endif %}
28+
```
29+
30+
We now copy this logic into 3 tokens and output the desired variable in each case:
31+
32+
1. token `subtotal` uses the above logic and `{{ subtotal }}`
33+
2. token `tax` uses the above logic and `{{ tax }}`
34+
3. token `total` uses the above logic and `{{ total }}`
35+
36+
Tada! 🎉
37+
38+
{{% notice tip %}}
39+
Use the [Twig `number_format` filter](https://twig.symfony.com/doc/3.x/filters/number_format.html) to format the numbers to your liking!
40+
{{% /notice %}}
41+
42+
43+
44+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: "CSV-Anhang"
3+
url: "eigene-tokens/beispiele/csv-anhang"
4+
---
5+
6+
Stell dir vor, du möchtest eine CSV-Datei an deine E-Mail anhängen, die die eingegebenen Formulardaten enthält.
7+
Das Hauptproblem, das du dabei beachten musst, ist das korrekte Escaping. Stell dir eine Logik wie diese vor:
8+
9+
```
10+
firstname,lastname,city
11+
##form_firstname##,##form_firstname##,##form_city##
12+
```
13+
14+
Das würde nicht funktionieren, indem du einfach die Tokens ersetzt, weil du drei verschiedene Zeichen escapen musst:
15+
16+
* Das Trennzeichen ("Delimiter" - `,` standardmässig)
17+
* Das Begrenzungszeichen ("Enclosure" - `"` standardmässig)
18+
* Das Escape-Zeichen selbst (`\` standardmässig)
19+
20+
Andernfalls, wenn ein Besucher z. B. `Jürgen "Kloppo"` als Vornamen und `Portland, ME` als Stadt angibt, um zu
21+
präzisieren, welches Portland gemeint ist, würdest du am Ende etwas erhalten, das völlig ungültiges CSV ist:
22+
23+
```
24+
firstname,lastname,city
25+
Jürgen "Kloppo",Klopp,Portland, ME
26+
```
27+
28+
Die korrekte Ausgabe wäre:
29+
30+
```
31+
firstname,lastname,city
32+
"Jürgen \"Kloppo\"",Klopp,"Portland, ME"
33+
```
34+
35+
Das manuell zu tun ist schwierig, deswegen verfügt das Notification Center Pro über einen `csv` Twig-Filter:
36+
37+
```twig
38+
{% set headers = [
39+
'firstname',
40+
'lastname',
41+
'city',
42+
] %}
43+
{{ headers|csv }}
44+
{% set row = [
45+
parsedTokens.form_firstname|default(''),
46+
parsedTokens.form_lastname|default(''),
47+
parsedTokens.form_city|default(''),
48+
] %}
49+
{{ row|csv }}
50+
```
51+
52+
{{% notice tip %}}
53+
Der [Twig `default`-Filter](https://twig.symfony.com/doc/3.x/filters/default.html) sorgt dafür, dass statt eines
54+
Fehlers eine leere Ausgabe erfolgt, wenn ein Token nicht existiert.
55+
{{% /notice %}}
56+
57+
Das war's! Du hast nun ein Token mit dem korrekten CSV-Wert. Alles, was du noch tun musst, ist diesem Token einen
58+
Namen zu geben, es als Datei-Token zu aktivieren und ihm den gewünschten Dateinamen zu vergeben:
59+
60+
![Screenshot of the file attachment token configuration]({{% asset "/notification-center-pro/images/screenshot_csv_token_attachment.png" %}}?classes=shadow)
61+
62+
Dieses Token kann nun benutzt werden, um bspw. das CSV einer Mail anzuhängen. Tada! 🎉
63+
64+
{{% notice tip %}}
65+
Der `csv`-Filter ist nicht Teil von Twig, sondern wird stattdessen vom Notification Center Pro bereitgestellt. Du
66+
kannst auch mit einem anderen Trennzeichen arbeiten. Wenn du die Werte beispielsweise mit `;` trennen möchtest,
67+
verwende `{{ row|csv(';') }}`. Der zweite und dritte Parameter können genutzt werden, um das "Enclosure" und
68+
Escape-Zeichen entsprechend anzupassen, falls erforderlich.
69+
{{% /notice %}}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: "CSV attachment"
3+
---
4+
5+
Imagine you wanted to attach a CSV file to your e-mail that contains the form data that was submitted.
6+
The major problem here you need to consider is correct escaping. Imagine a logic like this:
7+
8+
```
9+
firstname,lastname,city
10+
##form_firstname##,##form_firstname##,##form_city##
11+
```
12+
13+
This would not work by simply replacing the tokens because you need to escape 3 different characters:
14+
15+
* The separator (`,` by default)
16+
* The enclosure (`"` by default)
17+
* The escape character itself (`\` by default)
18+
19+
Otherwise, if a visitor e.g. submits `Jürgen "Kloppo"` as their firstname and `Portland, ME` as city to clarify which
20+
Portland, you would end up having this which is completely invalid CSV:
21+
22+
```
23+
firstname,lastname,city
24+
Jürgen "Kloppo",Klopp,Portland, ME
25+
```
26+
27+
The correct output would be
28+
29+
```
30+
firstname,lastname,city
31+
"Jürgen \"Kloppo\"",Klopp,"Portland, ME"
32+
```
33+
34+
Doing that manually is hard, this is why the Notification Center Pro provides its own `csv` Twig filter:
35+
36+
```twig
37+
{% set headers = [
38+
'firstname',
39+
'lastname',
40+
'city',
41+
] %}
42+
{{ headers|csv }}
43+
{% set row = [
44+
parsedTokens.form_firstname|default(''),
45+
parsedTokens.form_lastname|default(''),
46+
parsedTokens.form_city|default(''),
47+
] %}
48+
{{ row|csv }}
49+
```
50+
51+
{{% notice tip %}}
52+
The [Twig `default` filter](https://twig.symfony.com/doc/3.x/filters/default.html) would make sure there is an empty
53+
value if the token is not provided instead of throwing an error.
54+
{{% /notice %}}
55+
56+
That's it, you now have a token with the correct CSV value. All you need to do is to give this token a name, enable
57+
it as a file token and give it your desired filename:
58+
59+
![Screenshot of the file attachment token configuration]({{% asset "/notification-center-pro/images/screenshot_csv_token_attachment.png" %}}?classes=shadow)
60+
61+
This token can now be used to e.g. attach it to an e-mail. Tada! 🎉
62+
63+
{{% notice tip %}}
64+
The `csv` filter is not part of Twig but instead provided by the Notification Center Pro. You can also work with a
65+
different delimiter. If you need to separate the values by `;` for example, use `{{ row|csv(';') }}`. The second and
66+
third argument can be used to configure the enclosure and escape character respectively, if required.
67+
{{% /notice %}}

0 commit comments

Comments
 (0)