Create a templates.py file in your code and use this

"""This module provides a number of utility functions for use in templates."""
from pyramid.events import subscriberfrom pyramid.events import
BeforeRenderfrom pyramid.security import has_permission

@subscriber(BeforeRender)def add_global(event):
    event['utils'] = TemplateUtils(event['request'])

class TemplateUtils(object):
    def __init__(self, request):
        self.request = request

    def has_permission(self, permission):
        return has_permission(permission, self.request.context, self.request)

    def is_admin(self,user):
        for role in user.role:
            if 'Admin' in role.__dict__.values():
                return True
        return False




Now we can use these methods in templates as

%if utils.has_permission('admin'):
        <li> <a href="/user/add"> Add user </a> </li>
        <li> <a href="/users/list/vmusers"> User Configuration </a> </li>
        <li> <a href="/users/list/admins"> Manage Admins </a> </li>
%endif


Correct me if i am wrong somewhere.


Thanks,


Regards,

AbdulWahid




On Mon, Sep 2, 2013 at 5:27 PM, Paul Everitt <[email protected]> wrote:

> On 9/2/13 6:51 AM, Marek Szwałkiewicz wrote:
>
>> Hi all,
>>
>> in current project I need to include 2 form objects (flatland forms) in
>> context of almost every template.
>>
>> Those 2 objects are used in master template (that is inherited everywhere)
>> and need to be available in almost all cases.
>> What is a best way to include those objects (except from manually
>> including
>> it in every view return dictionary)?
>>
>
> Arrange for a callable, available in your templates, that will return the
> forms to you. Two classic ways to do this:
>
> 1) BeforeRender event. Write something that subscribes to the BeforeRender
> event and, on each request, assigns something to the request.
>
> http://docs.pylonsproject.org/**projects/pyramid/en/latest/**
> narr/hooks.html#using-the-**before-render-event<http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#using-the-before-render-event>
>
> 2) Request method. Grab the configuration and add a method to the request.
> The method could return a callable, a mapping, whatever.
>
> Here is an example of adding a custom request.get_user request method that
> could be called from your template.
>
> http://docs.pylonsproject.org/**projects/pyramid_cookbook/en/**
> latest/auth/user_object.html<http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/auth/user_object.html>
>
> --Paul
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to 
> pylons-discuss+unsubscribe@**googlegroups.com<pylons-discuss%[email protected]>
> .
> To post to this group, send email to 
> pylons-discuss@googlegroups.**com<[email protected]>
> .
> Visit this group at 
> http://groups.google.com/**group/pylons-discuss<http://groups.google.com/group/pylons-discuss>
> .
> For more options, visit 
> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to