修剪Mako输出

时间:2009-01-24 16:56:23

标签: python layout template-engine mako

我非常喜欢在Pylons和其他几个Python框架中使用的Mako模板系统,我唯一的抱怨是WS通过简单的继承方案泄漏了多少。

无论如何要在下面完成,没有创建如此巨大的WS差距......或者像我开始使用base.mako那样打包我的代码?

否则要抓住我想要在下面完成的事情。

Base类似于整个应用程序的所有视图的接口类,布局只是3-4个不同布局文件(表格,纯CSS等)的原型构思,而控制器/动作是一个测试来确保我的想法是理智的。

问题的简短摘要:如何删除我的Mako计划中创建的WS?

更新:不是解决方案,因为它涉及用我的所有mako文件播种    http://www.makotemplates.org/docs/syntax.html#syntax_newline

/base.mako

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head><%def name="headtags()"></%def>${self.headtags()}</head>
  <body>
    <%def name="header()"></%def>${self.header()}${next.body()}<%def name="footer()"></%def>${self.footer()}
  </body>
</html>

/layout.mako

<%inherit file="/base.mako"/>
<%def name="headtags()">
   ${parent.headtags()}
   <script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
</%def>
<%def name="header()">
  <h1>My Blogination</h1>
</%def>
<div id="content">${next.body()}</div>

/controller/action.mako

<%inherit file="/layout.mako" />
<%def name="headtags()">    
    <title> Hello world, templating system is 1 percent done</title>
    ${parent.headtags()}
</%def>
Hello ${c.name}! 

渲染输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>    
    <title> Hello world, templating system is 1 percent done</title>

   <script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>


</head>
  <body>

  <h1>My Blogination</h1>


<div id="content">

Hello Anonymous!

</div>

  </body>
</html>

1 个答案:

答案 0 :(得分:2)

找到我自己的答案 http://docs.makotemplates.org/en/latest/filtering.html

仍然需要一些试验和错误,但使用

t = TemplateLookup(directories=['/tmp'], default_filters=['trim'])

大大减少了空白流血。通过检查已编译的模板并查找只是推送'或类似的任何写入,可以找到额外的节省。

相关问题