Call a view from a template

1 view
Skip to first unread message

Thomas Riché

unread,
Jan 27, 2010, 9:22:44 AM1/27/10
to Pluf Users
Hello,

I have the following problem;

I need to display a menu bar on all the pages of my app => I putted in place the according <div> in my base.html template.
The thing is, the menu need to be different given the user, ie Anonymous get minimal menu, normal user get additional menu and admin get full menu.

To achieve this, I created a Menu model, and store my menu in the  DB, given a permission.
I also create a Menu View that returns the list of menus according to the user rights.

My question is: do I have to

$menus = new Platoon_Views_Menu();
$menus = $menus->get($request, $match);

in each and every views and then pass it to the template

{* Display the menu *}
    <div id="menu">
        {*assign $menus = Platoon_Views_Menu::menu()*}
        {if $menus}
            {foreach $menus as $menu}
                <div id="item"><a title="{$menu.description}" href="{$menu.link}">{$menu.name}</a></div>
            {/foreach}
        {/if}
    </div>

Or can I somehow tell the template that the menu variable needs to be generated from the Platoon_Views_Menu ?

Thanks a lot.


Bruno Renié

unread,
Jan 27, 2010, 9:56:14 AM1/27/10
to pluf-...@googlegroups.com
Hello,

Sorry for not replying to your previous post, I was a bit busy... This
looks related so here you go :)

On Wed, Jan 27, 2010 at 3:22 PM, Thomas Riché <thomas...@gmail.com> wrote:
> Hello,
>
> I have the following problem;
>
> I need to display a menu bar on all the pages of my app => I putted in place
> the according <div> in my base.html template.
> The thing is, the menu need to be different given the user, ie Anonymous get
> minimal menu, normal user get additional menu and admin get full menu.
>
> To achieve this, I created a Menu model, and store my menu in the  DB, given
> a permission.
> I also create a Menu View that returns the list of menus according to the
> user rights.

A 'Menu' model sounds overkill according to what you want to achieve.
If you need to dynamically display a menu depending on the user's
role, here's what I would do :

- A view that looks like :

Pluf::loadFunction('Pluf_Shortcuts_RenderToResponse');

class App_Views {
public function index($request, $match) {
// do something useful, and then...
return Pluf_Shortcuts_RenderToResponse('sdoc/base.html',
array(), $request);
}
}

By default, the RenderToResponse will put the '$request->user'
attribute in the template context. Then, you will need in your
template:

<div id="menu">
<!-- items available to all roles -->
{if $user.isAnonymous()}
<!-- some menu items only for anonymous users -->
{else}
<!-- some other menu items for authenticated users -->
{/if}
{if $user.staff}
<!-- some menu items for administrators -->
{/if}

You could also use Pluf's permission system to do things like :

{if $user.has_perm('App.do_something_nasty')}
<!-- tell him he can do nasty things -->
{/if}

You'll probably have more detailed information about the permission
system in the thread you just started :)

Regards,
Bruno

> --
> You received this message because you are subscribed to the Google Groups
> "Pluf Users" group.
> To post to this group, send email to pluf-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pluf-users+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pluf-users?hl=en.
>

Reply all
Reply to author
Forward
0 new messages