django template forloop.counter question - css

i have many fields in my form i was trying to apply different css to neighbour forms fields like
<li class='thiscolor' >
<field>
</li>
<li class='thatcolor' >
<field>
</li>
if there a way like
{% for field in form %}
**{% if forloop.counter%2 == 0 %}**
<li class='thiscolor'>
{% else%}
<li class='thatcolor'>
{%endif}
{{field}}
</li>
{% endfor %}
for forloop.counter ?
Thanks a lot!

The cycle tag is designed for this type of problem:
{% for field in form %}
<li class="{% cycle 'thiscolor' 'thatcolor' %}">{{ field }}</li>
{% endfor %}

I agree with Jarret that cycle is best here, but to actually answer the question, the %2==0 operation can be replicated by using the divisibleby filter.
{% if forloop.counter|divisibleby:"2" %}

Another thing to keep in mind is that since this is a front end problem - the styling is what you're trying to effect - you can solve it on the front end. There's a good example provided toward the bottom of this A List Apart article. Of course, if you've already got working Django code there's no sense in doing this now.

Related

Sensiolabs Insight : Twig templates should not contain business logic

I am actually running Sensiolabs Insight analysis on my Symfony 2.8 project.
I have a major issue with some of my Twig templates:
Twig templates should not contain business logic
The associated message is always the same :
Template too complex, depth of 10 is reached but only 5 is allowed.
For example this happens with the following template :
{% extends "FBNGuideBundle::layout.html.twig" %}
{% block title %}
{{ 'fbn.guide.page_title.bookmarks'|trans }}
{% endblock %}
{% block body %}
<div id="bookmarks" data-bookmark-ids="{{bookmarkIds|json_encode()}}">
{% if (restaurants|length > 0) %}
<div class="restaurants">
<h3>MES RESTOS</h3>
{% for bookmark in restaurants %}
<div class="bookmark" id="{{'bookmark-' ~ bookmark.id}}">
{{ bookmark.restaurant.name }}
<br>
<br>
<button>SUPPRIMER DES FAVORIS</button>
<br>
<hr>
</div>
{% endfor %}
</div>
{% endif %}
</div>
{% endblock %}
I tried to include in a separated file the code contained inside <div id="bookmarks"></div> and the depth has been reduced, but it is not a solution. I suppose that the problem is the access to some properties through several objects using getters (i.e bookmark.restaurant.slug).
I have a free plan so I am not able to access the documentation related to this warning. Anyone knows how to solve the problem ?
Thanks.
When you have too much logic in the view, you can put it in a custom Twig Extension. An advantage is that you don't need to duplicate the html if you are reusing that part in another page and of course, the code is more clear :)
In your case, you can write a new Twig Extension that renders all the bookmarks.
If you didn't build somethng similar till now, you can read about it here http://symfony.com/doc/current/templating/twig_extension.html

Drupal 8 conditional syntax

I'm looking for the proper syntax with a Drupal 8 conditional display of a link. I've been reading the twig documentation, and it's a significant jump from the PHP templating days so it's slow going. I need to conditionally display a link within the footer of a view.
In the past, this would be straightforward, i.e:
<?php global $user;?>
<?php if (user_access('administer nodes')):?>
<div class="foo">Shorthand link for admins</div>
<?php endif;?>
Getting this to work in Twig has been difficult as I'm unfamiliar with the syntax. Do I need to declare global user in some capacity? From this link here, Symfony 2: How do I check if a user is not logged in inside a template?, it seems that all I'd need to do is:
{% if is_granted('ROLE_ADMIN') -%}
<div class="foo">Shorthand link for admins</div>
{% endif %}
But I get an error when trying to submit that. Is ROLE_ADMIN not defined? How do I get the Symphony? (correct?) roles as defined within the D8 installation? I'm not sure what I'm doing wrong. Any assistance is appreciated.
If you need to check a specific permission try
{% if user.hasPermission('administer nodes') %}
<div class="foo">Shorthand link for admins</div>
{% endif %}
If you try to check the current user is administer in TWIG file , you can use
{% if is_admin %}
<div class="foo">Shorthand link for admins</div>
{% endif %}

