Django: How to include a block site wide? - django

Apologies, this is pretty basic. I have abstracted my static html navigation bar to a block so that it can be dynamically rendered from a model. I have created a new block marker in base.html with the following syntax
{% block navigation %}{% endblock %}
How can I ensure this is rendered on every page? Do I need to create some sort of middle layer for this? Everything I have done so far has simply used the primary block.
EDIT Sunday, 14 August 2011 11:25 AM
I didn't explain this very well. The content of navigation block is
{% extends 'base.html' %}
{% block navigation %}
<nav>
<ul>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
</nav>
{% endblock %}
I want to render this on every page without having to go through child templates of base.html individually and add it to them, if that is possible.

You have got static navigation bar, so you can just write code in your base.html page and then use it with {% extends 'base.html' %} tag on every new page. Your markup will be in every page. Also, if you use only extends tag in your child page and then render it - you will see base.html without any edition.
If you have got code, which need to appear on few pages, but not on everyone - create 'includes' directory, save code there and extend your base template with {% include %} tag. It won't avoid repating, but make your code shorter.
And last thing you'll need in future, maybe with dynamic code - caching. With {% cache %} tag you can cache block for some time.

Your block is not doing anything here, but if you want something to appear in all the templates, you have to define it in a base template (base.html) and make the others extend from it:
base.html
Yor menu and the stuff you want to appear everywhere
{% block content %}{% endblock %}
Another template
{% extends "base.html" %}
{% block content %}
The actual content of the page
{% endblock %}

{% block FOO %}{% endblock %} reserves a space to be overwritten in sub templates.
{% include "foo.html" %} pulls content from another file into the current file.
To get {% block navigation %}{% endblock %} to display on every page, you need to add content
{% block navigation %}SHOW ME{% endblock %}

Related

Purpose and use of Wagtail/Django template tags of the form {% block body_class %}template-homepage{% endblock %}

In the Wagtail docs (Your first Wagtail site) we are told to create a homepage.html template as follows:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-homepage{% endblock %}
{% block content %}
{{ page.body|richtext }}
{% endblock %}
In the above code, what is that tag {% block body_class %}template-homepage{% endblock %} supposed to be doing? I can't find any mention of it in the text of the tutorial.
The tutorial goes on to instruct us to create two more templates, blog_index_page and blog_page templates, both of which are to contain {% block body_class %} tags. Again, I can find no mention at all of those lines in the docs, let alone an explanation of how they might be used/modified.
A search for "body_class" in the docs finds only the three code blocks just mentioned, and one other mention: in the version 1.2 release notes, under 'What's new — Minor features' the notation: "Simplified body_class in default homepage template."
Any insight as to what I'm supposed to do with these tags? What special meaning or function, if any, does the text enclosed within the tags in those tutorial templates (for example, "template-homepage" in the code above)?
In base.html you have a part that looks like <body class="{% block body_class %}{% endblock %}">. What you add into that block in your templates will be rendered into that section.
For example, lets say on your design your <body> contains a bottom margin, however on a particular template you don't want the margin there and create a css class .remove-margin { margin-bottom: 0 };
You can then add {% block body_class %}remove-margin{% endblock %} into your template and it will apply the class to the <body> tag without you having to modify the base.html template for that one use case.
You are also able to create your own ones. In base.html add a custom block such as {% block my_custom_block %}{% endblock %} and then in your template include some text or html inside that my_custom_block and it will be rendered wherever you've place that part in the base template.
Just to help clarify the correct answers above with a picture as I was also wondering what that block tag was doing, here is the rendered html. As per answer above, this is standard Django.
In the base.html you have this line:
<body class="{% block body_class %}{% endblock %}">
The substitution happens in the home_page.html template:
{% block body_class %}template-homepage{% endblock %}
And it renders as per below screenshot:
<body class="template-homepage">

Django include - using the correct include depending on the page

Hi guys I am fairly new to the Django framework and was hoping someone could assist me in my current dilemma :)
I have my base template set up for my project. I have a main-content area and a side-bar
I created a include for my side bar:
{% include "modules/include/sidebar.html" %}
The issue is that I have two different side bars that I would like to render depending on the page the user is on e.g. if the user is on the home page then use
{% include "modules/include/sidebar.html" %}
but if the user is on an article page then use
{% include "modules/include/article-sidebar.html" %}
I looked through the docs but couldn't find anything to help me solve this issue.
All and any help will be appreciate.
Create a block in your base template, something like {% block sidebar %}{% endblock %}
Then in your home page template do
{% extends 'your-base-template-name-here' %}
{% block sidebar %}
{% include "modules/include/sidebar.html" %}
{% endblock %}
And in your article template use
{% extends 'your-base-template-name-here' %}
{% block sidebar %}
{% include "modules/include/article-sidebar.html" %}
{% endblock %}
More about template inheritance here https://docs.djangoproject.com/en/1.9/ref/templates/language/#id1

