Flint

Flint is a bare-bones Markdown templating engine written in python. It’s purpose is to lower tha barrier of entry to people who want to code their own simple dynamic web pages.

Templates

Flint templates are based of off a dictionary (a list of key/value pairs).

Single Values

To insert a value, add {key} to the Markdown template where you want the value associated with that key to go.

For instance, if the value for the key username is Foobar, then

1
Welcome, {username}!

will be rendered to pure Markdown:

1
Welcome, Foobar!

which will in turn be converted to HTML:

1
<p>Welcome, Foobar</p>

Lists

To render an iterable value, such as a list, you can use the syntax |key|[[This is a list with the value {*}]] to produce a bulleted list with each occurence of {*} replaced with each of the values from key.

For instance, if the value for todo_list is ["Write code", "Be awesome", "Write more code"], then

1
|todo_list|[[To Do: {*}]]

will produce the Markdown

1
2
3
+ To Do: Write code
+ To Do: Be awesome
+ To Do: Write more code

and the HTML

1
2
3
4
5
<ul>
  <li>To Do: Write code</li>
  <li>To Do: Be awesome</li>
  <li>To Do: Write more code</li>
</ul>

Using Flint

To use Flint in your python code, you need to:

  1. Create a flint object associated with a specific Markdown template.
  2. Add key/value pairs to that flint object.
  3. Render the template and export the HTML.

To illustrate, here is a snippet that renders template.md with the keys username and days_till_birthday:

1
2
3
4
5
6
from flint import Flint

my_flint = Flint("template.md")
my_flint.add_dict({"username": "Foobar", "days_till_birthday": 10})
my_flint.render()
my_flint.exportHTML("birthday.html")

You can either export the rendered HTML to a file by using my_flint.exportHTML("path/to/export.html") or you can get the rendered HTML as a string by using my_flint.getHTML().

Installation

pip install flint-md

Example: Using Flint with Flask

This example Flask app displays the visitor’s name (given in the URL), and the current time and date.

(flask_example.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask
from datetime import datetime
from flint import Flint

app = Flask(__name__)

@app.route('/<name>')
def hello(name):
  today = datetime.today()

  todo_list = ["Be awesome",
               "Make a website",
               "Take over the world"]

  flint = Flint("flask_example.md")
  flint.add_dict({"year": today.year,
                  "month": today.month,
                  "day": today.day,
                  "hour": today.hour,
                  "minute": today.minute})
  flint.add("visitor_name", name)
  flint.add("todo_list", todo_list)
  flint.render()
  return flint.getHTML()

# Handle the case when no name is supplied
@app.route('/')
def root():
  return hello("Visitor")

if __name__ == '__main__':
  app.run()
(flask_example.md) download
1
2
3
4
5
6
7
8
Welcome {visitor_name}!
=======================

Today's date is {month}/{day}/{year}, and it is currently {hour}:{minute}

Here is your todo list:

|todo_list|[[{*}]]

Documentation

The flint class supports a number of methods:

  • __init__(self, path_to_template) creates a new flint object with the template specified.
  • add(self, key, value) adds the key/value pair given to the dict used to render the template.
  • add_dict(self, dict) adds the given dict to the dict used to render the template.
  • render(self) renders the template with the current dict of key/value pairs.
  • getHTML(self) returns the rendered HTML.
  • exportHTML(self, path_to_export) exports the rendered HTML to the specified file.
  • getRenderedText(self) returns the rendered markdown.
  • exportRenderedText(self, path_to_export) exports the rendered markdown to the specified file.
  • get(self, key) returns the value for the specified key.
  • getText(self) returns the raw (unrendered) template.
  • exportText(self, path_to_export) exports the raw (unrendered) template to the specified file.

For more check out the source code on github.

License

This code is released under the MIT License (a copy of which can be found in the github repo)