Django Registration Redux Customize - css

I've been trying to get a clear, simple answer to this, but I can't seem to extract anything that makes sense from either the docs or from stack overflow.
I've implemented the standard templates that come with Django Registration Redux, but I can't seem to work out how to change the form styles and the text copy that are the defaults in Redux.
Here's my html:
{% extends 'base.html' %}
{% block title %}
Registration Form - {{ block.super }}
{% endblock title %}
{% block content %}
<div class="content-wrapper">
<div class="content">
<div class="pure-g">
<div class="is-center pure-u-1">
<div class="pure-u-1-2 thin-box">
<h1>Registration form</h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" class="s-button"/>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock content %}
As you can see I've styled the button, but none of the elements are available for styling. The redux data must be sitting somewhere, but I've no idea where it is.
Someone on SO has suggested using crispy forms, but I'm using pureCSS rather than bootstrap, and I've already gone to the trouble of setting up a set of form styles that I want to use, so it seems like overkill to install a module with a set of styles that I then have to over-write.

If i understood you correctly you would like to access individual fields. To access them you can get the fields' names here.
So in the template instead of {{ form.as_p }} you could use individual fields like this:
for e.g. {{ form.username }}.
If you also would like to add styling then you need to use filters like:
{{ form.username | add_some_css:"my_new_class" }}. This styling needs to be configured in a separate file for e.g. redux_css.py. To make your template use this file you need to include after {% extends 'base.html' %} the following {% load redux_css %}.
Now you need to create redux_css.py:
from django import template
register = template.Library()
#register.filter
def add_some_css(field, css):
"""
Adds css to fields in Redux templates
"""
return field.as_widget(attrs={"class":css})
All this should make your redux fields (which are text inputs) styled like: class="my_new_class"
Obviously you need to create .css files and "import" them into your .html files.
PS you might need to access fields errors as well, you can do this with {{ form.username.erros }}

Related

Applying different CSS style for each element in For Loop - Django

I'm building a blog with Django and i'm trying to apply my static design to dynamic HTML templates. I have several CSS classes for diffrenet card types (regular, medium and large), and i'm builing a posts page that should display all posts from all classes.
I'm trying to find a solution for looping through every post and applying different CSS class accordingly.
To sum it up:
i have a posts list in my db
i want to loop over all the posts and assign different pre-defined CSS class to different posts (meaning the "class" should be different for some posts, some should have class "card" and some "large-card", etc)
Any help?
Here is a sample of the html code i refer to:
{% for post in post_list %}
<div class="card">
...
</div>
{% endfor %}
You have various possibilities to do that:
How about just using a the built-in forloop variable (scroll down a bit for a list of availabel variables):
{% for post in post_list %}
<div class="card{{ forloop.counter }}"> {{ post }} </div>
{% endfor %}
You could also make post_list a list of tuples in the backend, i.e. something like post_list = [ ("a", "class_a"), ("b", "class_b")]. Then in the template you can unpack this:
{% for post in post_list %}
<div class="card{{ post.1 }}"> {{ post.0 }}</div>
{% endfor %}
There are more, you can use the built-in regroup for example
I was managed so solve this issue using “Slice” and choosing different posts and then assign different classes when looping over the posts in the database.

Drupal overwrite view default markup

I am new in Drupal 8 and trying to build a custom theme. Overwriting a theme works well for paragraphs, blocks etc. but not for views. I need your help to tell me how to override the markup of a view. I already created a the .twig file in the custom folder and tested it. It works.
That's what I have inside:
{% for row in rows %}
<div{{ row.attributes.addClass(row_classes) }}>
{{ row.content }}
</div>
{% endfor %}
The {{ row.content }} gives me every element I need in the default Drupal view markup. I want to costumize the HTML. such as:
<h1 class="headline">
{row.title}
</h1>
<p class="text">
{row.text}
</p>
<img src="{row.image}" />
I didn't find a way to get the values such as the title from the row. I tried things like row.{field_name} or row.content.{field_name} but nothing works yet. Is there anyway to access these fields by machine name?
The easy way to display the contents would be to set the views to content list with a view mode. For e.g: If you want to display a node content type article with display mode teaser then you will get the node--article--teaser.html.twig getting rendered.
Then you can modify the the twig file in required format.
<h1 class="headline">
{{ node.title.value }}
</h1>
<p class="text">
{{ node.body.value }}
</p>
<img src="{{ node.field_image.entity.uri.value }}" />

