Support for modern webdev practices/technologies #294

Open
wants to merge 1 commit into
from

Conversation

Projects
None yet
6 participants

jannon commented Jul 30, 2012

Okay here is a pull request that repackages and cleans up a couple earlier requests

What's in this pull request

  • Let tag arguments after kind (css|js) be specified in any order
  • Use more standard 'as ' syntax to name the code block
  • Add ability to specify options in the tag
  • Ability to group files before processing
  • Ability to redirect tag output to template context variable

There are tests and documentation updates as well as the code changes

Tag Argument Order

There's not really any need to enforce a certain arrangement of arguments in the tag so let's just let people specify them however they want after the kind parameter. So now all of these are valid:

{% compress js inline as main %}

{% compress js as main file %}

{% compress css group_first=true as base %}

Using 'as <name>'

Lots of things in python and django use this syntax, so it would be nice to do so as well

Specifying options in the tag

Just like the with tag in django, you can specify an arbitrary number of keyword/value arguments to the tag. These will get put into a dictionary and passed to the Compressor classes. Also, the default compressors add these to the options that they pass the precompilers and filters. One of the main motivations for this was to prepare for being able to specify that files should be grouped first before, say, lessc precompilation (as discussed in #30) Currently supported options:

  • group_first - if 'group_first=true' is specified, files of like type will be concatenated before being handed to precompilers/filters
  • deferred - if 'deferred=true' is specified AND 'as ' is specified the results of the template tag are stored in a template context variable

Concatenating files before processing (fixes #30)

As mentioned above, using the group_first=true tag options will concatenate files before processing. This is useful for having django_compressor work for best practices like separate mixins.less and page.less files

Template context variable (fixes #249)

As mentioned above, using 'deferred=true' and 'as " in the template tag will cause the output to be placed in a template context variable rather than rendered immediately. This is useful for capturing the results of the compression (the url) for use with asynchronous resource loaders (e.g. yepnope.js, require.js, etc.). In fact, if the output mode is 'file', the 'deferred' option set the template variable to be just the compressed url (without 'script' tag). If the mode is 'inline' the template variable just contains whatever the output would have been normally

@jannon jannon Support for modern webdev practices/technologies:
- Ability to group files of like type before processing.  Useful for having django_compressor work for things like separate mixins.less and page.less files
- Ability to redirect teh output of teh template tag to a template context variable.  Useful for using the compressed Url with asynchronous resource loaders (e.g. yepnope.js, require.js, etc.)
- Ability to specify options in the template tag that are passed through to the compressors, precompilers, and filters.  Currently supported options:
	group_first - if 'group_first=true' is specified, files of like type will be concatenated before being handed to precompilers/filters
	deferred - if 'deferred=true' is specified AND 'as <name>' is specified the results of the template tag are stored in a <name> template context variable
- Naming a tag by using 'as <name>' like other pythonic and django-y things
- Can specify all tag arguments after the type  (css|js) in any order
cd64b11

This pull request fails (merged cd64b11 into 41d5e52).

Deraen commented Aug 3, 2012

This does not work when using offline compression:

    {% compress js deferred=true as js_history %}
    <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.history.js"></script>
    {% endcompress %}

    {% compress js %}
    <script type="text/javascript" src="{{ STATIC_URL }}js/yepnope.1.5.4-min.js"></script>
    <script type="text/javascript">
    ...
    yepnope('{{ js_history }}');
    ...
    </script>
    {% endcompress %}

OfflineGenerationError: You have offline compression enabled but key "9a67e822d7f98c94f109bb4075fb8ad5" is missing from offline manifest. You may need to run "python manage.py compress".

This is because when running compress command, the second block will be rendered without js_history variable and will get different hexdigest than when rendering template.
This can be fixed by removing context.push()/pop() from management/commands/compress.py lines 269 and 283, then context wont be reseted between different compress blocks and second block can access variable from first block.

Owner

diox commented Aug 3, 2012

FYI : I like this pull request a lot, haven't checked all the code yet but I'd love to integrate it. We're about to push a new release, I'll take a closer look once that is done.

@Deraen problem needs to be fixed however, and a testcase added. I'm not sure simply removing the context push/pop is the right path, we might need something a little more clever, as we don't want to pollute the context for all compress blocks.

Owner

diox commented Oct 30, 2012

Release was done a few weeks ago, so let's try to work on merging this.

@jannon : can you look at the travis test failures and see if @Deraen's problem can be fixed easily ? I'm available on IRC to discuss this if needed.

@kingtut kingtut commented on the diff Jan 19, 2013

compressor/templatetags/compress.py
def render_output(self, compressor, mode, forced=False):
return compressor.output(mode, forced=forced)
+ def render_result(self, content, context, deferred, mode):
+ if deferred:
+ if mode == OUTPUT_FILE:
+ match = re.search(r'(?:src|href)=["\']([^"\']+)', content)
@kingtut

kingtut Jan 19, 2013

This would be better as re.findall to output a list of all files for debug (or compress=False) mode.

Owner

diox commented Apr 19, 2014

@jannon : most of this PR probably still applies, but I can't merge this without fixes for the test failures (and conflicts, since this is old).

k-funk commented Jun 1, 2016

Would really like to see this go out if you still have time @jannon . If not, let us know and hopefully someone else can work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment