Awesome Massimo, this is exactly what I need.
On Monday, 25 March 2019 13:02:42 UTC+11, Massimo Di Pierro wrote:
> Hello yng,
>
> You can but you have to render it before you inject it in the template:
>
> from gluon.template import render
>
> def test():
> template = '{{for x in range(2):}}{{=x}}hello<br />{{pass}}'
> dynamicHtml = render(template, context=locals())
> return dict(dynamicHtml = dynamicHtml)
>
> and in view test.html
>
> {{= XML(dynamicHtml) }}
>
> OR
>
> from gluon.template import render
>
> def test():
> template = '{{for x in range(2):}}{{=x}}hello<br />{{pass}}'
> return dict(dynamicHtml = template, render=render)
>
> and in view test.html
>
> {{= XML(render(dynamicHtml, context=globals())) }}
>
> You can specify different context is needed. For example:
>
> def test():
> template = '{{for x in range(a):}}{{=x}}hello<br />{{pass}}' # notice
> (a)
> return dict(dynamicHtml = template, render=render)
>
> and in view test.html
>
> {{ b = 10 }}
> {{= XML(render(dynamicHtml, context={'a': b} )) }}
>
>
> On Sunday, 24 March 2019 11:29:41 UTC-7, yng wrote:
>>
>> Hi Guru's.
>>
>> I'm new to web2py and web development in general.
>> Hope somebody can help me.
>>
>> I want to inject a html markup with python code at run time.
>>
>> controller:
>> def test():
>> dynamicHtml = XML('{{for x in range(2):}}{{=x}}hello<br />{{pass}}')
>> return locals()
>>
>>
>> view: test.html
>> {{=dynamicHtml}}
>>
>>
>> The output is this:
>> {{for x in range(10):}}{{=x}}hello<br />{{pass}}
>>
>> This is what I'm expecting:
>> 1hello
>> 2hello
>>
>> Hope my explanation is clear.
>> What would be the best solution to implement this?
>>
>> Thanks advance,
>> yng
>>
>>
>>
>>
>>
>>
>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
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/d/optout.