Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created November 3, 2013 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/7288751 to your computer and use it in GitHub Desktop.
Save podhmo/7288751 to your computer and use it in GitHub Desktop.
<%inherit file="./layout.mako"/>
<%! from datetime import datetime %>
<%def name="title()">
<% color = "#222244" if datetime.now().hour > 19 else "#444477" %>
<span style="color:${color}">title message</span>
</%def>
<%def name="message(content)">
<p>this is message: ${content}</p>
</%def>
<div class="box" style="padding-left:30px; padding-right:30px;">
<%include file="./env.template.mako" args="env=self"/>
</div>
<%page args="env"/>
<h1>${env.title()}</h1>
this is template page
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
${env.message("<this sentence passed by template side.>")}
<!-- start of header @layout -->
<div class="header">
header
</div>
<!-- end of header @layout -->
${next.body()}
<!-- start of footer @layout -->
</div class="footer">
footer
</div>
<!-- end of footer @layout -->
# -*- coding:utf-8 -*-
"""
file structure::
.
|-- sample_caller.py
`-- templates
`-- caller
|-- env.base.mako
|-- env.template.mako
`-- layout.mako
"""
import contextlib
@contextlib.contextmanager
def section(message):
print("## {}".format(message))
print("```")
yield
print("```")
from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=["./templates/caller"])
with section("env template -> base template"):
result = (lookup.get_template("env.base.mako").render())
## cleanup
import re
empty_lines_rx = re.compile("\n{2,}", re.MULTILINE)
print(empty_lines_rx.sub("\n", result))
@podhmo
Copy link
Author

podhmo commented Nov 3, 2013

env template -> base template

<!-- start of header @layout -->
<div class="header">
  header
</div>
<!-- end of header @layout -->
<div class="box" style="padding-left:30px; padding-right:30px;">

<h1>

  <span style="color:#444477">title message</span>
</h1>
this is template page
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>
  <p>this is message: <this sentence passed by template side.></p>
</div>
<!-- start of footer @layout -->
</div class="footer">
  footer
</div>
<!-- end of footer @layout -->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment