GithubHelp home page GithubHelp logo

Django templates error about minimize HOT 8 CLOSED

kmturley avatar kmturley commented on June 12, 2024
Django templates error

from minimize.

Comments (8)

kmturley avatar kmturley commented on June 12, 2024 1

I think I have managed to solve it by using a different library:

var gulp = require('gulp'),
      minifyHtml = require('gulp-htmlmin');

gulp.task('optimise.html', function () {
    'use strict';
    return gulp.src('src/**/*.html')
        .pipe(minifyHtml({
            collapseWhitespace: true,
            ignoreCustomFragments: [ (/\{\%[^\%]*?\%\}/g) ]
        }))
        .pipe(gulp.dest('www'));
});

from minimize.

Swaagie avatar Swaagie commented on June 12, 2024

Where in the html do those template directives lives? E.g. inside an element as text or inside a parameter?

from minimize.

kmturley avatar kmturley commented on June 12, 2024

with django you can use them wherever you want, I was using them as an attribute:
{% if bg %}style="background-image: url('{{ bg }}')"{% endif %}

from minimize.

Swaagie avatar Swaagie commented on June 12, 2024

Hmm yeah, I suspect surrounding html/code is probably causing the change. If I run the CLI script directly with the above supplied snippet it seems to work fine ./bin/minimize "{% if bg %}style=\"background-image: url('{{ bg }}')\"{% endif %}". Note that minimize only supports parsing of HTML though, e.g. excluding template directives. You'll have to inspect what the parsed element looks like of htmlparser2 to get a solid understanding of what is going wrong.

from minimize.

kmturley avatar kmturley commented on June 12, 2024

It seems to work with:

./bin/minimize "{% if bg %}style=\"background-image: url('{{ bg }}')\"{% endif %}"
// outputs
{% if bg %}style="background-image: url('{{ bg }}')"{% endif %}

but not with:

./bin/minimize "<a {% if bg %}style=\"background-image: url('{{ bg }}')\"{% endif %}> hey </a>"
// outputs
<a {% if bg %} style="background-image: url('{{ bg }}')" endif>hey</a>

from minimize.

Swaagie avatar Swaagie commented on June 12, 2024

Hmm yeah, little I can do about that. Htmlparser2 probably detects the {% if bg %} as part of the param, as a param can start with 'any' character. But always closes with"or space, and detects that before the closing{% endif %}`. Anyways, minimize is not designed to work with templates, it parser html. The only way to accommodate for this is by providing a custom parser which understands handlebar templates.

from minimize.

kmturley avatar kmturley commented on June 12, 2024

Ya I can't see an option in htmlparser2 to support custom templating language. I guess what I would need is an option to switch htmlparser2 out for another parser such as swig:
http://paularmstrong.github.io/swig/

But by then it might be better to just use another minification library altogether! It's a shame because I use minimize for the static html, would be useful to use it for the dynamic templates too!

from minimize.

Swaagie avatar Swaagie commented on June 12, 2024

htmlparser2 perfectly supports a custom parser and so does minimize, however it requires development. Another option would be to do minification after the template was processed, e.g. only HTML remains

from minimize.

Related Issues (20)

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.