What I need is to append text blocks to the same name in any template and finally print its value.
What I have so far.
In child template:
{% set tmp %}
...some text...
{% endset %}
{% set capture = capture|default([])|merge([tmp]) %}
In parent (main layout):
{{ capture|default([])|join|raw }}
{# it works in the same template but not in parent #}
I know it is ugly but still would be happy if it worked.
Ideally I'd like to make an extension to work like this:
{% capture %}
...some text...
{% endcapture %}
{# to capture #}
{% capture() %}
{# to print #}
but documentation that I've seen (http://twig.sensiolabs.org/doc/advanced.html) does not help.
I don't know what type of extension should I choose, where actual data manipulations are made, how I access and append data.
What existing extension could I use as a base?
Couldn't access even Twig global variable set in sf config.
So created 2 Twig functions: one to append content, another to get what is buffered.
Using twig extension class property as "global var" because container params turned out to be locked.
Usage:
set content to local var
append local var to global var
get and print raw global var content
Related
I am very new to django and working on it.. I visited a html file and dont know the difference between {{}} and {% %} in html files used
as here
{% load static %}
Thanks a lot
You can use
{% %} For sentences such as if and for or to call tags such as load, static, etc.
{{ }} To render variables in the template.
Read More about it at Django Docs
{% %} is for displaying code and {{}} is for displaying variables
There are three things in the template in Django
First is template variable and the second thing is template tag and third and last is template filter
so we write a template variable is {{}}
and write a template tag is {% %}
third and last is template filter {{variable |filter:arg}}
I'm new too for Django, so if i'm wrong, please someone correct me.
The difference between they are:
{{variable}} is used to use a variables. When the template encounters a variable, it evaluates that variable and replaces it with the result.
You also can use filters {{variable|filter}} like this:
{{name|length}} in this case you will use a variable "name" and return the length of that variable.
{%tag%} could use for loops or logic, or load external information into the template to be used by later variables. You can create block tags to help extend other html files parts. Also you can create custom tags.
A good place to see how to do it:
https://www.codementor.io/hiteshgarg14/creating-custom-template-tags-in-django-application-58wvmqm5f
Tags like loops and block, need to be closed.
{% %} for IF ELSE CONDITIONS and FOR LOOP etc
{{ }} for veriables that rendered from view function also used in FOR LOOP veriables like
`enter code here`
{% for obj in qs%}
{{ obj.veriable_name }}
{% endfor %}
Is there any way to make global placeholder in my base template? I need it to be the same on every page (banners list).
How can I do that?
I usually create a page in my CMS that isn't published, but contains placeholders that I would like to use elsewhere (footer/headers) etc.
Make a new template extra_placeholders.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block content %}
{% placeholder "Banner-List" %}
{% endblock %}
add it to your settings:
CMS_TEMPLATES = (
('my/path/extra_placeholders.html', 'Extra Placeholder Page'),
...
)
now go to the admin and create the placeholder with whatever plugin you want. Then go to you base template (*base.html probably) from which all your other pages inherit and add this wherever you want the placeholder to appear:
{% load cms_tags %}
...
{% show_placeholder "Banner-List" "extra_placeholders" %}
You can read more about it in the docs
EDIT
As #José L. Patiño has mentioned in the comments, this solution is only necessary for those using django-cms < 3.0. For the newer version you can simply use the static_placeholder template tag
There is the "static_placeholders" now, http://docs.django-cms.org/en/latest/reference/templatetags.html#static-placeholder
Sounds like it's what you needed way back when.
You can use the following ways to create a global palceholder for all pages.
create a place holder on the base page. {% Placeholder "footer"%}
make the contents of the placeholder through django cms as home page
then to display the same for each placeholder page, add {% show_placeholder "footer" "home"%}, this means displaying the newly created footer placeholder earlier from the home page,
This will display the entire contents existing footer placeholders on the home page of all pages that use the template.
but for the home page there will be two footer is displayed, to mengilangkannya, please do modifications to use CSS to hide the master placeholder.
So I am using django-google-charts to generate bar chart. Here the chart is drawn in the "out" div.
However I want to generate more than one charts in one page, within different div, for example chart0 in "out0", chart1 in "out1", chart2 in "out2".
{% load googlecharts %}
<div id="out"></div>
{% googlecharts %}
{# some code here #}
{% graph "out" "out_data" "out_options" %}
{% endgooglecharts %}
So I tried to modify the {% graph "out" "out_data" "out_options" %}, my intention was
{% graph out|add:{{ forloop.counter0 }} "out_data" "out_options" %} so that graph output source will be replaced by out0, out1, out2, etc.
However the use of {{}} inside {{%%}} is not allowed. Plus the graph tag will take whatever at the first place as a string parameter as separated by comma.
Is it possible to solve the problem on the template side?
Thanks.
{% graph "out"|add:forloop.counter0 "out_data" "out_options" %}
no brackets inside {% %} and it should work
also: |add concatenates strings, you were concatenating a variable (out instead of "out")
the above code should now work as you expect :)
Finally I worked out fine using only the google chart tools Javasciprt API. It allows multiple charts on one page. Similar question here. The last answer is the solution.
I got a problem where I want to use template including in Django.
Here is the real example:
I got 3 files:
home.html (will get the context variable passed from Views)
base.html (the skeleton template file)
and the header.html (included by base.html).
If I put the code below directly in base.html without including header.html, the {{title}} variable passed from home is correctly called. But if I include the header.html in base.html, the {{title}} variable's value cannot be called.
<title>{% block title %}{% endblock %} | {{ SITE_INFO_TITLE }}</title>
Is there any solution to this problem? Thanks.
Could you just pass in a variable within the {% include %} tag? It's documented here: https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#include
{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
As far as I know blocks and variable are distinct in django.
If you want to pass title as a context variable you have to set it using a declaration in base.html such as :
{% include "header.html"%}
Which in turn contains :
{% block title %} {{title}} {%endblock%}
You can also set it in home like this.
{% block title %} Home page {%endblock%}
But I also try to set in the template context.
Without the title block.
def test_view(ctx):
xa = { "title":"Sommaire"}
return render_to_response("test.html",xa)
I think you can also see the with template tag I think it is possible to set a context variable using this tag.
You can use Inclusion Tags to render an additional template from within a Django template. You can additionally pass the 'child' template context from the 'parent' template.
It's a little involved for your use case but it solves your problem. I tend to use it when I'm looping a list to render each item with a custom template. I can then reuse that template elsewhere without duplicating the markup if I need to render another item of the same type.
I need to dynamically generate a code like this in the resulting html:
<p>>> gallery one</p>
<p>gallery two</p>
<p>about the author</p>
<p>our news</p>
I do have menu_code string variable created in views.py (it is generated depending on an item number of the current page passed — 1 in the case above), which contains that long string with the code shown above. It is (well, supposed to) passed by locals() into the html template (all other variables are passed that way successfully):
return render_to_response('gallery_page.html', locals())
I have this:
{% include menu_code %}
inside the template html. But instead of being interpreted as code it is just shown as text in the browser.
What am I doing wrong? How to make it work as a dynamically generated menu?
Turned out it's necessary to surround the line in a template with autoescape like that:
{% autoescape off %}
{{ menu_code }}
{% endautoescape %}