[web2py] Re: Common value for all application pages
This value lives in my file in the modules folder.
[web2py] Re: Common value for all application pages
Not sure I undestand On Jan 26, 10:03 am, walter wdv...@gmail.com wrote: I want to set common value for all pages. If do it manually, that is, the probability of error, if I forget to do it. Is there any option to regular automatically add some value to the dictionary returned by the controller?
[web2py] Re: Common value for all application pages
Into the modules folder I created file (call it myfile.py for example). Into this file there is a variable title. This variable contains value that is common for all pages of my application. If import var = local_import('myfile') and return dict(var=var) all be OK. But with many controllers I'll be have duplicated code and risk get an error if I forget add var to the returned dictionary. On 26 янв, 19:22, Massimo Di Pierro massimo.dipie...@gmail.com wrote: Not sure I undestand
Re: [web2py] Re: Common value for all application pages
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. Bruno Rocha http://about.me/rochacbruno/bio
Re: [web2py] Re: Common value for all application pages
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
[web2py] Re: Common value for all application pages
I want to create a unified view file for multiple application. I plan to use it with the different design templates. On 26 янв, 21:06, Anthony abasta...@gmail.com wrote: On Wednesday, January 26, 2011 12:56:56 PM UTC-5, rochacbruno wrote: 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