django templating more than just - meetup

25
Django Templating: More Than Just Blocks Presentation by Christine Cheung http://www.xtine.net @plaidxtine

Upload: others

Post on 27-Mar-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Django Templating:More Than Just Blocks

Presentation by Christine Cheunghttp://www.xtine.net

@plaidxtine

About the Presenter

● Front End Developer○ Django Advocate

● President of PyLadies○ Python diversity organization

● Speaker at DjangoCon 2011○ Best Practices for Front End Developers

● Trombonist

What I'm Going to Talk About

● Intro to Templating○ the basics

● Templating Best Practices○ structure and tools○ blocks / includes / variables

● Extending Templates○ tag and filter creation○ urls, internationalization, forms

● Template Loading○ replace templating engine○ optimization

Django Templating 101

● This is how your website is shown to the end user○ HTML files

● Balance between power and ease○ subset of the Python language○ what's not built in, you can extend

● Design○ templates to be touched by designers and front-end

developers○ not necessarily for back-end

The Basics

● Template file/folder structure I use○ put all templates in one place○ <insert example image of file structure>

● Start with a base.html○ also can do base_<category>.html

● Extend it with subpages○ block content

Style Guide

While template files are more HTML than Python, it's still good to follow a style guide in the vein of PEP 8.● indent like Python● don't mix tabs and spaces (pick one!)● load all template tags up top● use {# template comments #} over

HTML <! -->

Handy Tools

Django template theme for your IDE

django-debug-toolbardjango-template-utils

print out tag/filter reference guide

Blocks

Blocks I tend to use:● title● meta_tags / robots● extra_head● content● extra_js

Block Practices

End your blocks{% block title %} foo {% endblock title %}

Use super when needed{{ block.super }}displays the parent block data

Don't "over block"

Includes

You can include templatesGood for snippets of template you don't want to repeat like a sidebar or widget

Try not to use them often to avoid dependency crush* Don't have an include in an include *

Variables

Tend to be objects passed from a view

● Modify the variables with filters● Loop through them with tags● If nothing suits your needs

○ create your own or modify with JavaScript

Security

By default, Django's security from the template area is rather secure...

However when using the "safe" tag or if you've autoescaped a block ...** make sure you sanitize the data **

Filter Examples

<insert examples here>

Tags

Subset of Python used in templates to iterate over or perform logic with variables.

You can create your own tags for your own needs

Tag Creation

<insert basic example>

<inclusion tag>

Classy-tags

DO NOT

<insert example>

do not make tag that runs pythondefeats the purpose of template language* dangerous* difficult to support

URLs

Use {% url %} tagsdefine them in urls.pybetter markup, less hassle when they change

Use a {{ STATIC_URL }} as tempting as it is to just write "/static/img/foo.png"

Internationalization

http://codeonfire.cthru.biz/?p=103

http://www.multitasked.net/2010/sep/19/django-i18n-tricks/

http://www.technomancy.org/python/django-template-i18n-lint/

Forms

For a lot of form action, use a package● django-floppyforms for HTML5 support● django-crispy-forms for DRY forms, replaces

uni-form

Use a form template{% include form.html %}as_ul makes more sense than p/table

More Than One Way to Skin a Cat

Even though template tags and filters may seem limited, there are multiple ways to accomplish the same task

There's no ultimately right or wrong way

Use one that suits you or your team

Null Checks

{% if entry.title %} {{ entry.title }} {% else %} {{ entry.short_title }} {% endif %}

This also works:{% firstof entry.title entry.short_title %}

New Additions in Django 1.4

Custom Project and App templatesstartapp / startproject -- templatecombine with your favorite boilerplate

Else If!{% elif %}

Replacing the Templating Engine

● You can replace the built-in templating engine○ Jinja2, Mako, Cheetah, etc.

● Why?○ More familiar with another template language○ Performance boost○ Different logic control and handling

● You risk "frankensteining" your project.○ Goodbye support

Jinja2 and Django and You

Pros● Functions callable from templates● Loop controls, multiple filter arguments● Slight performance increaseCons● More to dependencies, overhead● Time spent on developing / support● Minimal speed increase● Too much logic in templates

need for speed

django-pancake"flatten" your template files

cache template loaderTEMPLATE_LOADERS = ( ('django.template.loaders.cached.Loader' , ( 'django.template.loaders.filesystem.Loader' , 'django.template.loaders.app_directories.Loader' , )),)

django-compress