On Wednesday, January 26, 2011 12:56:56 PM UTC-5, rochacbruno wrote: 
>
> To simplify, I think you just need to use models instead of modules folder. 
>
>
> Put the myfile.py in /models and you have access to any function, class or 
> variable defined in there.
>
> You can keep your file in modules, but it will be a little more 
> complicated, you will need to create a model file which imports the file 
> from module.
>
> in models/importator.py
> myfile = local_import('/.../myfile')
>
>
> now, you can access myfile.myvar in any place you want.
>
 
Note, this works because all model files are executed on every request, so 
anything in the model files is always available globally (i.e., in all 
controllers and views).
 
I think this is only mentioned explicitly at one spot in the book (
http://web2py.com/book/default/chapter/04?search=executes+the+models). Also, 
if you have multiple model files, they are executed in alphabetical order, 
and I don't think that is mentioned in the book at all. This topic should 
probably be discussed in more detail in the book, as many people don't 
realize the model files can be used (judicously) for site-wide helpers and 
variables if needed. This might deserve its own small section somewhere in 
Chapter 4.
 
Also, Walter, I don't know what your variable is, but if it's something that 
is ultimately needed in your views, I wonder if it would make sense to 
define it in your 'layout.html' view file (assuming all views that need the 
variable extend 'layout.html').
 
Anthony
 

Reply via email to