How to run JSLint / JSHint in a file with templates? - django

I'm trying to introduce some tools at work to improve code quality. An obvious solution that I'd used at a previous company was to run jslint before checking in code.
The problem is that we are using Django to do our templating (though, I assume we would have a similar problem with other templating languages).
How is it possible to take code like the below, and have it JSLint/JSHint properly while ignoring the template tags?
var a = { "test" : "test"};
{% comment %}
{% endcomment %}
{{ my_variable }}
window.x = "y";
I've seen this question specifically regarding JSHint, which looks like it could handle some cases, but it doesn't address inlining variables, like {{ my_variable }} above.
Is there any way to ignore certain lines using JSHint/JSLint, or otherwise get the linting to execute correctly?

Much like linting coffeescript with these tools, you're trying to lint content outside it's pervue. Probably easiest to run the template with dummy values, and JSHint that resulting file.
Probably duplicate of How to run JSHint on files with Django template markup in them?

Related

Do I really need to use {% load static %} in Django Templates?

I am using Django 3, the latest version. I have defined the static files directories properly as required.
Currently to display an image, both the following source codes work fine.
Code 1:
<img src="static/img/logo.png">
Code 2:
{% load static %}
<img src="{% static 'img/logo.png' %}">
Since both the code snippets are working and running fine without any problems, I am wondering why not to simply use the way of Code 1 above and refrain from the extra lines of Code 2.
Which one would be beneficial and why? Please guide.
The base/master level templates would work without static tag and maybe considered for performance.
However for other level templates either nested or in app templates or nested urls, it's required to be reference with static as it would minimise chances of error. So for all other templates it is recommended to use static tag.

How can I configure VS Code to work with Prettier HTML formatter?

I am trying to have VS Code format my Django HTML files but I am getting:
There is no document formatter for 'django-html'-files installed.
The solution I found on the web works with Beautify, not Prettier.
How can I make it work with Prettier?
#Tedkovsky's answer might technically address the error you're getting, but once you're past that, you'll see that Prettier will mangle your templates since it tries to break long lines containing template tags like {{ }} and {% %}.
This is because Prettier currently (as of 2021-01-09) doesn't support Jinja or Django templates, and for the time being, it looks like the developers aren't interested in adding this functionality. There are 2 (closed) tickets about it here:
https://github.com/prettier/prettier/issues/5581
https://github.com/prettier/prettier/issues/5754
I wasn't able to find a plugin for it either, so it doesn't look like there's a solution for using Prettier with Django templates.
edit: I've been following this thread in the Django forum about autoformatters for Django templates. Perhaps something might materialize there.
later edit: Looks like djhtml can handle indentation, although it's separate from Prettier. It doesn't do full autoformatting, though.
even later edit: djlint can also be used for formatting templates
In settings.json, try
"[django-html]": {
"editor.defaultFormatter": "prettier"
},

clojure luminus global variable access into template

I'm trying to do a simple thing(at least for common frameworks) using Luminus and Selmer template but I can’t find a way to do it. I would like just to pass values to the base template, something like this:
file: home.html
{% extends "base.html" {:user {{ user }} } %}
​{% block content %}
...
​{% endblock %}
I know that we can do it using render a file, like this
(render-file "html/base.html" {:user user})
But it not looks right, it looks like a weird workaround.
My idea actually is to make some global variables to access anywhere of application like user session, but I didn’t find it in luminus documentation
Well, even when Selmer is cool, most of the web culture in Clojure turns around Hiccup as the template generator. You will use Hiccup with ClojureScript (Reagent - Re-frame) and that is another reason to switch to it. Probably you already have Hiccup installed, check it with:
lein deps :tree | grep hiccup
You will need to create a layout like this one. And uses it in a "controller" like this to build your HTML view.

Django: best practice to minify HTML to improve page speed loading

Its about results in https://developers.google.com/speed/pagespeed/insights/
Probably it is pretty much to just globally wrap whole HTML with spaceless template tag?
{% spaceless %}
<!DOCTYPE html>
<html>
...
</html>
{% endspaceless %}
Or it is not safe and there are some better ways?
I think there are a few options for this. After a quick search I found StripWhitespaceMiddleware and another 3rd party app called django-htmlmin. You would need to make sure that these packages are compatible with your version of python and Django. Also, have a look at the solutions in this question about HTML minification in Django.
Two three solutions I've found:
django.middleware.gzip.GZipMiddleware
http://wiki.nginx.org/HttpGzipModule
http://wiki.nginx.org/HttpStripModule
Gzip should does the same thing - removes unnecessary white spaces before html delivered.
I recommend using a custom template loader, which strips unnecessary whitespace characters. This approach has two advantages:
With the combination with cached template loader, whitespace stripping is done only once during template compilation. On the other hand with {% spaceless %} or middleware approach minification is called every time a template is rendered.
Whitespace stripping can be more aggressive - remove spaces between HTML tags, Django tags and variables.
After research, I find django-template-minifying-loader project, but it has several issues and it is unmaintained. You can use my own fork django-spaceless-templates.
You can try Jinja2, in some cases you can achieve tenfold speedup.
Check Django docs for more details.

Template Partials in Symfony 2

Are there such things as partials in Symfony 2, reusable templates from anywhere, effectively?
I have found include http://twig.sensiolabs.org/doc/tags/include.html but this only allows the rendering of a template in a specific directory structure.
What I really want a folder that contains all my partial templates, rather than lumping them into my main views?
So I want to be able to do something like
{% include "Bundle:Default:Partials:view.html.twig" %}
Update
I do not want to use the enforced structure of Bundle:Controller:Template structure. I do not want to use this as it means putting all my template partials in with my main view templates. I need something that lets me do Bundle:Controller:PartialDir:Template
You can already do that. The symfony2 docs has a section describing how to do this.
http://symfony.com/doc/current/book/templating.html#including-other-templates
In Symfony 2.4 (the version I am currently using but it probably works in other 2.x versions as well) you can do the following:
{% include '::_partials/partial.html.twig' %}
or in Symfony 2.2+ using the include function
{{ include('::_partials/partial.html.twig') }}
This will look for the partial.html.twig template inside of the app/Resources/views/_partials directory. You can obviously name the _partials directory whatever you want. It also works without the '::' prefix.
I know this is old, but the way to achieve what OP asks is the following:
Rather than doing
bundle:controler:partialDir:template
we have to switch it slightly to achieve:
{% include 'Bundle:PartialDir/Controller:Template' %}
To include the controller, you'll need to refer to it using the standard string syntax for controllers (i.e. bundle:controller:action):
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}