CollectionType custom prototype via form_theme

I want to customize the way an entry of a CollectionType in a form will be rendered, that is the way the prototype is generated, I need my own HTML. So I need to do it with the Twig form_theme, but it doesn't work.
I am currently using Symfony 4.1.2.
Here is what I have for now:
In the template that renders the whole form:
{% form_theme form.additionalEmails.vars.prototype 'form/additionalEmail.html.twig' %}
In the form/additionalEmail.html.twig file:
{% block form_row %}
<div style="margin: 10px 0">
{{ form_label(form) }}
{{ form_widget(form.children.value) }}
<div>Recevoir les mails de type : {{ form_row(form.children.isUsedForAdminCommChannel) }}{{ form_row(form.children.isUsedForInfosPratiquesCommChannel) }}{{ form_row(form.children.isUsedForAgendaCommChannel) }}</div>
</div>
{% endblock %}
If I do that I get this error:
Key "value" does not exist as the array is empty.
What is weird is that if I replace the above code by this (by commenting the old one and adding the new line):
{{ dump(form.children) }}
I see that form.children is an array with 4 items and one named "value" so it is clearly broken.
Your help would be much appreciated, as I'm just an amateur dev trying to make a website for a youth movement ^^
Best Regards,
[EDIT]
Here is the form that includes the CollectionType:
https://gist.github.com/MrPOC/a4af94cc5e577a1244dac59de5ea3506
The field is named "additionalEmails"
Just so I understand you correctly. You have a Form which contains a CollectionType and you want to change the prototype of that CollectionType?
If yes could you please post the Form with the CollectionType?
EDIT: Ok so here's how to overwrite the prototype template for a CollectionType.
First we have to find out what your forms unique_block_prefix is. You can find this by going to the page where this form is rendered, you then click in the symfony debug bar on the form icon. Then you have to expand the View variables block and look for unique_block_prefix.
I assume your block prefix could look like this _user_type the name of the block you would overwrite would then be _user_type_additionalEmails_entry_widget (for more details please read the link provided below)
You can replace the widget part with row or label depending on what you want to overwrite (I would suggest trying to overwrite them all to see what exactly changes because I don't know of the top of my head)
In twig you would then take your block prefix and overwrite the prototype field in the same way you would overwrite any other field
{% block '_user_type_additionalEmails_entry_widget' %}
//Content
{% endblock %}
What I wrote down here is what I found out after reading https://github.com/symfony/symfony-docs/issues/6056 (read HeahDude' comment specifically)
I hope this helps!

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

Symfony Form Theme extend inline

I extend FosUserBundle register form like this:
{% extends "#App/base.html.twig" %}
{% form_theme form 'bootstrap_3_horizontal_layout.html.twig' %}
{% block form_label_class -%}
col-md-8
{%- endblock form_label_class %}
Unfortunately the block form_label_class which is from bootstrap_3_horizontal_layout.html.twig is not extending there.
I want now in my form to use label as col-md-8 (instead the default col-sm-2) but then in another form maybe want to use col-md-6 and so on.
Is there an easy way to do it inline in every from instead create extends for each col-md-XX which is really not very convenient at all.
Thanks a lot!
You need to tell Symfony to include your current template as a form theme:
{% form_theme form with [
'bootstrap_3_horizontal_layout.html.twig',
_self,
] %}
Otherwise, it doesn't know to look in the current template for form theme blocks.
You can also use this to include other templates with form blocks as well. This is helpful if you have multiple templates that re-use the same form blocks; you can refactor them all into one template and reference it everywhere it's needed.

Resources