Skip to content

Commit

Permalink
Merge pull request #23 from ToshY/issue/presets-value-substitution
Browse files Browse the repository at this point in the history
Updated preset sections with variable substition example
  • Loading branch information
Younes KHOUBZA authored May 29, 2023
2 parents cd60492 + 0344cb7 commit b20ea36
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/laravel.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,45 @@ class BookController
flash()->add{{ type | capitalize }}('{{ message }}', '{{ title }}');
```

<p id="preset-variables"><a href="#preset-variables" class="anchor"><i class="fa-duotone fa-link"></i> Variables</a></p>

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.

```php
<?php // config/flasher.php

return [
'presets' => [
'hello_user' => [
'type' => '{{ type }}',
'message' => 'welcome_back_user',
],
],
];
```

In the translations file you can define `welcome_back_user` with the message containing the variable `:username`.

```php
<?php // /resources/lang/vendor/flasher/en/messages.php

return [
'welcome_back_user' => 'Welcome back :username',
];
```

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.

```php
class BookController
{
public function save()
{
$username = 'John Doe';

flash()->addPreset('hello_user', ['username' => $username]);
```

---

## <i class="fa-duotone fa-list-radio"></i> Dark Mode
Expand Down
34 changes: 34 additions & 0 deletions docs/symfony.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,40 @@ class BookController
flash()->add{{ type | capitalize }}('{{ message }}', '{{ title }}');
```

<p id="preset-variables"><a href="#preset-variables" class="anchor"><i class="fa-duotone fa-link"></i> Variables</a></p>

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.

```yaml
# config/packages/flasher.yaml
flasher:
presets:
hello_user:
type: {{ type }}
message: welcome_back_user
```

In the translations file you can define `welcome_back_user` with the message containing the variable `:username`.

```yaml
# translations/flasher.en.yaml
welcome_back_user: Welcome back :username
```

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.

```php
class BookController
{
public function save()
{
$username = 'John Doe';
flash()->addPreset('hello_user', ['username' => $username]);
```

---

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

0 comments on commit b20ea36

Please sign in to comment.