0

I'm looking for the recommended way to pass a same array to the template of multiple routes.

Exemple:

$app->group('/blabla', function (RouteCollectorProxy $group) {
    $group->get('', function (Request $request, Response $response, $args) {
        return $this->get('view')->render($response, 'first-route.html.twig', [
            'var1' => "wiggle",
            'var2' => "lightSpeedIn",
            'var3' => 'fadeInDown',
            'var4' => 'wiggle',
            'var5' => "wobble",
        ]);
    })->setName('portfolio-ancien');

    $group->get('/hello', function (Request $request, Response $response, $args) {
        return $this->get('view')->render($response, 'seconde-route.html.twig', [
            'var1' => "wiggle",
            'var2' => "lightSpeedIn",
            'var3' => 'fadeInDown',
            'var4' => 'wiggle',
            'var5' => "wobble",
        ]);
    })->setName('portfolio-ancien-quiSuisJe');
});

My array parameters is the same, how can I pass it to the template without repeated it twice ?

Macbernie
  • 1,303
  • 6
  • 24
  • 48

1 Answers1

0

You could define them at the group level and then pass them into to the routes with use...

$app->group('/blabla', function (RouteCollectorProxy $group) {
    $vars = [
        'var1' => "wiggle",
        'var2' => "lightSpeedIn",
        'var3' => 'fadeInDown',
        'var4' => 'wiggle',
        'var5' => "wobble",
    ];
    $group->get('', function (Request $request, Response $response, $args) use ($vars) {
        return $this->get('view')->render($response, 'first-route.html.twig', $vars);
    })->setName('portfolio-ancien');
    
    $group->get('/hello', function (Request $request, Response $response, $args) use ($vars) {
        return $this->get('view')->render($response, 'seconde-route.html.twig', $vars);
    })->setName('portfolio-ancien-quiSuisJe');
});
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Repeating `use ($vars)` and passing `$vars` to every template is still a lot of unnecessary redundant code. You can [add global variables to Twig environment](https://stackoverflow.com/a/61121299/1788201) so they are available in every template without being explicitly passed to them. – Nima Jan 25 '21 at 06:04
  • @Nima It's amazing how if someone suggested using global variables in a PHP script would be told about how this is discouraged and not recommended, yet other environments it seems to be encouraged and doing something specifically seems to be deemed *unnecessary redundant code* – Nigel Ren Feb 14 '21 at 07:13
  • @NigelRen Maybe those who discourage using global variables in one environment but encourage using them in other ones are somehow sensitive to `global` keyword itself? In my opinion use of global variables is discouraged in all environments, but if some variables MUST be available everywhere then they actually **are** globals, and _repeating a piece of code to make them available_ does not change this fact. – Nima Feb 14 '21 at 08:28
  • @Nima, the same applies to any environment. When different programmers start adding globals to any environment, the chance that two programmers end up with the same variables means that they can affect code not directly affecting their own needs. Using *MUST* as a reason doesn't always work (Even PSR's which use MUST are only recommendations) – Nigel Ren Feb 14 '21 at 08:32
  • @NigelRen I agree with your point, and as I said I discourage the use of global variables. The __MUST__ word in my comment refers to OP's requirement. – Nima Feb 14 '21 at 09:22
  • @Nima, they mention multiple routes and not all routes. So it may be there are a lot of instances where these variables are not needed. – Nigel Ren Feb 14 '21 at 15:34