Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created November 3, 2013 10:46
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/7288912 to your computer and use it in GitHub Desktop.
Save podhmo/7288912 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</span>
</%def>
<%def name="messagebox(id)">
<div class="messsage-box" id="${id}"}>
${caller.body()}
</div>
</%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>
<%env:messagebox id="box_1">
this is template page
</%env:messagebox>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<%env:messagebox id="box_2">
this is template page
</%env:messagebox>
<!-- 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_caller2.py
`-- templates
`-- caller2
|-- env.base.mako
|-- env.template.mako
`-- layout.mako
"""
import contextlib
@contextlib.contextmanager
def section(message):
print("## {}".format(message))
print("```html")
yield
print("```")
from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=["./templates/caller2"])
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</span>
</h1>
  <div class="messsage-box" id="box_1"}>

this is template page
  </div>
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>
  <div class="messsage-box" id="box_2"}>

this is template page
  </div>
</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