Giter Site home page Giter Site logo

jinja2modern's Introduction

Jinja2 Modern

Jinja2 tags for modern web. Includes sass, scss, less, coffee, uglify. Can be easily extended for any command line tools. Preprocessors not running if source files not changed.

Instalation

pip install jinja2modern

Usage

In your project directory create settings.py, guess you already have it, with settings:

Basic settings:

#home path
JINJA2MODERN_HOME = os.path.realpath(os.path.dirname(__file__))
#media path relative to home directory
JINJA2MODERN_MEDIA_PATH = 'media'
#media url
JINJA2MODERN_MEDIA_URL = '/media'

Engines

There are several preprocessor engines that you will need in your project. Here is how you can get them:

JINJA2MODERN_ENGINES = {
    'coffee': '/path/to/coffee/bin/coffee',
    'uglify': '/path/to/uglify/bin/uglifyjs',
    'lesscss': '/path/to/lesscss/bin/lessc',
    'sass': '/path/to/sass/bin/sass',
}

Templates

You always can overide basic templates by placing it in tags subdirectory in your jinja2 environment template directory

####js.html: <script src="{{ file_link }}"></script>

####css.html:

Tags

Less, Sass, Scss

All of this template code:

{% less "style.css" %}
    less/style.less
{% endless %}
{% less "less/style.less" %}

will render tags/css.html, for default template: <link rel="stylesheet" type="text/css" href="/media_url/css/style.css" />

Of course you can specify out path and file name

{% less "path/to/style.css" %}
    less/style.less
{% endless %}

this will render tags/css.html, for default template: <link rel="stylesheet" type="text/css" href="/media_url/path/to/style.css" />

Javascript

Js tag just copy files to media path

{% js "js/main/main.js" %}

this will render tags/js.html, for default template: <script src="/media_url/js/main.js"></script>

Advanced js tag using:

You can specify librarys for using in your templates by set this in your settings.py

JINJA2MODERN_JS_LIBS_PATH = 'js/libs'
JINJA2MODERN_JS_LIBS = {
    'jquery': {
        'src': 'js/lib/jquery*.js',
        'template': 'js/jquery.html'
    },
}

and then just use

{% js "jquery" %}

this will render template js/jquery.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="{{ file_link }}"><\/script>')</script>

Coffee script

All of this template code:

{% coffee "js/main/main.coffee" %}
{% coffee "main.js" %}
    js/main/main.coffee
{% endcoffee %}
{% coffee "main.js" %}
    js/main/main1.coffee
    js/main/main2.coffee
    js/main/main3.coffee
{% endcoffee %}

will render tags/js.html, for default template: <script src="/media/url/js/main.js"></script>

and of course you can specify out path and file name like for less tag.

UglifyJS

All of this template code:

{% uglify "js/main/main.js" %}
{% uglify "main.js" %}
    js/main/main.js
{% enduglify %}
{% uglify "main.js" %}
    js/main/main1.js
    js/main/main2.js
    js/main/main3.js
{% enduglify %}

will render tags/js.html, for default template: <script src="/media/url/js/main.js"></script>

Advanced coffee and uglify tag using:

In this case uglify will run on every template rendering

{% coffee "main.js" %}
    js/main/main1.coffee
    js/main/main2.coffee
    js/main/main3.coffee
{% endcoffee %}
{% uglify %}
    /media/url/js/main.js
{% enduglify %}

Coffee tag will produce /media/url/js/main.js and render tags/js.html template. Than uglify will compress this file and will not render any templates.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.