You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

17 KiB

Jinja templates

marking up plain text!

What is a Jinja template?

In [1]:
from jinja2 import Template
In [2]:
# To render HTML in a Jupyter notebook ...
from IPython.display import HTML, display
In [3]:
hello = '<h1>Hello <strong>{{ name }}</strong>!</h1>'
In [4]:
template = Template(hello)
In [5]:
template.render?
Signature: template.render(*args, **kwargs)
Docstring:
This method accepts the same arguments as the `dict` constructor:
A dict, a dict subclass or some keyword arguments.  If no arguments
are given the context will be empty.  These two calls do the same::

    template.render(knights='that say nih')
    template.render({'knights': 'that say nih'})

This will return the rendered template as unicode string.
File:      /opt/tljh/user/lib/python3.7/site-packages/jinja2/environment.py
Type:      method
In [6]:
html = template.render(name='XPUB1')
In [7]:
# Print it as HTML (Python style)
print(html)
<h1>Hello <strong>XPUB1</strong>!</h1>
In [8]:
# Print it as rendered HTML (Jupyter style)
display(HTML(html))

Hello XPUB1!


Multiple variables within one template

In [9]:
welcome = '<h1>Welcome <strong>{{ name }}</strong> here in <strong>{{ location }}</strong> through this <strong>{{ channel }}</strong>!</h1>'
In [10]:
template = Template(welcome)
In [11]:
variables = {}
In [12]:
variables['name'] = 'XPUB1'
variables['location'] = 'Rotterdam'
variables['channel'] = 'hotline'
In [13]:
html = template.render(variables)
In [14]:
# Print it as HTML
print(html)
<h1>Welcome <strong>XPUB1</strong> here in <strong>Rotterdam</strong> through this <strong>hotline</strong>!</h1>
In [15]:
# Print it as rendered HTML
display(HTML(html))

Welcome XPUB1 here in Rotterdam through this hotline!


Open templates from a file

In [17]:
example_template = open('jinja-templates-example.html', 'r').read()
In [18]:
template = Template(example_template)
In [19]:
variables = {}
In [20]:
variables['title'] = 'A Vernacular Web'
variables['summary'] = 'When I started to work on the World Wide Web I made a few nice things that were special, different and fresh. They were very different from what was on the web in the mid 90\'s.'
variables['introduction'] = ''' I'll start with a statement like this, not to show off my contribution, but in order to stress that -- although I consider myself to be an early adopter -- I came late enough to enjoy and prosper from the "benefits of civilization". There was a pre-existing environment; a structural, visual and acoustic culture you could play around with, a culture you could break. There was a world of options and one of the options was to be different.

So what was this culture? What do we mean by the web of the mid 90's and when did it end?

To be blunt it was bright, rich, personal, slow and under construction. It was a web of sudden connections and personal links. Pages were built on the edge of tomorrow, full of hope for a faster connection and a more powerful computer. One could say it was the web of the indigenous...or the barbarians. In any case, it was a web of amateurs soon to be washed away by dot.com ambitions, professional authoring tools and guidelines designed by usability experts.

I wrote that change was coming "soon" instead of putting an end date at 1998, for example, because there was no sickness, death or burial. The amateur web didn't die and it has not disappeared but it is hidden. Search engine rating mechanisms rank the old amateur pages so low they're almost invisible and institutions don't collect or promote them with the same passion as they pursue net art or web design.

Also new amateur pages don't appear at such amounts as ten years ago because the WWW of today is a developed and highly regulated space. You wouldn't get on the web just to tell the world, "Welcome to my home page." The web has diversified, the conditions have changed and there's no need for this sort of old fashioned behavior. Your CV is posted on the company website or on a job search portal. Your diary will be organized on a blog and your vacation photos are published on iphoto. There's a community for every hobby and question.

This is why I refer to the amateur web as a thing of the past; aesthetically a very powerful past. Even people who weren't online in the last century, people who look no further than the first 10 search engine results can see the signs and symbols of the early web thanks to the numerous parodies and collections organized by usability experts who use the early elements and styles as negative examples.

Just as clothing styles come back into fashion so do web designs. On a visual level things reappear. Last year I noticed that progressive web designers returned to an eclectic style reincorporating wallpapers and 3D lettering in their work. In the near future frames and construction signs will show up as retro and the beautiful old elements will be stripped of their meaning and contexts.

In the past few years I've also been making work that foregrounds this disappearing aesthetic of the past. With these works I want to apologize for my arrogance in the early years and to preserve the beauty of the vernacular web by integrating them within contemporary art pieces. But this is only half of the job.

Creating collections and archives of all the midi files and animated gifs will preserve them for the future but it is no less important to ask questions. What did these visual, acoustic and navigation elements stand for? For which cultures and media did these serve as a bridge to the web? What ambitions were they serving? What problems did they solve and what problems did they create? Let me talk about the difficult destiny of some of these elements. '''
In [21]:
html = template.render(variables)
In [22]:
display(HTML(html))
<html> <head> </head>

