Created
July 7, 2020 10:08
-
-
Save ivanteoh/5cb30be54b16c7ea0857ad7e9289d559 to your computer and use it in GitHub Desktop.
Python Web Templating Battle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html xmlns="http://www.w3.org/1999/xhtml"> | |
... | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul tal:switch="len(items) % 2"> | |
<li tal:case="True">odd</li> | |
<li tal:case="False">even</li> | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div tal:repeat="item range(10)"> | |
<p tal:condition="repeat.item.even">Even</p> | |
<p tal:condition="repeat.item.odd">Odd</p> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div tal:repeat="item range(10)"> | |
... | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?python import pdb; pdb.set_trace() ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--! This comment will be dropped from output --> | |
<!--? This comment will be included verbatim --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chameleon import PageTemplate | |
PageTemplate.expression_types['upper'] = uppercase_expression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chameleon import PageTemplateFile | |
from chameleon.tales import PythonExpr | |
class MyPageTemplateFile(PageTemplateFile): | |
expression_types = { | |
'python': PythonExpr, | |
'upper': uppercase_expression | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div tal:content="upper: hello, world" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if is_logged_in %}Thanks for logging in!{% else %}Please log in.{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div tal:content="'hello, world'.upper()" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
path = os.path.dirname(__file__) | |
from chameleon import PageTemplateLoader | |
templates = PageTemplateLoader(os.path.join(path, "templates")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template = templates['hello.pt'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chameleon import PageTemplate | |
template = PageTemplate("<div>Hello, ${name}.</div>") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> template(name='John') | |
'<div>Hello, John.</div>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ my_variable }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ user.username|e }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# this is comment #} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for user in users if not user.hidden %} | |
<li>{{ user.username|e }}</li> | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lower(value): # Only one argument. | |
"""Converts a string into all lowercase""" | |
return value.lower() | |
register.filter('lower', lower) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jinja2 import Environment, PackageLoader | |
env = Environment(loader=PackageLoader('yourapplication', 'templates')) | |
template = env.get_template('mytemplate.html') | |
print template.render(the='variables', go='here') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<theme /> | |
<notheme /> | |
<replace /> | |
<before /> | |
<after /> | |
<drop /> | |
<strip /> | |
<merge /> | |
<copy /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bin/diazocompiler -o theme.xsl -r rules.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bin/diazorun -o output.html -r rules.xml content.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ somevariable|lower }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%! | |
import mylib | |
import re | |
def filter(text): | |
return re.sub(r'^@', '', text) | |
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mako.template import Template | |
mytemplate = Template(filename='/docs/mytmpl.txt') | |
print mytemplate.render() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mako.template import Template | |
mytemplate = Template( | |
filename='/docs/mytmpl.txt', | |
module_directory='/tmp/mako_modules') | |
print mytemplate.render() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template import Context, Template | |
t = Template("My name is {{ my_name }}.") | |
c = Context({"my_name": "Ivan"}) | |
t.render(c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def some_view(request): | |
# ... | |
return render_to_response('my_template.html', | |
my_data_dictionary, | |
context_instance=RequestContext(request)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p namespace-prefix:command="argument"> ... </p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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