What would be the best way to create my menu in Symfony 2

I'm working on the admin of my portfolio and I'm having a dilemma about my menu. The admin side of my portfolio allows me to change some general info, add, modify or delete a project. Therefore, all pages of my admin have the same menu. The menu have the following buttons: a button to change the general info and a button to add a new project. if there are projects in the database, the menu have more things, a select with the names of my projects and the two buttons to modify or delete the project.
At first, I made a form for everything about the project and an other just for the change info button, but after asking some question, I got often told that I shoudn't make any form for that and that just making link should be fine.
I like that idea, but, if I do that, how can I pass the selected project when I click on modify for exemple?
<a href="{{ path("modify_project") }}">
Here is my current code:
<ul>
<li>
Ajouter un projet
</li>
</ul>
{% if projets | length > 0%}
<select>
{% for id,projet in projets %}
<option value={{ id }}>{{ projet }}</option>
{% endfor %}
</select>
<ul>
<li>
Modifier
</li>
<li>
Supprimer
</li>
</ul>
{% endif %}
If someone have a better idea, I will be very interested to ear it.
Have you had a look at this: https://github.com/KnpLabs/KnpMenuBundle?
Sorry, your are right, the answer is actually this one:
{{ entity.id }}
Twig will understand that, nothing more to do.
After really thinking about it, The best way would be a form, since you can't change the href of the link, except with javascript and I don't want to do that.

Button stays 'pressed down' in Bootstrap theme

I am in the process of setting up the theme for my website using Jekyll.
I am using the Bootstrap example theme for this and I am currently facing an issue where by the Home button stays 'pressed down' when on another page such as the About page.
Here is a link to the CSS file: https://github.com/Dansmithyy/dansmeuktheme/blob/master/css/main.scss
Is there any way of changing this so that the buttons stay pressed down when the user is on that specific page?
Your main menu is hard coded in html. And it remains the same all over your pages.
<li class="active">Home</li>
<li>About</li>
<li>Contact</li>
My code inspector tell me that a rule is applied to
.navbar-inverse .navbar-nav>.open>a, .navbar-inverse .navbar-nav>.active>a
Here is the origin of the symptom.
The real cause of your problem is that your are hard coding your main menu.
If you look in your original default.html file and then in _includes/header.html you can see things like {% include header.html %} and {% for page in site.pages %}...
The last tag help you automatically generate a menu. I've added the active class trick in it to save you some time :
<ul class="nav navbar-nav">
{% for node in site.pages %}
{% if node.title %}
<li class="{% if page.url == node.url %} active{% endif %}">
{{ node.title }}
</li>
{% endif %}
{% endfor %}
</ul>
NOTE the {% if node.title %} if you want a page to appear in the menu you must put a title variable in the page's front matter. eg: title: My nice title

twig raw filter and functions

Hi everyone and sorry for my english,
I have a service who generates some html code which is passed to a twig template. I had to use the raw filter to show the code, but in that code I call a twig function.
This is the code stored in a var which is passed to the template by the controller.
'<li class="active" >Help</li>'
The resulting html code is the same, so {{ path('help') }} is not called.
Is there any filter to show the html code and call the functions?
Thanks
In your code, you are using {{path('help')}} for the hyperlink. Instead of using the twig path function, include the original Url in the code that is sent from the service. In the service. use
'<li class="active" >Help</li>'
I answered this before here: Twig: prevent parsing of client-side templates
{% raw %} deprecated
{% verbatim %}
<ul>
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endverbatim %}
You should render your variable using the {% include(template_from_string(your_var)) %} twig block.
See the answer of Render content from string/database and generate links with twig for more info.

Resources