Embedded controller views with assets

I am currently working on a symfony2 application and am using embedded controllers. My embedded controllers are like widgets which should encapsulate its own set of functionality and can be embedded anywhere and still be expected to function.
I have a controller called users online. The view it generates is simple, just a list of online users. But, I would like to add some javascript to that view so that I can use ajax to retrieve information for a user that's clicked on.
The controller basically returns a view:
return $this->render('AppBundle:Users:usersOnline.html.twig', array('somedata' => $data);
Here's the view for that controller:
{% extends partial.html.twig" %}
{% block content %}
<ul>
<li>User 1</li> (this would all be generated using 'somedata')
<li>User 2</li>
....
<ul>
{% endblock content %}
{% block scripts %}
..some javascript for interacting with this widget
{% endblock %}
This is the partial that is extended from:
{% block content %}
{% endblock content %}
{% block scripts %}{% endblock %}
Here's the main page that embeds the controller:
{% "base.html.twig" %}
{% block title %}Main{% endblock %}
{% block content %}
..some markup here
<div id="usersonline">
{% render "AppBundle:Users:usersOnline" with {'max': 4} %}
</div>
{% endblock %}
{% block scripts %}
..some javascript
{% endblock %}
This is the base that it extends:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %}{% endblock %} - App</title>
...Some stylesheets
</head>
<body>
{% block content %}{% endblock %}
<script src="http://yui.yahooapis.com/3.5.0pr2/build/yui/yui-min.js"></script>
{% block scripts %}{% endblock %}
</body>
</html>
The problem I am facing now is including javascripts from the embedded controller. In my case, the view extends partial and is fully rendered as 1 unit before it is inserted into the main page. In this case, I can only put my javascript in the content block, which means I will have <script> tags within <div> tags. I would also prefer to have scripts at the end of the body for user interface performance.
How should I structure my templates (or is it even possible) so that I can render the appropriate pieces from an embedded controller's view into appropriate blocks in the template that embeds the controller? In my current template the YUI library would be loaded after the embedded controller's rendered HTML markup, so accessing using the YUI library within the embedded controller would be impossible.
I have written an extension which is essentially a token parser to deal with this. The documentation for the token parser is very sparse and the code is also mostly uncommented, so it took a bit of fiddling around to get working.
I have structured the parser so that at the beginning, you would declare something (similiar to {% use %}):
{% myparser "AppBundle:Users:usersOnline" with {'max': 4} as xyz %}
Then blocks like xyzcontent would be avaliable and then you just render them in the appropriate places using {{ block('xyzcontent') }}. It works quite well. However the following does not work:
{% set max = 4 %}
{% myparser "AppBundle:Users:usersOnline" with {'max': max} as xyz %}
I could not find a way to evaluate an expression and get its value directly within the token parser. For the most part it works fine, except for this problem.
If someone has some idea as to how to get the value of a variable within the token parser, it would be great :)
After one day of looking for a satisfying solution, I finally choose for the following approach:
Render a controller for your widget's functionality (like you are already doing);
Render templates to embed your widget's assets (controllers are not needed and will waste your performance). Ignore include if the template doesn't exist.
Use a consistent naming convention to make the widget functionality more dynamic.
Example for one widget:
{% "base.html.twig" %}
{% block title %}Main{% endblock %}
{% block content %}
..some markup here
<div id="usersonline">
{{ render(controller("AppBundle:Users:usersOnline", {'max': 4})) }}
</div>
{% endblock %}
{% block scripts %}
{{ include("AppBundle:Users:usersOnline_js.html.twig" ignore missing }}
{% endblock %}
{% block stylesheets %}
{{ include("AppBundle:Users:usersOnline_css.html.twig" ignore missing }}
{% endblock %}
(As you can see, I use the Symfony 2.2 way of including and embedding)
Or, a more dynamic way of widget rendering:
{% "base.html.twig" %}
{% block title %}Main{% endblock %}
{% block widgets %}
..some markup here
{% for widget in widgets %}
{{ render(controller(widget.controller ~ ':widget', widget.options)) }}
{% endfor %}
{% endblock %}
{% block scripts %}
{% for widget in widgets %}
{{ include(widget.controller ~ ':widget_js.html.twig' ignore missing }}
{% endfor %}
{% endblock %}
{% block stylesheets %}
{% for widget in widgets %}
{{ include(widget.controller ~ ':widget_css.html.twig' ignore missing }}
{% endfor %}
{% endblock %}
My conclusion
Rendering multiple controllers in one page, while "merging" it's templates blocks, cannot be achieved out of the box in Symfony2. Symfony2 has always been focusing on one controller for each request, which explains why multiple controllers outputs are separated and hard to combine.
I think the Symfony CMF has (or will have) a proper solution for this, because this whole widget topic is more applied in dynamic content managed websites.
Anyway, I think my way of combining embedded controllers and included templates is the best approach for Symfony 2 so far.
The alternatives, in my opinion, do have to many disadvantages:
Using multiple controllers: too heavy and not needed because assets won't need any controller.
Loading the widgets assets directly in the page: hard to maintain when changing widgets / assets.

Is there a way to make a block optional in Django template

In Django's templates system, if I have a block that I want to make optional using an if statement, how do I do it?
I was trying this:
{% if val %}{% block title %}Archive {{ foo }}{% endblock %}{% endif %}
But that doesn't work. Is there a way to do that, so that for a given value (in this case Null) the block isn't issued and the base template uses the original values?
Edit:
Let me be a little more specific, so that it is easier to answer.
I have a page with 10 entries per page. The user can then go on to the next page and see the next ten items. For each further page they go, past the first, I would like to have the title tag say something like "Archive 1" or "Archive 10" but if they get back to the original page, it is no longer archive, and it should just go to the original site title already given in the base templates.
I ran into a similar issue with a project I'm working on. Here's how I resolved it using {{ block.super }} to pull the default value from the parent block:
My parent template contains:
{% block title %}Default Title{% endblock %}
My child template contains:
{% block title %}
{% if new_title %}{{ new_title }}{% else %}{{ block.super }}{% endif %}
{% endblock %}
*Note: You may want to wrap the code in {% spaceless %}{% endspaceless %} if you plan to use the result in an HTML title tag.
(It looks like Jordan Reiter posted the same solution in the comments of the original question a bit before my response.)
As far as I understand blocks are placeholders to be "overridden" in the child templates. They have to be defined at "compile time" and not at "run time".
As for your specific problem why not modify the title based on the page number (assuming you use pagination)? Something like this:
{% block title %}
{% ifequal page 1 %}Current{% else %}Archive {{ page }}{% endifequal %}
{% endblock %}
I would only have to add to the good answers above that depending on the Django version sometimes the {{ block.super }} puts the content from the master twice, this seems to happen in the most recent versions of Django.
I am using Django 1.8 and whenever i put the {{ block.super }} it started to behave in that way just as an addition to the Jamie answer i can say that in the base template you can put the content you wish
{% block title %} Default Title {% endblock %}
And then in the child if you want the footer to be inherited and displayed just do not do anything it will be. But if you do not want that block to be displayed then put the tag in the child with empty content just like this:
{% block title %}
{% endblock %}
Then it will be hidden once it is rendered also you can overwrite the content on it if you wish.

Nested blocks in Django templates

The master template in my Django app looks like this:
{% block parent %}
Some text...
{% block child %}
Default content here...
{% endblock child %}
...some more text
{% endblock parent %}
Now, this template should be overwritten in a way that the child block is changed:
{% extends "master.html" %}
{% block child %}
New content here...
{% endblock child%}
However, the rendering stays the same (printing "default content here..."). Have I missed something obvious or are nested blocks not possible? (Or, violating the DRY principle, have I to re-define the parent block?)
Edit: I'm working with Django 1.1, if that matters.
OK, it's a bug in Django's template system. For most other cases, Ben James is right (see his comment to my question above).
In my case, the child block was inside a {% ifnotequal a b %} block, and that breaks the block inheritance. I consider that to be a bug, since there are dozens of natural use cases for such operations.
The corresponding ticket.
Do this:
{% extends "master.html" %}
{% block parent %}
{% block child %}
New content here...
{% endblock child%}
{% endblock parent %}