Django CMS conditional - django-cms

How do I use a conditional in Django CMS in base.html to detect if the page is the home page and add a unique class to the body tag? I'd prefer to not duplicate base and just add a class so I can handle some styles differently on the home page.

It depends how you structure your pages.
I opt for creating pages as a child of the 'home' page so use something like this for page titles;
{% if request.current_page.get_ancestors|length <= 1 %}
<h1>{{ request.current_page.get_page_title }}</h1>
{% else %}
{% for ance in request.current_page.get_ancestors %}
{% if ance.depth == 2 %}
<h1>{{ ance.get_page_title }}</h1>
{% endif %}
{% endfor %}
{% endif %}
So you could do something like;
<body class="{% if request.current_page.get_ancestors|length <= 1 %}base{% endif %}">

Related

Twig variable not available on extended page outside blocks?

I have this situation:
{% extends 'base.html.twig' %}
{% set isManager = (isAdmin or isAffiliateManager or app.user.isFinanceDepartment) %}
{% block content %}
[...](add some html if manager)
{% endblock %}
{% block css %}
(add some css if manager)
{% endblock %}
{% block js %}
(add some js if manager)
{% endblock %}
isAdmin and isAffiliateManager are declared in base.html.twig. I want to access isManager in all the blocks, without declaring it 3 times. Why isn't that possible?
Edit: simpler replication: https://twigfiddle.com/kcz6mn/2

How to change "if" condition related to a page

I would like to include tab_system.twig file in two differents files : archive.twig and blog_list.twig to have the same html in those two files.
{% include "tab_system.twig" %}
In this tab_system.twig file I have a condition to add the active class :
{% if XXX %}active{% endif %}
But this condition must be different depending on which page the user is.
For blog_list.twig :
{% if loop.first %}active{% endif %}
For archive.twig :
{% if item.term_id == term_page.term_id %}active{% endif %}
I wrote this without success :
{% set addclass = '' %}
{% if is_blog_list %}
{% set addclass = '{% if loop.first %}active{% endif %}' %}
{% elseif is_archive %}
{% set addclass = '{% if item.term_id == term_page.term_id %}active{% endif %}' %}
{% endif %}
In tab_system.twig I have a tab system with menu from a part and content to the other part. I wrote a js loop to display the corresponding content. I need to add active class on the first link and first content tab in blog_list.twig file and to add active class to the link and content tab depending on which category the user is.
<div class="tab-menu-list">
{% for item in all_posts %}
<a href="#" class="tab-menu {{ addclass }}"</a>
{% endfor %}
</div>
<div class="tab-content-list">
{% for item in all_posts %}
<div href="#" class="tab-content {{ addclass }}"</div>
{% endfor %}
</div>
is_archive and is_blog_list are variables defined elsewhere. They work
How can I create a condition ? Thank you in advance.
As I'm assuming you are looping the records, I'd say don't overcomplicate things,
single.twig
{% for i in 1..5 %}
{% include 'article.twig' with { 'active' : loop.first, } %}
{% endfor %}
template_blog.twig
{% for item in items %}
{% include 'article.twig' with { 'active' : item.term_id == term_page.term_id, } %}
{% endfor %}
article.twig
<div class="articles__list tab-content{{ active ? ' active'}}">
foo
</div>
demo - just change the main template
If you really wanted to keep that for inside the article template, which I think is not a great idea, you could still use the variables is_archive and is_blog_list inside article, e.g.
article.twig
{% for item in items %}
{% if is_archive %}
{% set active = loop.first %}
{% elseif is_blog_list %}
{% set active = item.term_id == term_page.term_id %}
{% endif %}
<div class="articles__list tab-content{{ active ? ' active'}}">
{% endfor %}

How to limit knp_paginator from extending?

I want to limit the pagination boxes going for ever. Say there is 100 events loaded -> 3 events are displayed/page; and such that the pagination boxes [1][2][3][4]....[40] doesn't go on...
In config.yml add this:
knp_paginator:
page_range: 5 # number of links showed in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
If you need change the sliding use any of these:
template:
pagination: '#KnpPaginator/Pagination/sliding.html.twig'
#KnpPaginator/Pagination/sliding.html.twig (by default)
#KnpPaginator/Pagination/twitter_bootstrap_v3_pagination.html.twig
#KnpPaginator/Pagination/twitter_bootstrap_pagination.html.twig
#KnpPaginator/Pagination/foundation_v5_pagination.html.twig
I guess that knp paginator does that for you but in case it does not You could try to modify any of the templates above to do something like this:
set a control variable
{% if pageCount > maxNumberOfBoxes %}
{% set breakpointAdded=true %}
{% endif %}
then find the loop which look like this
{% for page in pagesInRange %}
{% if page != current %}
<span class="page">
{{ page }}
</span>
{% else %}
<span class="current">{{ page }}</span>
{% endif %}
{% endfor %}
and replace it for something like this(with your own logic of course)
{% for i in range(0,pageCount) %}
{% if i > xBreakpoint and i < yBreakpoint and breakpointAdded == false %}
<span class="dots">...</span>
{% set breakpointAdded = true %}
{% else %}
{% if page != current %}
<span class="page">
{{ page }}
</span>
{% else %}
<span class="current">{{ page }}</span>
{% endif %}
{% endif %}
{% endfor %}
if anything of this works you could try to modify the getPaginationData function from SlidingPagination class.
what ever works for you.

Twig / Symfony2 Dynamic Template

I want to do this kind of thing so that if a template doesn't exist it just renders the content. The below code won't work though as you can't code it like this.
{% if app.request.attributes.get('twig_parent_template') != "" %}
{% extends app.request.attributes.get('twig_parent_template') %}
{% block title "The Title Here" %}
{% endif %}
{% block content %}
Content here
{% endblock %}
Can I do this kind of thing somehow?
Twig extends has a good documentation on this topic.
Since you need to specify a template to extend, my thoughts go on creating a default template.
#Bundle/Resources/views/yourview.html.twig
{% set extender = app.request.attributes.get('twig_parent_template') ? : 'Bundle::default.html.twig' %}
{% extends extender %}
{% block title "Your title" %}
{% block content %}
Your content
{% endblock %}
#Bundle/Resources/views/default.html.twig
{% block content %}{% endblock %}
#Bundle/Resources/views/parent.html.twig
{% block title %}{% endblock %}
{% block content %}{% endblock %}
Doing such, if app.request.attributes.get('twig_parent_template') is set, it will render the template given in its value.
Otherwise, it will render default.html.twig containing only the content block

Symfony 2 custom themes folder

I'm designing a multi-tenant application with Symfony2, There will be common templates and each tenant could have custom templates. I would like create a theme folder like this(like wordpress with css,img,etc...) :
Themes/commons/base.twig.html
Themes/commons/css/styles.css
Themes/commons/js/script.js
Themes/tenantID/base.twig.html
Themes/tenantID/css/styles.css
Themes/tenantID/js/script.js
Perhaps I'm taking a wrong way...?
Any suggestion ?
Thanks.
There is nothing wrong with your design. I may name "commons" as "default" but thats up to you. Approach with tenantid looks good to me. Whats your question?
https://github.com/fabpot/Twig/issues/17 - no dynamic inheritance
LiipThemeBundle might be a solution: http://symfony2bundles.org/liip/LiipThemeBundle
You can achieve all this by following symfony standard . because if you follow structure you will make full stack full site in frame work and you can also learn how to use frame work.
like in
bundle folder:
userbulndle/css
userbulndle/js
adminbundle/css etc
and use theme forming
{% block gender_widget %}
{% spaceless %}
{% if expanded %}
{% for child in form %}
<div class="radio_ele">
{{form_widget(child) }}
{{form_label(child) }}
</div>
{% endfor %}
{% else %}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}
{# ----------------------------------------------------------- #}
{% block field_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<div class="error_list">
{% for error in errors %}
{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}
{% endfor %}
</div>
{% endif %}
{% endspaceless %}
{% endblock field_errors %

Resources