backslant or python templates engines design guidelines

12
PYTHON TEMPLATES DESIGN GUIDELINES RIGHT WAY TO MAKE THINGS BAD

Upload: -

Post on 13-Apr-2017

219 views

Category:

Technology


1 download

TRANSCRIPT

PYTHON TEMPLATES DESIGN GUIDELINES

RIGHT WAY TO MAKE THINGS BAD

DO YOUR OWN JINJA2PYTHON TEMPLATES GUIDELINES

• make ad-hoc loading machinery (template loaders)

• reinvent semi programming language (filters, includes, templatetags)

• hide functions and prohibit direct usage

• make it hard to use with other template engines

• don’t help to generate HTML

FROM DJANGO.TEMPLATE.LOADER IMPORT RENDER_TO_STRING

TEMPLATE LOADING

• reinvent the wheel everytime

• dont use python one

FiltersAre functions

MacrosAre functions

IncludesAre functions?

INTEROPERABILITYCOMPATIBILITY

• We want to use this new fast and shiny new templates!

• Rails way — ok, `gem install new_shiny_project`, start to use

• Python way — no way, we need to rewrite everything! Pain and suffer, cry everywhere.

• Why? Jinja2 awaits that loaders return text, not ready to use function.

BACKSLANTWhen someone broke rules

ALTERNATIVE GUIDELINESBACKSLANT

• use python import to load templates

• functions for everything

• generate HTML from jade/slim/haml like lang

• play nice with jinja2

IN ACTIONBACKSLANT

html body a(href="") 'ABOUT'

<html> <body> <a herf="/genious/link">ABOUT</a> ... </body> </html>

IN ACTIONBACKSLANT

- def input(name, label): div.pretty-input-sm2 label(for=(name)) = name input(name=(name))

- def render(about): html body a(href="") ‘ABOUT' = about form == input(‘username’, ‘Your name’)

import backslantbackslant.install_loader()

from project.templates.main import render

@app.route(‘/‘)def main(req): return backslant.string( render( about=“HELLO” ) )

JINJA2 INTEROPERABILITYBACKSLANT

- from backslant.flask import extend_jinja2, include_jinja2

:call extend_jinja2('layouts/base.html') - def content(ctx): h2 "And something about other template engines"