mako

Using from __future__ import in Mako template

匿名 (未验证) 提交于 2019-12-03 00:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have <%! from __future__ import division %> at the very top of my template file. I get the error: SyntaxError: from __future__ imports must occur at the beginning of the file What's the correct way to do this? 回答1: You cannot use from __future__ import statements in Mako templates. At all. This is because a Mako template is compiled to a python file, and in order for this to work it sets up some initial structures at the top of that python file: # -*- encoding:ascii -*- from mako import runtime, filters, cache UNDEFINED = runtime.UNDEFINED

How do you debug Mako templates?

混江龙づ霸主 提交于 2019-12-02 16:36:24
So far I've found it impossible to produce usable tracebacks when Mako templates aren't coded correctly. Is there any way to debug templates besides iterating for every line of code? Triptych Mako actually provides a VERY nice way to track down errors in a template : from mako import exceptions try: template = lookup.get_template(uri) print template.render() except: print exceptions.html_error_template().render() Looking at the Flask-Mako source, I found an undocumented configuration parameter called MAKO_TRANSLATE_EXCEPTIONS . Set this to False in your Flask app config and you'll get nice

How to use dicts in Mako templates?

ぐ巨炮叔叔 提交于 2019-12-01 11:23:18
Whenever I pass a complicated data structure to Mako, it's hard to iterate it. For example, I pass a dict of dict of list, and to access it in Mako, I have to do something like: % for item in dict1['dict2']['list']: ... %endfor I am wondering if Mako has some mechanism that could replace [] usage to access dictionary elements with simple . ? Then I could write the line above as: % for item in dict1.dict2.list: ... %endfor Which is much nicer, isn't it? Thanks, Boda Cydo. class Bunch(dict): def __init__(self, d): dict.__init__(self, d) self.__dict__.update(d) def to_bunch(d): r = {} for k, v in

How to use dicts in Mako templates?

◇◆丶佛笑我妖孽 提交于 2019-12-01 08:28:49
问题 Whenever I pass a complicated data structure to Mako, it's hard to iterate it. For example, I pass a dict of dict of list, and to access it in Mako, I have to do something like: % for item in dict1['dict2']['list']: ... %endfor I am wondering if Mako has some mechanism that could replace [] usage to access dictionary elements with simple . ? Then I could write the line above as: % for item in dict1.dict2.list: ... %endfor Which is much nicer, isn't it? Thanks, Boda Cydo. 回答1: class Bunch(dict

How can I connect my python script with my HTML file?

人盡茶涼 提交于 2019-11-30 14:17:50
问题 Basically I am getting some data from a webpage, and putting it into an array, I want to output the contents of that array into a table in a HTML file. After some research I found that using a mako template might be the best solution, but I don't understand how to use it? Can any one guide me through the steps or offer a better solution to execute this python script and output its result on the web? import urllib2 import mako from bs4 import BeautifulSoup as BS html = urllib2.urlopen("<link

Mark string as safe in Mako

不羁的心 提交于 2019-11-30 12:07:09
I'm using Pylons with Mako templates and I want to avoid typing this all the time: ${ h.some_function_that_outputs_html() | n } I want to somehow mark the function, or a variable as safe (you can do that in Django) so I don't have to pipe-en all the time. Any ideas? I just found out that if you put a html method in your class, then Mako will just call that method and output whatever it returns in the template. So I did: def __html__(self): return unicode(self) That's basically what h.literal does. According to the mako docs about filtering , you can set the default filters that are applied

How can I connect my python script with my HTML file?

喜欢而已 提交于 2019-11-30 10:13:43
Basically I am getting some data from a webpage, and putting it into an array, I want to output the contents of that array into a table in a HTML file. After some research I found that using a mako template might be the best solution, but I don't understand how to use it? Can any one guide me through the steps or offer a better solution to execute this python script and output its result on the web? import urllib2 import mako from bs4 import BeautifulSoup as BS html = urllib2.urlopen("<link-to-web-page>") soup = BS(html) data = [] for each_course in soup.findAll('li',{'class':'<class-name>'}):

Insert javascript at top of including file in Jinja 2

狂风中的少年 提交于 2019-11-30 05:09:58
In Jinja2, I would like the following to work as it looks like it should, by running: from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('.')) template = env.get_template('x.html') print template.render() Essentially the objective is to coalesce all the javascript into the <head> tags by using a a {% call js() %} /* some js */ {% endcall %} macro. x.html <html> <head> <script type="text/javascript> {% block head_js %}{% endblock %} </script> </head> <body> {% include "y.html" %} </body> </html> y.html {% macro js() -%} // extend head_js {%- block head_js

Mark string as safe in Mako

拜拜、爱过 提交于 2019-11-29 18:43:07
问题 I'm using Pylons with Mako templates and I want to avoid typing this all the time: ${ h.some_function_that_outputs_html() | n } I want to somehow mark the function, or a variable as safe (you can do that in Django) so I don't have to pipe-en all the time. Any ideas? 回答1: I just found out that if you put a html method in your class, then Mako will just call that method and output whatever it returns in the template. So I did: def __html__(self): return unicode(self) That's basically what h

Mako templates using Django template tags

无人久伴 提交于 2019-11-29 07:25:10
Our Django site is built using Mako templates. We want to use a third party project called django-socialregistration , but its template tags use Django's templates. If we used Django templates we could just {% load facebook_tags %} {% facebook_button %} {% facebook_js %} How can I do the same thing in Mako? You can inline strait up python in Mako, but I haven't figured out how to do it that way either. Final Fix <%! from django.template import Template, Context %> <% tpl = "{% load facebook_tags %}{% facebook_button %}{% facebook_js %}" %> ${Template(tpl).render(Context(dict_=dict(request