2

Now, I put some common javascript files in PIPELINE_JS settings. But I also want to load more specific js files based on each individual template. I know I can simply add a script tag to include corresponding js files in each template, but that would add additional request to server, which is not a good idea I think. The ideal way I want to do is to use PIPELINE to load, minimize and combine different js files based on different templates. Is there any way to do this?

chaonextdoor
  • 5,019
  • 15
  • 44
  • 61

1 Answers1

2

Yes, It is very simple:

PIPELINE_JS = {
    'stats': {
        'source_filenames': (
          'js/jquery.js',
          'js/application.js',
        ),
        'output_filename': 'js/stats.js',
    },
    'logged_in': {
        'source_filenames': (
          'js/jquery.js',
          'js/collections/*.js',
          'js/application.js',
        ),
        'output_filename': 'js/logged_in.js',
    },
    #and so on..
}

and while including in the template,

{% load compressed %}
{% compressed_js 'stats' %}

In another template where you wish to import the other target,

{% load compressed %}
{% compressed_js 'logged_in' %} {# or whichever you wish to import #}

Read the documentation for better understanding.

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Does PIPELINE provide a inheritance structure? I mean as in your example, js/jquery.js should be included in every template, it's a little bit tedious to manually include it in every target. – chaonextdoor Jun 12 '13 at 19:29
  • In your base.html, have a block section named `pipeline` and then where you want the specific targets, override the block in those specific templates. – karthikr Jun 12 '13 at 19:32
  • What I want to do is not to override the whole block. I just want to append some new js files to the target in original or base template and make it as a whole new compressed file in pipeline. – chaonextdoor Jun 12 '13 at 19:56
  • 1
    they use `{{block.super}}` at the beginning of the block in the derived html file – karthikr Jun 12 '13 at 19:59