mako

Python lookup function

醉酒当歌 提交于 2020-01-07 03:41:07
问题 I want to set up a lookup function with mako. on top of the template, i have <%! lookup = { 'key': function } %> <%def name="function()"> Output </%def> so i can use it later <%def name="body()"> ${lookup['key']()} </%def> this gives me a function is not a defined error. can i get around this? i know why it doesn't work, as it runs first, before the method gets loaded, but how else would i set this up? 回答1: I can tell you why it isn't working, but I do not have a clean solution at this point.

How can I get a Django Template to render itself within a Mako Template?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 05:30:11
问题 We made the decision quite awhile ago to use Mako Templates in our Django project. We're also supporting Django Templates, since a lot of reusable apps (obviously) assume that Django Templating is available. I've found it possible to render Django Templates from Mako, but I haven't been able to find a way to make it work the other way around. I've just added django-articles to our list of apps, and it uses Django Templating. It assumes that the base.html file is an overriden Django Template.

how to check if request is ajax in turbogears

江枫思渺然 提交于 2020-01-03 05:32:07
问题 How do I go about checking if a request is an ajax request in a controller method in Turbogears? Further, is it possible to return a 'partial' much like in rails or symfony if the request is an ajax request. I know about the json decorator but I need a way to return a partial of a mako template (because I need to format the data and don't want to to do it all in Javascript). For example if I want to return the formatted list for page two of a list of news stories, I do not want to return the

Formatting with mako

坚强是说给别人听的谎言 提交于 2019-12-24 03:24:24
问题 Anyone know how to format the length of a string with Mako? The equivalent of print "%20s%10s" % ("string 1", "string 2") ? 回答1: you can use python's string formatting fairly easily in mako ${"%20s%10s" % ("string 1", "string 2")} giving: >>> from mako.template import Template >>> Template('${"%20s%10s" % ("string 1", "string 2")}').render() ' string 1 string 2' 来源: https://stackoverflow.com/questions/1029965/formatting-with-mako

ImportError: no module named mako.template

蹲街弑〆低调 提交于 2019-12-24 03:06:37
问题 This line of code in my foobar.py file: from mako.template import * results in an ImportError: No module named mako.template How can I fix this? Both help('modules') and repr(mako) in my Python console assures me that the mako module is installed and available. I'm on a Mac by the way, running Python 2.6 for this particular project. As requested, this is the output from running pip freeze Mako==0.8.1 MarkupSafe==0.18 PyYAML==3.10 wsgiref==0.1.2 As requested, this is my sys.path ['', '/Users

Using from __future__ import in Mako template

左心房为你撑大大i 提交于 2019-12-23 11:56:48
问题 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

Mako escaping issue within Pyramid

点点圈 提交于 2019-12-23 09:38:14
问题 I need to put javascript function to mako template. The first argument of this function is string, so I write in my *.mako file (dict(field_name='geom')): init_map( '${field_name}' ); But when I see my html page it loks like: init_map( 'geom' ) How I can disable escaping in this case? Rendering performs the following way: from pyramid.renderers import render render('georenderer/map.mako', template_args) 回答1: You'll need to include the quotes in your expression I think. You can use the json

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5

╄→гoц情女王★ 提交于 2019-12-23 09:26:24
问题 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 537: ordinal not in range(128), referer: ... I always get this error when I try to output my whole website with characters "č". I am using mako templating. What to do? 回答1: The error occurs because somewhere code coerces your unicode template string into a python 2 str ; you need to encode the rendered template into an UTF-8 bytestring yourself: if isinstance(rendered, unicode): rendered = rendered.encode('UTF-8') # rendered

Strip whitespace from Mako template output (Pylons)

旧城冷巷雨未停 提交于 2019-12-21 04:52:12
问题 I'm using Mako + Pylons and I've noticed a horrendous amount of whitespace in my HTML output. How would I go about getting rid of it? Reddit manage to do it. 回答1: I'm not sure if there's a way to do it within Mako itself but you can always just do some post-rendering processing before you serve up the page. For example, say you have the following code that generates your horrendous whitespace: from mako import TemplateLookup template_lookup = TemplateLookup(directories=['.']) template =

In Pyramid, how can I use a different renderer based on contents of context?

可紊 提交于 2019-12-19 02:31:20
问题 I have 3 different product page layouts that I would like to display dependent on the information available about the products. Using traversal I have a class called ProductFinder that grabs all the information. For example the user goes to domain/green/small and ProductFinder will list all products from my DB that are green and small. This list is self.products in the ProductFinder class. In my __init__.py I have added the line: config.add_view('app.views.products', name='') In products.py I