I have 6 stylesheets which i am generating with LESS. Since i want to automate this procedure i would like to know if there is any way to parse the stylesheets directly into my template? I need a hybrid between the include and static tag. Static only gives a path, while i want the include functionality being able to parse it.
Is it possible to combine them include and static somehow:
{% include {% static 'test/test.js' %} %}
Update: Its very important i mention that the test.js also contains {% static %} tags.
If it needs to be parsed as a template, it's not a static file and shouldn't be stored in the static directory. That's for assets that need to be served directly from your webserver.
Put it in your templates folder - in a "scripts" subdirectory if you like - and use the standard include tag.
Related
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.
I've been using {% get_media_prefix %} for a very long time. I was explaining this to someone when he pointed this out.
Why do I need to declare {% load static %} in order to use it?
It even uses in the documentation's example code here.
To an extent I understand that static files and media files are similar in nature. Even when we use them with combination of nginx+gunicorn, nginx handles both of them(we let everything else proxy, but not these).
But still we have a separate MEDIA_URL and STATIC_URL as well as MEDIA_ROOT and STATIC_ROOT defined for these files.
Then why {% load static %} needs to be declared in order to use {% get_media_prefix %} ?
Thanks in advance.
In order to use a template tag in your HTML you must first load the module that contains it.
So, accorrding to the source code of get_media_prefix template tag, this template tag lives inside django/templatetags/static.py.
That's why you have to load it every time you use it, in every HTML template.
This, of course, applies to every template tag usage. At the top of every HTML file you load the template tags and then you use them. Just like you import a function in your python code.
UPDATE: From the Django 1.3 release notes:
In previous versions of Django, it was common to place static assets in MEDIA_ROOT along with user-uploaded files, and serve them both at MEDIA_URL. Part of the purpose of introducing the staticfiles app is to make it easier to keep static files separate from user-uploaded files. Static assets should now go in static/ subdirectories of your apps or in other static assets directories listed in STATICFILES_DIRS, and will be served at STATIC_URL.
As you can see, Django used to treat both static and media the same. Since Django 1.3 this changed, but the template tag not. No big deal. It's just a convention.
In a Django project, I would like to include a static file in a template. The following is the the project structure:
proj/
main/
static/
main/
js/
main.js
news/
templates/
news/
news.html
In news.html, I would like to include the main.js with the following hypothetical format:
{% load staticfiles %}
...
{% include {% static 'main/js/main.js' %} %}
How do I do it?
The include directive only searches file in template directories. What you could do (although I wouldn't) is to change your settings to also include the static files:
TEMPLATE_DIRS = [
...
"path/to/your/statics",
]
I have a couple of ideas.
The easiest thing to do would be to ensure you have the filesystem loader enabled, and you include the relevant directory containing static files in TEMPLATES[0]['DIRS'] (previously TEMPLATE_DIRS). The caveats are that it will not pick up static files contained inside your installed applications automatically, unless you list them in DIRS, too. Another option would be to use STATIC_ROOT, but that will only work if you run collectstatic every time you change the static file, which you normally don't have to do in development. Another pitfall is that it will only work with local static storage, not if you use any CDN or otherwise host your static files on a different host / network / whatever.
The other thing you can do is to write your own template loader which will use the static files API to load statics as templates. This is a bit more involved, but it should be able to access static files regardless of how they are stored and served.
Should you choose either option, you still have to be careful. For instance, you'll have to ensure that the static files you're including as templates are safe to include into HTML or whatever other context you're using them in. There will be no escaping going on.
In case you're trying to optimize the number of requests the client has to make, there are better ways. You're most likely better off implementing some pipeline that will pre-process and minify your static files. Including any significant chunk of CSS / JS into the HTML will make it impossible for clients to take advantage of caching, forcing them to re-download the same static content over and over again, likely impacting the performance negatively.
Edit: Since you want to just include some dynamic JavaScript variables, that's not a static file at all. What you really want is to create a template containing JavaScript code, and not mess with handling static files as templates. There is no rule that would say every javascript needs to be a static file. If it's dynamic, it's not a static file.
For the record, this was a typical instance of the XY problem. Problem X was dynamically assigning values to JavaScript variables when rendering templates. You came up with including static files in templates as a potential solution, but then got stuck with problem Y, which was that you didn't know how to include static files in templates.
In django I'm curious concerning the {% load staticfiles %} template directive.
In static files I have a sass directory, and sass in turn can have a cache which gets quite large. I'm not inquiring of 'best practice' though, but in any case are all files pre-loaded or not?
If I'm only using, for instance, fonts, bootstrap, a personal stylesheet, etc...is it going to load all the things I'm not using?
Very curious about this. I don't want to use up more resources than needed.
You've completely misunderstood what the load tag does. All it does is make a template tag library available for the template to use: in this case, the "staticfiles" library which includes the definition of the {% static %} tag. Without that load statement, you can't use that tag.
It doesn't do anything with the staticfiles itself - indeed it can't, loading them is a matter for the browser, which will do whatever is in your HTML.
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 }
)) }}