Skip to content

Commit b20ea36

Browse files
author
Younes KHOUBZA
authored
Merge pull request #23 from ToshY/issue/presets-value-substitution
Updated preset sections with variable substition example
2 parents cd60492 + 0344cb7 commit b20ea36

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

docs/laravel.md

+39
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,45 @@ class BookController
286286
flash()->add{{ type | capitalize }}('{{ message }}', '{{ title }}');
287287
```
288288

289+
<p id="preset-variables"><a href="#preset-variables" class="anchor"><i class="fa-duotone fa-link"></i> Variables</a></p>
290+
291+
Presets can also contain variables that can be substituted by using the translation system. Take the following example where you have a preset showing a personalised welcome message to the user.
292+
293+
```php
294+
<?php // config/flasher.php
295+
296+
return [
297+
'presets' => [
298+
'hello_user' => [
299+
'type' => '{{ type }}',
300+
'message' => 'welcome_back_user',
301+
],
302+
],
303+
];
304+
```
305+
306+
In the translations file you can define `welcome_back_user` with the message containing the variable `:username`.
307+
308+
```php
309+
<?php // /resources/lang/vendor/flasher/en/messages.php
310+
311+
return [
312+
'welcome_back_user' => 'Welcome back :username',
313+
];
314+
```
315+
316+
If you want to substitute the `:username` in the above translation with a username in the controller, you can achieve this by passing an array of values to be substituted as the second argument.
317+
318+
```php
319+
class BookController
320+
{
321+
public function save()
322+
{
323+
$username = 'John Doe';
324+
325+
flash()->addPreset('hello_user', ['username' => $username]);
326+
```
327+
289328
---
290329

291330
## <i class="fa-duotone fa-list-radio"></i> Dark Mode

docs/symfony.md

+34
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,40 @@ class BookController
266266
flash()->add{{ type | capitalize }}('{{ message }}', '{{ title }}');
267267
```
268268

269+
<p id="preset-variables"><a href="#preset-variables" class="anchor"><i class="fa-duotone fa-link"></i> Variables</a></p>
270+
271+
Presets can also contain variables that can be substituted by using the translation system. Take the following example where you have a preset showing a personalised welcome message to the user.
272+
273+
```yaml
274+
# config/packages/flasher.yaml
275+
276+
flasher:
277+
presets:
278+
hello_user:
279+
type: {{ type }}
280+
message: welcome_back_user
281+
```
282+
283+
In the translations file you can define `welcome_back_user` with the message containing the variable `:username`.
284+
285+
```yaml
286+
# translations/flasher.en.yaml
287+
288+
welcome_back_user: Welcome back :username
289+
```
290+
291+
If you want to substitute the `:username` in the above translation with a username in the controller, you can achieve this by passing an array of values to be substituted as the second argument.
292+
293+
```php
294+
class BookController
295+
{
296+
public function save()
297+
{
298+
$username = 'John Doe';
299+
300+
flash()->addPreset('hello_user', ['username' => $username]);
301+
```
302+
269303
---
270304

271305
## <i class="fa-duotone fa-list-radio"></i> Dark Mode

0 commit comments

Comments
 (0)