It seems like you are doing some complex things with the views.  That is 
fine,  but personally I find this:

1. In MVC it is more recommended to collect and prepare all your data in 
the controller and then pass all that to the view when it is ready.
2. In that way you can make sure that all the variables exist.
3. If someone else is writing a view,  you can still inform them all the 
variables which are available. You might develop your own conventions for 
that.

If your method of working does not allow that workflow,  you can still do 
what you are doing,  but I don't think there is a 'recommended' way.  The 
methods of checking whether variables exist is really a python question and 
not one specific to web2py.  You already know some methods to do that.

By 'exposing' the variables,  yes,  I meant for example {{=myvar}}.  I mean 
you can pass locals() to the view,  but you do not need to expose them all 
to the public.


On Sunday, 7 July 2013 22:15:14 UTC+1, shapova...@gmail.com wrote:
>
> thanks for reply.
>
> Yes, I've showed 3 ways of checking if var was previously defined, but the 
> question was about preferred way of doing that in Web2Py, also I want to 
> add 'not empty' to this check.
>
> > If you are the developer you should know what variables you are going to 
> be using in the view?
>
> It may happen that views are written by other people, etc, isn't it better 
> to introduce as much independence as possible?
>
> Here is my case, but I guess there may be other similar. 
>
> I keep content for my static pages in db. It is broken in parts like 
> footer_block, header_block, content_block, etc. 
> And I use blocks in views, so I can fill parts of view with content from 
> db, or leave them with default content from extended view (i.e. my version 
> of layout.html) with {{super}}. Also, some part of page can be empty in db. 
>
> So, I thought that it would be a good idea to return only non-empty parts 
> to the view, and in case when some part is empty in db, it will not be 
> passed to the view, and content from extended layout will be used.
>
> This is in the view that extends main template, this only checks if var is 
> defined, but not if it is not empty.
> {{block header_block}} {{try:}} {{=header_block or ''}} {{except:}} {{
> super}} {{pass}} {{end}}
>
> Does it make sense?
>
> On Sunday, July 7, 2013 11:52:54 PM UTC+3, villas wrote:
>>
>> It seems that you already know how to check whether variables exist,  so 
>> you have already answered the question.
>>
>> However,  I think the entire premise of the question seems strange.  If 
>> you are the developer you should know what variables you are going to be 
>> using in the view?  So, just make sure they always exist so you don't have 
>> to mess up your view with all those tests.
>>
>> Maybe if you doing something unusal it is better to state the use case?
>>
>>

-- 

--- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to