How to tell Template where to load files from? - django

I want to store some templates in the database for custom rendering. I create a Template object with the string coming from the database and then I use the render method. So far so good.
Now I would like to be able to use the extension mechanism to load a template from another one with the {% extends %} tag so that even the base template could be loaded from the database. I realized that the extension mechanism works and uses the global configuration given in settings. So I'm able to extend from a file from my template loaders. I can also add my custom loader to look for data source in the database. However I would like my TemplateLoader to know what database object was the source of the first template, and look for base files depending on that.
I would expect a hook into the Template object to specify custom TemplateLoaders instead of global ones. I've looked into documentation and source files, but failed to find such an hook.
Any hint?
** CLARIFICATION **
Since I dind' get an answer, I try to clarify the question. Suppose I have a template to render some kind of objects of my database. This template uses an {% extends "base.html" %} to load the skeleton file with the base layout of the site. Suppose now that for some of these objects (based for example on an attribute in the object) I want to modify the base file (not the template!). How do I achieve this?

You can easily add feature to load you templates from databases with: http://django-dbtemplates.readthedocs.org/en/latest/.

Related

drf-yasg Customize SwaggerUIRenderer

I'd like to customize the style (font, colors, logo, etc) of drf_yasg generated docs.
I see that I can extend drf_yasg/swagger-ui.html with the blocks extra_head, extra_styles, extra_body, extra_scripts, and can even overwrite the other blocks if I need to.
What I am not clear on is how I point to my template that extends swagger-ui.html.
I started with
class MyCustomSwaggerUIRenderer(SwaggerUIRenderer):
template = 'api/custom-swagger-ui.html'
I want to replace SwaggerUIRenderer with MyCustomSwaggerUIRenderer in get_schema_view but do not understand how/where to do it without explicitly trying to enumerate all the other Renderers required too in some subclass of rest_framework.views.APIView and that seems convoluted.
Pointers to docs or examples are appreciated. I've already read https://drf-yasg.readthedocs.io/ without success.
You don't have to create a custom class for this. You just need to create a directory with the name drf-yasg under new or existing app and then place a file with the name swagger-ui.html underneath it with your custom template. For example, if you already have an app with the name api, you can just put it under api/templates/drf-yasg/swagger-ui.html. Make sure the app api is specified before drf-yasg in INSTALLED_APPS.
Reference: https://github.com/axnsan12/drf-yasg/issues/294#issuecomment-464461773

Does Django cache custom tags and filters?

I have a considerable number of custom template tags that perform a variety of functions, including:
simple string transformation
display of complex ui elements
timestamp manipulation and formatting
handling and display of user avatars
etc...
All of these functions reside in a single file: app/templatetags/custom_tags.py. When I want to use one of these tags in a template, I import all of them using {% load custom_tags %}.
However, only a small subset of the available tags are actually used in any given template. In other words, all these functions are being 'loaded' into the template, yet only a few of them are called in a specific web request.
Is this inefficient, in terms of performance? Should I be loading code more conservatively -- i.e., splitting up my custom tags into separate files and only loading the subset that I need?
Or does this not matter, because all tags are loaded in memory -- i.e., subsequent calls to {% load custom_tags %} elsewhere in the application won't result in any additional overhead?
I apologize if there are incorrect assumptions or premises in this question. I'd love to have a better understanding of the implications of importing python code in general, or in a Django environment specifically.
For Django <= 1.8:
The load tag is defined here and actually does the loading here and here. Both places call to get_library, defined here. According to the docstrings there, yes, it caches template tag/filter libraries within the same process in the dictionary initalized here.
For Django 1.9:
The modules for template tags are now being loaded even earlier, when the parser is instantiated, and the libraries are being stored directly on the parser. Load tags call out to find_library here and here, which just gets the already-loaded tag directly from the parser.
Aside from the actual loading activity
As #spectras points out below, regardless of the Django version, the tag's loading behavior is, strictly speaking, a side effect, and the tag returns (<=1.8/1.9) a no-op node(<=1.8/1.9), which renders no content--so there's not really a performance consideration as far as that goes.

Assigning Backbone templates using Django pipeline similar to Rails JST?

I'm building a large scale Backbone Marionette app on top of Django utilizing the Django asset pipeline to compile all of the assets.
Right now, I am saving my Handlebars templates as JS strings in the app object like so:
App.Templates.Header = '
<div id="header">
... header stuff ...
</div>
'
class App.Views.Header extends Backbone.Marionette.ItemView
template: App.Templates.Header
I'm not sure that saving templates out into JS strings is really the best way to do things at all. With Rails, you can save out template files and reference them directly in the file structure with JST:
template: JST['apps/base/templates/header']
My understanding is that this is a feature that is baked in to Rails. Is something like this possible with Django? Or, is there another more efficient way that I should be handling my templates?
This features is actually built into Django Pipeline under as JavaScript Templates.
Basically, you define the function to use for processing your templates (Mustache, Handlebars, Prototype or JST) and then the global namespace where those templates are stored and the extension that the compiler uses to determine which files to add to that template object.

How to automatically include my helper file in model in Joomla?

I am wondering how to automatically include helper into to the all models files of the component, that i only need to create object from helpers class in models files?
Avoid using require_once, and use the proper Joomla methods for including helper classes
JLoader::register('ClassName', 'path_to_class');
Put this snippet in the beginning of your component entry controller. Joomla will then autmotically load the class only when it's needed.
By using the regular PHP require_once you will slow down your code, since the file will instantly be loaded into memory, even if it's not needed.

Django template outliner

I'm trying to obtain a visual representation of the templates of a Django project, as a hierarchy.
The main idea is to get a list of template names as the default template loading machinery would return (ie honoring TEMPLATE_DIRS, TEMPLATE_LOADERS, etc.) and then parsing the templates looking for {% block %} and {% extends %} tags, in order to create a tree structure. Finally use grapviz for the visualization.
I'm not sure if it's a viable approach. But just to start, how can I load the templates the way I described?
Or maybe, does something similar already exist?
Normally, templates are looked up by names online, there is no root entry for every template that might be used.
Thus if you want to grep all used templates in a project, you need to scan every place that loaders in TEMPLATE_LOADERS might check, to generate possible entries for later bottom-to-up checking. This would be hard, some backends of loaders might even does not allow fetch-by-directory operation.
Or you could parse views and urls files inside INSTALLED_APPS to grep template names, but this only works w/ hard-coded name of template and even more difficult.
Also, there might be templates loaded from hard-coded string...
For a given template name, its easier to load the template and check nodes inside it, just as your idea. You could check django.template.base to know how it works.
Furthermore, you could take advantage of django-debug-toolbar to show the templates used for a request. And, IMO, keeping template structure flat, simple and thus determined, is easier to achieve.