[web2py] Re: Is there any way to preprocess a view before its rendered?

2019-02-08 Thread Alfonso Serra
This would be a solution:

At gluon/templates.py line 790, poll for "*response.parseview*" which is a 
custom function that will get the template text and return a modified one:


else:
text = filename.read()

response = context.get("response")
if response and response.parseview:
text = response.parseview(text)

text = to_native(text)
# Use the file contents to get a parsed template and return it.
return str(TemplateParser(text, context=context, path=path, lexers=
lexers, delimiters=delimiters)


Create the custom function returning text:
def js_template_fix(text):
return text.replace(r"\{{", "")

And at a controller:
def mycont():
response.parseview = fix_js_templates
return locals()

Not sure if it will work when the views are compiled, but for now this is 
good enough.
Thanks again.

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Is there any way to preprocess a view before its rendered?

2019-02-08 Thread Alfonso Serra

Those are all good alternatives. ill investigate the js option but cant 
process a view with js delimiters as \{{ as they will be interepreted by 
python.

response.render is not documented but found an example at 
http://web2py.com/books/default/chapter/29/08/emails-and-sms?search=response.render#Using-the-template-system-to-generate-messages
 
easy enough to understand.

Ill read how response.render works and perhaps implement the callback 
functionality, so the view is not read twice per request. Ill post the 
solution once i have it.

I cant thank enough all the help you are offering on this forum, always 
providing quality answers.

King Regards.



-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Is there any way to preprocess a view before its rendered?

2019-02-08 Thread Anthony
It's just a file, so read it in, do whatever you want with it, and then 
call response.render() with the results. A simpler approach is to see if 
you can use alternative delimiters with your JS library. If the library 
doesn't allow that, you can use alternative delimiters in the original 
code, render the view with response.render(), and then replace all the 
alternative JS delimiters in that output before returning it to the browser.

Anthony

On Friday, February 8, 2019 at 8:10:09 AM UTC-5, Alfonso Serra wrote:
>
>
> Hi.
>
> The question would be if theres any way to run a function taking a 
> composed unprocessed view as argument to return a modified one, before its 
> executed and rendered.
>
> The problem im trying to solve is because handlebars, jquery or knockout 
> js are using the same delimiters as web2py.
>
> Im aware of response.deilimiter = ("") but would like to preserve 
> web2py defaults that im used to.
>
> Example:
> A jquery template looks like:
> 
> 
    > \{{each tags}} >
  • > >
    > Edit > Delete >
    >
  • > \{{/each}} >
> > > by providing the view to a custom prepocessor i could replace > automatically the js brackets starting by "\{{" for the python counterpart > like: > {{=XML("{{")}}each tags{{=XML("}}")}} > > ... > {{=XML("{{")}}each{{=XML("}}")}} > Im my opinion, this would be a useful feature for the already wonderful > web2py templating engine. > > So yes, is there currently a way to achieve this? > > Thank you very much. > King Regards. > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.