iamjazzar / django-mako

The first simple, elegant Django Mako library
https://www.ahmedjazzar.com
MIT License
18 stars 10 forks source link
django mako mako-package python python3

django-mako Tests

The simple, elegant Django Mako library Used base engine to create a template rendering class to be used like Django's TemplateView class. To understand how to use it, read Custom backends on django.

Enjoy! This shouldn't be tricky any more.

Installation

To install the package as a requirement in your python environemnt just do

pip install djangomako

Using the library

After installing the package in your python environment, navigate to your project's settings.py and add the following lines in the TEMPLATES variable

TEMPLATES = [
    # ...
    {
        'BACKEND': 'djangomako.backends.MakoBackend',
        'NAME': 'mako',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
        ],
    },
    # ...
]

Template Variables

I passed some template variables to the context if the request objects exists:

  1. CSRF_TOKEN and CSRF_INPUT
    ${ csrf_input }  ## {% csrf_token %} in Django templates.
    ${ csrf_token }  ## {{ csrf_token }} in Django templates.
  2. To access the request:
    ${ request }
  3. To include a static file url:
    ${ static('image.png') }  ## {% static "image.png" %} in Django templates.
  4. To reverse a url in the template:
    ${ url('home') }  ## {% url 'home' %} in Django templates.

Detailed Examples?

An example of how to use this library in Class-Based view and Function-Based Views is inside niceapp app.

Errors?

To test how this engine handles errors, just run theserver and go to this path [/mako]().

Detailed Explanation?

You can find a detailed explanation of how I implemented this library in my blog post named Integrating third-party templates' libraries with Django.

License

The MIT License (MIT) Copyright (c) 2017-2019 Ahmed Jazzar me@ahmedjazzar.com