Skip to content

Instantly share code, notes, and snippets.

@ivanteoh
Created July 7, 2020 10:08
Show Gist options
  • Save ivanteoh/5cb30be54b16c7ea0857ad7e9289d559 to your computer and use it in GitHub Desktop.
Save ivanteoh/5cb30be54b16c7ea0857ad7e9289d559 to your computer and use it in GitHub Desktop.
Python Web Templating Battle
{% extends "base_generic.html" %}
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
<h1>{{ section.title }}</h1>
{% for story in story_list %}
<h2>
<a href="{{ story.get_absolute_url }}">
{{ story.headline|upper }}
</a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>
{% endfor %}
{% endblock %}
<html xmlns="http://www.w3.org/1999/xhtml">
...
</html>
<ul tal:switch="len(items) % 2">
<li tal:case="True">odd</li>
<li tal:case="False">even</li>
</ul>
<div tal:repeat="item range(10)">
<p tal:condition="repeat.item.even">Even</p>
<p tal:condition="repeat.item.odd">Odd</p>
</div>
<div tal:repeat="item range(10)">
...
</div>
<?python import pdb; pdb.set_trace() ?>
<!--! This comment will be dropped from output -->
<!--? This comment will be included verbatim -->
import ast
def uppercase_expression(string):
def compiler(target, engine):
uppercased = self.string.uppercase()
value = ast.Str(uppercased)
return [ast.Assign(targets=[target], value=value)]
return compiler
from chameleon import PageTemplate
PageTemplate.expression_types['upper'] = uppercase_expression
from chameleon import PageTemplateFile
from chameleon.tales import PythonExpr
class MyPageTemplateFile(PageTemplateFile):
expression_types = {
'python': PythonExpr,
'upper': uppercase_expression
}
<div tal:content="upper: hello, world" />
{% if is_logged_in %}Thanks for logging in!{% else %}Please log in.{% endif %}
<div tal:content="'hello, world'.upper()" />
import os
path = os.path.dirname(__file__)
from chameleon import PageTemplateLoader
templates = PageTemplateLoader(os.path.join(path, "templates"))
template = templates['hello.pt']
from chameleon import PageTemplate
template = PageTemplate("<div>Hello, ${name}.</div>")
>>> template(name='John')
'<div>Hello, John.</div>'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>My Webpage</title>
</head>
<body>
<ul id="navigation">
{% for user in users if not user.hidden %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{# this is comment #}
<h1>My Webpage</h1>
{{ my_variable }}
</body>
</html>
{% for user in users if not user.hidden %}
<li>{{ user.username|e }}</li>
{% endfor %}
def lower(value): # Only one argument.
"""Converts a string into all lowercase"""
return value.lower()
register.filter('lower', lower)
{% macro render_dialog(title, class=’dialog’) -%}
<div class="{{ class }}">
<h2>{{ title }}</h2>
<div class="contents">
{{ caller() }}
</div>
</div>
{%- endmacro %}
{% call render_dialog(’Hello World’) %}
This is a simple dialog rendered by using a macro and
a call block.
{% endcall %}
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
template = env.get_template('mytemplate.html')
print template.render(the='variables', go='here')
<html>
<head>
<title>My own Diazo</title>
</head>
<body>
<h1 id="title">My own Diazo home page</h1>
<div id="content">
<!-- Placeholder -->
Lorem ipsum ...
</div>
</body>
</html>
<rules
xmlns="http://namespaces.plone.org/diazo"
xmlns:css="http://namespaces.plone.org/diazo/css"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<theme href="theme.html" />
<drop theme="/html/head/title" />
<drop css:theme="#title" css:if-content="#content.wide" />
<replace css:theme-children="#content" css:content-children=".content" />
</rules>
<theme />
<notheme />
<replace />
<before />
<after />
<drop />
<strip />
<merge />
<copy />
<replace css:theme="#details">
<dl id="details">
<xsl:for-each css:select="ul#details > li">
<dt><xsl:copy-of select="a" /></dt>
<dd><xsl:copy-of select="img" /></dd>
</xsl:for-each>
</dl>
</replace>
$ bin/diazocompiler -o theme.xsl -r rules.xml
$ bin/diazorun -o output.html -r rules.xml content.html
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:i18n="http://namespaces.zope.org/i18n"
xmlns:plone="http://namespaces.plone.org/plone"
i18n_domain="plonetheme.custom">
<!-- Register the /++theme++plonetheme.custom/ static resource directory -->
<plone:static directory="static" type="theme" />
</configure>
<%inherit file="base.html"/>
<%
rows = [[v for v in range(0,10)] for row in range(0,10)]
%>
<table>
% for row in rows:
${makerow(row)}
% endfor
</table>
## this is a comment.
<%def name="makerow(row)">
<tr>
% for name in row:
<td>${name}</td>
% endfor
</tr>
</%def>
{{ somevariable|lower }}
<%!
import mylib
import re
def filter(text):
return re.sub(r'^@', '', text)
%>
from mako.template import Template
mytemplate = Template(filename='/docs/mytmpl.txt')
print mytemplate.render()
from mako.template import Template
mytemplate = Template(
filename='/docs/mytmpl.txt',
module_directory='/tmp/mako_modules')
print mytemplate.render()
from django.template import Context, Template
t = Template("My name is {{ my_name }}.")
c = Context({"my_name": "Ivan"})
t.render(c)
def some_view(request):
# ...
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<!--! This comment will be dropped from output -->
<div tal:repeat="item range(10)">
<p tal:condition="repeat.item.even">Even</p>
<p tal:condition="repeat.item.odd">Odd</p>
</div>
<!--? This comment will be included verbatim -->
<div>
<?python import pdb; pdb.set_trace() ?>
<ul tal:switch="len(items) % 2">
<li tal:case="True">odd</li>
<li tal:case="False">even</li>
</ul>
</div>
</body>
</html>
<p namespace-prefix:command="argument"> ... </p>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n">
...
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment