As an alternative, you might try something like this (not tested):
{{import os}}
{{from gluon.template import render}}
{{for m in app.modules.codes:}}
{{=XML(render(filename=os.path.join(request.folder, m, 'left_navig.html'
),
path=os.path.join(request.folder, 'views'), context=
globals()))}}
{{pass}}
You could also move the code to the controller:
def myaction():
result = XML('\n'.join([
render(filename=os.path.join(request.folder, m, 'left_navig.html'),
path=os.path.join(request.folder, 'views'), context=globals
())
for m in app.modules.codes]))
return dict(result=result)
In the view:
{{=result}}
Anthony
On Thursday, August 1, 2013 9:52:43 AM UTC-4, Anthony wrote:
>
> The entire template is parsed (including all of the "include" directives)
> before any template Python code is run, so the include directives cannot
> include variables defined in the template itself. The code that comes right
> after the "include" is eval'ed in the view environment, which includes the
> web2py global environement, plus any variables defined in the model files
> or returned in the dict of the controller action. So, as you have observed,
> you can include variables in the "include" directive, but they must be
> defined in the models or controller, not in the view itself.
>
> Basically, "include" and "extend" are template system directives, not
> Python code, so they are processed when the template is parsed (into Python
> code), not when the parsed template is finally executed.
>
> Also, keep in mind that if you're "include" and "extend" files are
> determined dynamically at runtime using variables to determine the
> filenames, then you will not be able to compile the views (which obviously
> must be compiled prior to runtime, when the filenames will not be known).
>
> Anthony
>
> On Thursday, August 1, 2013 8:45:40 AM UTC-4, David Marko wrote:
>>
>> I'm trying to use include in 'for' statement importing left navigation
>> snippets from separate folders for particular modules. Code is very simple
>> as below. But it fails on line with include saying that 'm' variable doesnt
>> exist. I tried this and that and found out that include can see variables
>> specified in models but cant see any variable defined in template ... What
>> wrong I'm doing?
>>
>> {{ for m in app.modules.codes: }}
>> {{include m+'/left_navig.html' }}
>> {{pass}}
>>
>>
--
---
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.