How do Global Variables Work?

1,036 views
Skip to first unread message

Peter O

unread,
Jan 5, 2012, 9:03:03 AM1/5/12
to web...@googlegroups.com
Hihi,

I am baffled by what I see between Example 1 and 2.

It may be a good case to improve my understanding of the run-time environment of web2py.

Thanks in advance for any comments.

VIEW: (test.html)

<html>  <body>
{{=globals().get('t1', False)}}
 </body> </html>

CONTROLLER:

== Example 1 ==

def test():
    global t1
    t1 = True
    return dict() # the browser show 'False'. I was expecting 'True'

== Example 2 ==

def test():
    global t1
    t1 = True

    response.render('default/test.html', globals() ) #the return is discarded

    return dict() # the browser show 'True'. I was expecting the same result as Example 1.





Bruno Rocha

unread,
Jan 5, 2012, 9:26:27 AM1/5/12
to web...@googlegroups.com

you need to pass it

return dict(t1=t1)

or

return locals()

http://zerp.ly/rochacbruno

Peter O

unread,
Jan 5, 2012, 9:51:01 AM1/5/12
to web...@googlegroups.com
I understand explicitly passing the variable will do the work.

However, what's the purpose of global variables? I thought this conflicts with one of the examples in the Introduction chapter.

Secondly, I don't understand the different results between Example 1 and 2. Something deeper is happening here, I feel.

Anthony

unread,
Jan 5, 2012, 10:02:52 AM1/5/12
to web...@googlegroups.com
See http://web2py.com/books/default/chapter/29/4#Workflow:

"The view sees every variable defined in the models as well as those in the dictionary returned by the action, but does not see global variables defined in the controller."

web2py builds an environment and runs the models in that environment (which may then add objects to it). It then creates a copy of the environment before running the controller, and after running the controller, it adds the items returned in the dictionary by the controller to the pre-controller copy of the environment, which is where the view is executed. You can see this here: http://code.google.com/p/web2py/source/browse/gluon/main.py#200

Anthony

Anthony

unread,
Jan 5, 2012, 10:05:10 AM1/5/12
to web...@googlegroups.com
However, what's the purpose of global variables? I thought this conflicts with one of the examples in the Introduction chapter.

Which example? 

Peter O

unread,
Jan 5, 2012, 10:14:45 AM1/5/12
to web...@googlegroups.com
http://web2py.com/books/default/chapter/29/5?search=globals%28%29

I see. Am I right that View doesn't see the global variables (in Controller), but the parent View sees the variables in View as global variables?

Then, what causes the difference between Example 1 and 2? What's the relationship between the 'environment' and the 'context' in response? How do I access the 'environment' explicitly in the Controller?

Massimo Di Pierro

unread,
Jan 5, 2012, 10:34:16 AM1/5/12
to web2py-users
The list of global variables defined in models and passed to the
controllers are also passed to the view (for example the views see db,
request, session, etc. globals variables defined in controllers are
not passes to the view unless done explicitely.

Anthony

unread,
Jan 5, 2012, 10:56:51 AM1/5/12
to web...@googlegroups.com
On Thursday, January 5, 2012 10:14:45 AM UTC-5, Peter O wrote:
http://web2py.com/books/default/chapter/29/5?search=globals%28%29

The globals being accessed in the above linked example (which is actually the 'welcome' app layout.html) are defined either in models or in another (included) view, not in a controller.
 
I see. Am I right that View doesn't see the global variables (in Controller), but the parent View sees the variables in View as global variables?

It depends. An extended view sees variables defined in an extending view if (a) the variables are defined in the extending view before the {{extend}} directive, or (b) the variables are referenced in the extended view after the point where the extending view has been included. The sidebar example in 'welcome' linked above works via (a). Note that /default/index.html in 'welcome' starts with:

{{left_sidebar_enabled,right_sidebar_enabled=False,True}}
{{extend 'layout.html'}}

By defining left_sidebar_enabled and right_sidebar_enabled before extending layout.html, those variables are defined before any of the layout.html code is executed, so they are available anywhere in layout.html (including within any other views included within layout.html). When executing a view, web2py first pieces together the entire page, with all extends and includes, and then it executes the Python code to generate the HTML.
 
Then, what causes the difference between Example 1 and 2? What's the relationship between the 'environment' and the 'context' in response? How do I access the 'environment' explicitly in the Controller?

In your second example, you have:

response.render('default/test.html', globals()) 

Passing globals() to response.render() adds all the globals to the environment in which the view is rendered (actually, most of the objects in globals() are already in the view environment, so you are only really adding any new globals created in the controller). Your first example would be equivalent to:

response.render('default/test.html', dict()) 

which is simply equivalent to:

response.render('default/test.html') 

In that case, the view environment includes all the globals defined in the models (as well as the web2py framework globals), but you haven't added any variables generated in the controller to the view environment.

Anthony
Reply all
Reply to author
Forward
0 new messages