Hi!
This is somewhat a continuation of Conditional presence of a variable in
view <https://groups.google.com/d/topic/web2py/n8jqIXdV1js/discussion>and How
do Global Variables
Work?<https://groups.google.com/d/topic/web2py/zt74x2FsssU/discussion>
So, what is recommended way of checking if var is defined and passed to a
view?
i.e. avoiding errors
<type 'exceptions.NameError'> name 'foo' is not defined
I see at least 3 ways, one is using 'try ... except' in a view:
{{block content_block}}
{{try:}}
{{=message or ""}}
{{except:}} <h1>Message not defined!</h1>
{{pass}}
{{end}}
other, using "if":
{{block content_block}}
{{if 'message' in globals():}}
{{=message or ""}}
{{pass}}
{{end}}
and also we can use globals().get():
{{=globals().get('message', False)}}
which will return False if message was not returned with dict in controller.
Something like this is used in default layout along with "or" that simply
checks if var is not empty.
And what if I have a lot of variables to return from controller?
Then returning locals seems to be simpler and cleaner way (at least in
terms of syntax), but on the other hand I really like the idea of passing
only selected variables (that will be used) to the view, not all I have in
controller.
Probably somebody can point out to other security issues with returning
locals to all views?
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.