A Vernacular Web

When I started to work on the World Wide Web I made a few nice things that were special, different and fresh. They were very different from what was on the web in the mid 90's.
I'll start with a statement like this, not to show off my contribution, but in order to stress that -- although I consider myself to be an early adopter -- I came late enough to enjoy and prosper from the "benefits of civilization". There was a pre-existing environment; a structural, visual and acoustic culture you could play around with, a culture you could break. There was a world of options and one of the options was to be different. So what was this culture? What do we mean by the web of the mid 90's and when did it end? To be blunt it was bright, rich, personal, slow and under construction. It was a web of sudden connections and personal links. Pages were built on the edge of tomorrow, full of hope for a faster connection and a more powerful computer. One could say it was the web of the indigenous...or the barbarians. In any case, it was a web of amateurs soon to be washed away by dot.com ambitions, professional authoring tools and guidelines designed by usability experts. I wrote that change was coming "soon" instead of putting an end date at 1998, for example, because there was no sickness, death or burial. The amateur web didn't die and it has not disappeared but it is hidden. Search engine rating mechanisms rank the old amateur pages so low they're almost invisible and institutions don't collect or promote them with the same passion as they pursue net art or web design. Also new amateur pages don't appear at such amounts as ten years ago because the WWW of today is a developed and highly regulated space. You wouldn't get on the web just to tell the world, "Welcome to my home page." The web has diversified, the conditions have changed and there's no need for this sort of old fashioned behavior. Your CV is posted on the company website or on a job search portal. Your diary will be organized on a blog and your vacation photos are published on iphoto. There's a community for every hobby and question. This is why I refer to the amateur web as a thing of the past; aesthetically a very powerful past. Even people who weren't online in the last century, people who look no further than the first 10 search engine results can see the signs and symbols of the early web thanks to the numerous parodies and collections organized by usability experts who use the early elements and styles as negative examples. Just as clothing styles come back into fashion so do web designs. On a visual level things reappear. Last year I noticed that progressive web designers returned to an eclectic style reincorporating wallpapers and 3D lettering in their work. In the near future frames and construction signs will show up as retro and the beautiful old elements will be stripped of their meaning and contexts. In the past few years I've also been making work that foregrounds this disappearing aesthetic of the past. With these works I want to apologize for my arrogance in the early years and to preserve the beauty of the vernacular web by integrating them within contemporary art pieces. But this is only half of the job. Creating collections and archives of all the midi files and animated gifs will preserve them for the future but it is no less important to ask questions. What did these visual, acoustic and navigation elements stand for? For which cultures and media did these serve as a bridge to the web? What ambitions were they serving? What problems did they solve and what problems did they create? Let me talk about the difficult destiny of some of these elements.
</html>

Loops & Conditionals (what if? and else?)

In [23]:
names = ['student1', 'student2', 'student3']
In [24]:
welcome_all = '''
{% for name in names %}
    <h1>Welcome <strong>{{ name }}</strong>!
{% endfor %}
'''
In [25]:
template = Template(welcome_all)
html = template.render(names=names)
display(HTML(html))

Welcome student1!

Welcome student2!

Welcome student3!


Multiple templates

In [ ]:
 
In [ ]: