Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A Django 1.3+ tag to keep JavaScript out of your templates.

Project Description
$ pip install jsinclude

Example of use:

<!-- template.html -->
{% load jsinclude %}
{% with 31 as age %}
    {% jsinclude widgets/profile.js "name=Jane Doe" age gender=female %}
{% endwith %}
// profile.js
console.log('My name is ' + $jsi.name);
console.log("I'm a " + $jsi.age + ' year old ' + $jsi.gender);

Syntax:

{% jsinclude <path_to_script> [{arg}] %}

Django-tag API

The JSInclude tag has two sections. First argument is the script path.

# Path can be a string..
{% jsinclude literal/path/to/script.js %}
# ..or a context variable.
# context['mypath'] == "my/script/path.js"
{% jsinclude mypath %}

The second argument section is where you can pass in your template data into the JavaScript file. There are three variable format options, and there is no limit to the number of varialbes you can pass in.

<template variable>

Any context variable can be directly provided as a tag argument. JSInclude will preserve the variable’s name in the JavaScript $jsi object.

# context['color'] == 'red'
{% jsinclude shape.js color %}

name=value

Static values that do not contain spaces can be set as a simple key=value pair.

{% jsinclude shape.js type=square %}

“name=long value”

Static values that contain spaces must be wrapped in quotes. Django 1.3 does not support arbitrary arguments in template tags, so the entire key=value pair must be wrapped in quotes.

{% jsinclude shape.js "label=my red square" %}

JavaScript API

JSInclude exposes a single JavaScript object that contains all tag arguments. This object is scoped only to the included script, so it will not remain in scope after the script has executed and does not alter global namespace at any time.

window.myglobal = 1234; // Works as expected.
var myotherglobal = 1234; // Scoped only to the jsincluded script.

$jsi

JSInclude exposes the $jsi object scoped only to the included template.

$jsi.<name>

The $jsi object contains any Django template variables preserving original naming. Static data can be loaded into the $jsi object by the name=value or "name=long value" tag argument conventions.

Configuration

# settings.py
JSINCLUDE_STATIC_PATH = 'required/path/to/static/files'
JSINCLUDE_WRAP_PATH = 'optional/path/to/custom.template'
# Built-in TEMPLATE_DEBUG will enable/disable minification.
TEMPLATE_DEBUG = True

Dependencies


Release History

Release History

This version
History Node

1.2.4

History Node

1.2.3

History Node

1.2.2

History Node

1.2.1

History Node

1.2.0

History Node

1.1.8

History Node

1.1.7

History Node

1.1.5

History Node

1.1.4

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.3

History Node

1.0.2

History Node

0.0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
jsinclude-1.2.4.zip (16.3 kB) Copy SHA256 Checksum SHA256 Source Oct 6, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting