Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>
<% :} %>
@cdent

This comment has been minimized.

Copy link

commented Dec 13, 2011

It's been demonstrated time and time again that having language constructs in in templates encourages bad separation. So I have to ask: Why you want to have embedded python?

As far as templating in python goes, the one I like is Jinga2, which is a bit like the stuff in django, but more.

@simonbaird

This comment has been minimized.

Copy link
Owner Author

commented Dec 14, 2011

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

commented Dec 14, 2011

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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.