Skip to content

Instantly share code, notes, and snippets.

@i5ar
Last active August 11, 2019 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i5ar/71ccce5e20edf5846908c86d00f4d6a3 to your computer and use it in GitHub Desktop.
Save i5ar/71ccce5e20edf5846908c86d00f4d6a3 to your computer and use it in GitHub Desktop.
Template language

Fox

Fox template language.

Features

What makes Fox template language special.

Indentation based

The indentation based syntax allows you to easily read and write code while keeping the source code well formatted.

Powerfull and easy

The full Python API out of the box:

span 'mario'.capitalize()

Safe

Like in Django you have a few keywords protectiong you from errors (e.g. csrf_token).

Use

Keywords

Start each line with an HTML tag, a Fox keyword or a string.

Fox keywords:

  • autoescape;
  • block;
  • comment or #;
  • csrf_token;
  • cycle;
  • debug;
  • extend;
  • filter;
  • firstof;
  • include;
  • load;
  • lorem;
  • now (for datetime.datetime.now);
  • regroup?
  • spaceless;
  • url;
  • verbatim;
  • widthratio.

Special keywords: - frag or :.

Variables

You can either use concatenation, string template or format.

Tree

The statement always start with an HTML tag, a Fox keyword or a Python comment (#) so the keyword del and the HTML tag del don't conflict:

  • tag (e.g. div)
    • string (e.g. "Hello, world!")
    • code (e.g. f"Hello, {name}")
    • attribute (e.g. src)
  • keyword (e.g. frag)
    • code
  • comment (e.g. # TODO: Foo.)

Django Template Language (DTL) VS Fox

DTL Fox

<span>{{ value|default:"nothing" }}</span>

span value or "nothing"
{% load static %} <br> <img src="{% static "i/hi.jpg" %}" alt="!"> load static img src=(static 'images/hi.jpg') alt='Hi!'
<p> {% with total=business.employees.count %} <span>{{ total }} employee{{ total|pluralize }}</span> {% endwith %} </p> frag total = business.employees.count # FIXME: Global scope. span f'{total} employee{total.pluralize()}'

TODO

Django keywords (e.g. load, cycle, [static](https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#std:templatetag-static), [url](https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#url)) can't be methods! Django filter are used like methods.

load static # Django load
head
link rel='stylesheet' href='style.css'
title 'Hello, world!'
# this is a comment
body class=('auth' if authenticated else 'anon')
div id='content' class='wrap'
form action='./submit.py'
csrf_token
input type='text' value='foo' name='bar'
input type='submit' value='Submit'
img src='path/to/company-logo.png' alt='Company Logo' # TODO: Django static
a href='./index.html' 'Administration' # TODO: Django url
p 'Lorem ' + ipsum # concatenation
p '''
Lorem $ipsum
''' # string template
p f'Lorem {ipsum}' # format
p 'Hello, ' + user.username if user.is_authenticated
ul class='words'
li class=w '' for w in words # TODO: Django cycle
p 'Hello, '
span 'mario'.capitalize()
p unset or 'nothing' # default value
p len('Egg and spam')
unset = None
ipsum = "Ipsum"
words = ['foo', 'bar']
@i5ar
Copy link
Author

i5ar commented May 17, 2019

The statement always start with an HTML tag, a Fox keyword or a Python comment (#) so the keyword del and the HTML tag del don't conflict.

@i5ar
Copy link
Author

i5ar commented Jul 11, 2019

Conflicting HTML attributes:

  • async (keyword);
  • class (keyword);
  • dir;
  • for (keyword);
  • id;
  • list;
  • max;
  • min;
  • open;
  • type.

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