How to use django compressor with multiple LESS files that import shared files?

I organized the templates in my Django project so that each page of the site contains a “shared” LESS file, and each page can also specify a different LESS file that contains styles related to the page.

The problem is that I need a LESS file for a specific page in order to be able to refer to variables in a "shared" LESS file. The easiest way to do this, I thought, was to simply move the variable declarations to a separate file that both LESS files could @import.

However, Django applications use separate directories to store their static files. As a result, the file system looks something like this:

- common
   - static
      - css
         - definitions.less
         - common.less
- other
   - static
      - css
         - other.less

Both common.lessand other.lessmust be imported definitions.less. In the case, common.lessit is as simple as:

@import "definitions.less";

Here's how the LESS files are actually included on the page, just in case that helps:

{% load compress %}
{% load static %}

{% compress css %}
    <link href="{% static "css/common.less" %}"
          rel="stylesheet" type="text/less">
{% endcompress %}

What would be the easiest way to ensure that common variables are defined for both LESS files? I want to avoid merging LESS files for several reasons:

  • It removes the benefits of a weakened connection (the ability to deactivate the application without any side effects on the rest of the site).
  • The extra data retrieved for one page is increased because all styles for all applications must be retrieved.
+5
source share
1 answer

- main.less, . . , : .

django STATICFILES_DIRS - , , , , , . https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATICFILES_DIRS

{% compress css %}
{% include "_base_css.html" %}
{% endcompress %}

_base_css.html , main.less .

## _base_css.html
<link rel="stylesheet" type="text/less" href="{{ STATIC_URL }}css/main.less">
<link rel="stylesheet" type="text/less" href="{{ STATIC_URL }}css/about.less">
<link rel="stylesheet" type="text/less" href="{{ STATIC_URL }}css/dashboard.less">

, , , , , . , . , , CSS sitewide - , .

, .

0

All Articles