Default template variables

69 views
Skip to first unread message

Theron Luhn

unread,
May 9, 2012, 12:27:14 AM5/9/12
to pylons-...@googlegroups.com

I can pass variables into my template by returning them in a dictionary from the view controller. However, if I try to access a variable from the template that I didn't pass, I'll get an error.  Is it possible to have a default value so I don't get an error? Maybe with BeforeRender or something?

- Theron

Robert Forkel

unread,
May 9, 2012, 12:51:00 AM5/9/12
to pylons-...@googlegroups.com
If you are using mako, you may want to have a look at
http://docs.makotemplates.org/en/latest/runtime.html#context-variables
and adapt the strict_undefined setting in your config.
regards
robert
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.

Theron Luhn

unread,
May 9, 2012, 2:09:21 PM5/9/12
to pylons-...@googlegroups.com
I'm using Chameleon :\


On Tuesday, May 8, 2012 9:51:00 PM UTC-7, Robert Forkel wrote:
If you are using mako, you may want to have a look at
http://docs.makotemplates.org/en/latest/runtime.html#context-variables
and adapt the strict_undefined setting in your config.
regards
robert

On Wed, May 9, 2012 at 6:27 AM, Theron Luhn <the...@luhn.com> wrote:
> I can pass variables into my template by returning them in a dictionary from
> the view controller. However, if I try to access a variable from the
> template that I didn't pass, I'll get an error.  Is it possible to have a
> default value so I don't get an error? Maybe with BeforeRender or something?
>
> - Theron
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discuss+unsubscribe@googlegroups.com.

Erik Buttram

unread,
May 9, 2012, 6:09:28 PM5/9/12
to pylons-...@googlegroups.com
You can use tal:on-error to substitute content in case there's a NameError.  There's a couple other ideas/suggestions on this question on stack overflow:

http://stackoverflow.com/questions/8854179/chameleon-zpt-templates

I've tried using tal:one-error and it seems to work if your only using the variable to fill the content of an element.  Haven't used it for anything more advanced than that though.

Malthe Borch

unread,
May 10, 2012, 3:36:07 AM5/10/12
to pylons-...@googlegroups.com
On 9 May 2012 06:27, Theron Luhn <the...@luhn.com> wrote:
> I can pass variables into my template by returning them in a dictionary from
> the view controller. However, if I try to access a variable from the
> template that I didn't pass, I'll get an error.  Is it possible to have a
> default value so I don't get an error?

Yes!

You can use the "|" operator:

<div>
Hello, ${name | "world"}.
</div>

(Note that you can even use it several times, to provide multiple
"fallback" expressions.

\malthe

Raj

unread,
May 10, 2012, 12:46:22 PM5/10/12
to pylons-...@googlegroups.com
You can create an 'h' helper object as described here--> http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/templates/templates.html#using-a-before-render-event-to-expose-an-h-helper-object

or just have a subscribers.py file with:

@subscriber(BeforeRender)
def template_globals(event):
    request = event['request']  ## this is how you access the request object in this function

    from datetime import datetime
    event.update({'someglobalvar': 'this is my global value',
                                  'thedate': datetime.now})

    return ##no need to return anything

Then you can just do ${thedate()} in any of your templates.
-Raj

Raj

unread,
May 10, 2012, 12:48:17 PM5/10/12
to pylons-...@googlegroups.com
In the second example, config.scan() finds your @subscriber(BeforeRender)

Theron Luhn

unread,
May 10, 2012, 12:52:27 PM5/10/12
to pylons-...@googlegroups.com
Malthe's solution is the simplest and it works perfectly.  But thank you all for your answers!

- Theron


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/Uw-XGOu0ppcJ.

To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.

Jonathan Vanasco

unread,
May 10, 2012, 8:57:58 PM5/10/12
to pylons-discuss
Pylons had the concept of an attribute-safe object ( see below )

I toss it in /lib/helpers and use them throughout my code. I
typically have my core handler stash an instance or two in the request
object. All my template variables look to read off this object.



=======

class AttributeSafeObject(object):
"""object, with lax attribute access ( returns '' when the
attribute does not exist). Based on pylons."""

def __init__(self,**kwargs):
for key in kwargs:
setattr(self,key,kwargs[key])

def __getattr__(self, name):
try:
return object.__getattribute__(self,name)
except AttributeError:
if 0:
log.debug("No attribute called %s found on
AttributeSafeObject object, returning empty string", name)
return ''

Максим Коринец

unread,
May 14, 2012, 1:30:54 AM5/14/12
to pylons-...@googlegroups.com
To add to other answers - while using Mako I sometimes implement default rendering like this:

<div>${variable or 'some default value'}</div>

So, if variable is None, some default value is rendered. Don't know if this will work with Chameleon though.

Malthe Borch

unread,
May 14, 2012, 3:26:26 AM5/14/12
to pylons-...@googlegroups.com
Yep, it will.

Alternatively, you can use the ternary operator if you strictly want
to guard against ``None``:

${variable if variable is not None else 'some default value'}

\malthe

Marco Mariani

unread,
May 14, 2012, 8:03:06 AM5/14/12
to pylons-...@googlegroups.com
On 14 May 2012 09:26, Malthe Borch <mbo...@gmail.com> wrote:

> Alternatively, you can use the ternary operator if you strictly want
> to guard against ``None``:
>
>  ${variable if variable is not None else 'some default value'}

To keep it readable, I have an utility function available to all templates:

> def coalesce(*seq):
> el = None
> for el in seq:
> if el is not None:
> return el
> return el

then it becomes ${coalesce(variable, 'some default value')}

and sometimes this one:

> def coalesce_wrap(wrapper, *seq):
> el = coalesce(*seq)
> if el is not None:
> return wrapper(el)
> else:
> return None
Reply all
Reply to author
Forward
0 new messages