[mod_python] psp

Julien Cigar jcigar at ulb.ac.be
Mon Jun 12 11:03:35 EDT 2006


With the help of a knowledge I resolved my problem with:

def access( path , current , default = '' ) :
    if not isinstance(current, dict):
        current = current.__dict__
    parts = path.split('.')
    if not parts:
        return default
    for part in parts[:-1]:
        if part not in current:
            return default
        current = current[part].__dict__
    return current.get(parts[-1] , default)

I think it could be a good idea in psp to have a predefined bunch of 
standard "template tags". I wondered whether somebody would have already 
thought to that ? I think It could improve the clearness of the 
templates a lot ...

Regards,
Julien


Julien Cigar wrote:
> Hello list,
>
> I have a little problem with psp (I'm using psp as a template engine)
>
> In one of my templates I have :
> <input type="text" class="scientific_name" name="scientific_name" 
> value="<%= invasive.scientific_name %>" />
>
> The problem I have is that I'm using this form in different context 
> and sometimes the 'invasive' object doesn't exists.
> Is there a way to "display-only-if-object-exist" ? What I would like 
> to avoid is things like :
>
> <%
> try:
>    # BEGIN
> %>
>    <%= invasive.scientific_name %>
> <%
>    #END
> except:
>    #BEGIN
> %>
> blabla
> <%
> # NED
> %>
>
> I tried something like :
>
> def show_if_defined(s, obj=globals()):
>    for part in s.split('.') :
>        if not hasattr(obj, part):
>            return ''
>        obj = getattr(obj, part)
>    return obj
>
> and
> <input type="text" class="scientific_name" name="scientific_name" 
> value="<%= show_if_defined('invasive.scientific_name') %>" />
>
> but without success ...
>
> Any idea ?
>
> Thanks
>


-- 
Julien Cigar
Belgian Biodiversity Platform
http://www.biodiversity.be
Université Libre de Bruxelles
Campus de la Plaine CP 257
Bâtiment NO, Bureau 4 N4 115C (Niveau 4)
Boulevard du Triomphe, entrée ULB 2
B-1050 Bruxelles
office: jcigar at ulb.ac.be
home: mage at mordor.ath.cx



More information about the Mod_python mailing list