Skip to content

Instantly share code, notes, and snippets.

@simonbaird
Created December 10, 2011 13:50
Show Gist options
  • Save simonbaird/1455210 to your computer and use it in GitHub Desktop.
Save simonbaird/1455210 to your computer and use it in GitHub Desktop.
Embedded python?
<%#
#
# Embedded python?
#
# See:
# http://mail.python.org/pipermail/python-ideas/2011-December/013003.html
# http://readthedocs.org/docs/ncoghlan_devs-python-notes/en/latest/pep_ideas/suite_expr.html
# and related discussion:
# http://mail.python.org/pipermail/python-ideas/2011-December/thread.html#13003
#
# (Updated to add the new :} closing brace syntax and add a couple of missing
# close braces before the elses).
#
# Some background:
#
# I recently was doing some Django coding for the first time. I discovered
# that I needed to learn a templating language in order to write templates in
# Django.
#
# So I'm a Ruby guy. In ruby we have ERB. You don't have to 'learn' ERB since
# ERB is ruby. You never have to wonder how to do something in a template, or
# if a particular thing you want to do is possible. So the answer to "can it
# be done?" is always yes, and the question "how is it done?" never comes up,
# since, again, it's ruby.
#
# As a ruby programmer looking at python, the idea of needing to learn a
# templating language is a strange concept. And the fact that there
# are many different and competing python templating languages feels like a
# sign that something isn't right.
#
# As has been stated already, python's significant whitespace, for all its
# benefits, is largely what makes embedded python impractical. (See the first
# anti-example here: http://wiki.python.org/moin/Templating ). The proposal
# might change this, and I think that would be a good thing.
#
# (The following is to get a feel for what embedded python might look like).
#
%>
<% if danger_level > 3 {: %>
<div class="alert"><% if danger_level == 5 {: %>EXTREME <% :} %>DANGER ALERT!</div>
<% :} elif danger_level > 0 {: %>
<div>Some chance of danger</div>
<% :} else {: %>
<div>No danger</div>
<% :} %>
<% for a in ['cat', 'dog', 'rabbit'] {: %>
<h2><%= a %></h2>
<p><%= describe_animal(a) %></p>
<% :} %>
@simonbaird
Copy link
Author

You are correct. Being able to write arbitrary code in a template doesn't mean you should. And in practice it's not hard to learn a templating syntax.

But imagine a developer doing a 10 minute 'Hello World' tutorial with a python based framework like Django. When it gets to the part about templates she effectively must learn a templating language with a new unfamiliar syntax just to display hello world in a browser. I don't think it should be that way. Why not have embedded python?

@FND
Copy link

FND commented Dec 14, 2011

And then, after the initial playful phase, do you take away the Python-based templating and move to a more (positively) constrained one?

@simonbaird
Copy link
Author

At the point when a developer knows why they want to use a non-standard[1] templating engine they are qualified to choose to do so.

[1] As devil's advocate, I will refer to any python templating language that isn't embedded python as "non-standard". ;)

@FND
Copy link

FND commented Dec 15, 2011

Fair enough - I'm all for choice (and rope). Plus it's an interesting intellectual exercise.

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