The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Is possible to pass custom parameters to views??

Alejandro's Avatar

Alejandro

07 Jun, 2012 02:42 PM

Hello,
i've a question, is possible to automatically pass custom variables to views???

i'm using Twig, and i wanted to know if its posible to automatically load custom parameters, i've my templates divided in blocks, i want to principal layout load an array to construct menu, but, i dont want to pass it in every "render" on action....

how can i do this? or is this possible??

  1. 1 Posted by Alejandro on 07 Jun, 2012 02:51 PM

    Alejandro's Avatar

    ok, i've soled it this way, but, maybe is not the best way ....

    create a function middleware, where i create an array into config.
    $app->config('principalMenu', arrayData);

    then, this middleware is called on every GET request i've, so i can load that variable each time.

    then, on Twig extensions, i've create a custom function calle "loadMenu", and then i call it from the layout "{{ customMenu() }}" and it works has i expected.

    is this correct or thers is another simple way??

    thanks!

  2. Support Staff 2 Posted by Andrew Smith on 07 Jun, 2012 03:14 PM

    Andrew Smith's Avatar

    You can do this using addGlobal Twig method.

    $twig = $app->view()->getEnvironment();
    $twig->addGlobal('variablename', $menu);
    

    That should give you what you are looking for. If you need to pass a function use the addFunction method of Twig.

  3. 3 Posted by Alejandro on 07 Jun, 2012 03:18 PM

    Alejandro's Avatar

    ok, that was easy! thank you!

  4. Brian Nesbitt closed this discussion on 29 Jun, 2012 01:15 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac