Symfony 3.4 - Passing variables in my modals? - symfony

I would like to know if it is possible to pass variables in a modal using Symfony?
Before integrating a modal, my line looked like
<span class="glyphicon glyphicon-remove"></span> Supprimer
It is used to delete a line from my database.
I wanted to add a modal sign confirmation request.
Then I added a modal and now I have this:
<span class="glyphicon glyphicon-remove"></span> Supprimer
The modal :
<div class="modal" id="mymodal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<a href="#HereIWantMyVariablesForMyControllerPathRendering" type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Is there a parameter to put in my link calling the modal, to place the variables?
EDIT:
After using macros, by Fabian, here is my code:
Views/macro/macro.html.twig:
{%- macro create_delete_modal(body, deleteLink) -%}
{% filter spaceless %}
<div id="deleteModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Supprimer?</h4>
</div>
<div class="modal-body">
<p>{{ body }}</p>
</div>
<div class="modal-footer">
Supprimer
<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
</div>
</div>
</div>
</div>
{% endfilter %}
{%- endmacro -%}
Controller delete fonction:
/**
* Suppression d'un package | Suppression d'un type d'utilisateur attribué à un package
*
* #Route("/{id}/{type}/delete", name="paquets_delete")
*/
public function deleteAction(Request $request, $id, $type)
{
$em = $this->getDoctrine()->getManager();
$unPaquet = $em->getRepository('PagesBundle:Paquet')->find($id); //Récupération du package
$nbTypes = count($unPaquet->getTypeUser()); //Récupération du nombre de TypeUser auquel est attribué le package
if($nbTypes == 1)
{
$em->remove($unPaquet); // Si un seul destinataire, on supprimme le package.
}
else if($nbTypes > 1)
{
$am = $this->getDoctrine()->getManager();
$leType = $am->getRepository('PagesBundle:TypeUser')->findByTypeUtilisateur($type);
$unPaquet->deleteTypeFromTypesUser($leType[0]); // Si plusieurs destinataires, on supprimme juste le type d'utilisateur passé en paramètre
}
$em->flush(); // Validation
$this->getDoctrine()->getManager()->getRepository('PagesBundle:User')->updateNbDDLAll("dec"); // A la suppression d'un package, on décrémente le nombre de télécharme
$this->addFlash( // Message d'information à l'admin
'info_delete',
'Package supprimée !'
);
return $this->redirectToRoute('paquets_index'); // Actualisation de la page
}
index.html.twig :
{% extends 'base.html.twig' %}
{% import 'macro/macro.html.twig' as macro %}
{% block body %}
<br/>
<span class="glyphicon glyphicon-plus"></span> Ajouter un package
<br/> <br/>
{% for unType in typesUser %}
{% set i=loop.index0 %}
<h1 class="text-primary text-center">{{unType['typeUtilisateur']}} <span class="badge badge-primary badge-pill w3-small">{{ tabInfos[i]|length }}</span></h1>
<br />
<table class="table table-stripped">
<thead>
<tr>
<th>Id</th>
<th>Titre</th>
<th>Package</th>
<th>Notice</th>
<th>Commentaire</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for uneInfo in tabInfos[i] %}
<tr>
<td>{{ uneInfo.id }}</td>
<td>{{ uneInfo.titre }}</td>
<td><a href="{{ path('paquet_fileDDL', { 'id': uneInfo.id, 'type': 'package'}) }}"</a>{{ uneInfo.urlPaquet }} </td>
<td><a href="{{ path('paquet_fileDDL', { 'id': uneInfo.id, 'type': 'notice'}) }}"</a>{{ uneInfo.urlNotice }} </td>
<td>{{ uneInfo.commentaire }}</td>
<td>
<span class="glyphicon glyphicon-edit"></span> Modifier
{#<span class="glyphicon glyphicon-remove"></span> Supprimer #}
<button class="btn btn-danger" type="button" data-toggle="modal" data-target="#deleteModal"><span class="glyphicon glyphicon-remove"></span> Supprimer</button>
{{ macro.create_delete_modal('Voulez-vous vraiment supprimer ce package ? ', path('paquets_delete', {'id':uneInfo.id, 'type': unType['typeUtilisateur']})) }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br/><br/>
{% endfor %}
{% endblock %}
The most important part of the html.twig:
{% for uneInfo in tabInfos[i] %}
<tr>
<td>{{ uneInfo.id }}</td>
<td>{{ uneInfo.titre }}</td>
<td><a href="{{ path('paquet_fileDDL', { 'id': uneInfo.id, 'type': 'package'}) }}"</a>{{ uneInfo.urlPaquet }} </td>
<td><a href="{{ path('paquet_fileDDL', { 'id': uneInfo.id, 'type': 'notice'}) }}"</a>{{ uneInfo.urlNotice }} </td>
<td>{{ uneInfo.commentaire }}</td>
<td>
<span class="glyphicon glyphicon-edit"></span> Modifier
<button class="btn btn-danger" type="button" data-toggle="modal" data-target="#deleteModal"><span class="glyphicon glyphicon-remove"></span> Supprimer</button>
{{ macro.create_delete_modal('Voulez-vous vraiment supprimer ce package ? ', path('paquets_delete', {'id':uneInfo.id, 'type': unType['typeUtilisateur']})) }}
</td>
</tr>
{% endfor %}
The problem: it removes the first packages, not the ones I want to delete

I would recomment you to use a macro for your modal. This one you can reuse everytime you want. This would look something like this:
{%- macro create_delete_modal(body, deleteLink) -%}
{% filter spaceless %}
<div id="deleteModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Supprimer?</h4>
</div>
<div class="modal-body">
<p>{{ body }}</p>
</div>
<div class="modal-footer">
Supprimer
<button type="button" class="btn btn-default" data-dismiss="modal">close</button>
</div>
</div>
</div>
</div>
{% endfilter %}
{%- endmacro -%}
And to use it:
{% import 'macro/macro.html.twig' as macro %}
{{ macro.create_delete_modal('Are you sure you want to delete this?', path('paquets_delete', { 'id': uneInfo.id, 'type':unType['typeUtilisateur'] }) }}
<button class="btn btn-danger" type="button" data-toggle="modal" data-target="#deleteModal"><span class="glyphicon glyphicon-remove"></span> Supprimer</button>
Edit
The id of the modal (id="#deleteModal") is not unique any more, as you loop over it. Simply add the current loop index to it and it will work as expected.
{%- macro create_delete_modal(id, body, deleteLink) -%}
{% filter spaceless %}
<div id="{{ id }}" class="modal fade" role="dialog">
And in your loop pass the loop index:
{% for uneInfo in tabInfos[i] %}
<tr>
<td>
<button class="btn btn-danger" type="button" data-toggle="modal" data-target="#deleteModal-{{ uneInfo.id }}"><span class="glyphicon glyphicon-remove"></span> Supprimer</button>
{{ macro.create_delete_modal('deleteModal-'~uneInfo.id, 'Voulez-vous vraiment supprimer ce package ? ', path('paquets_delete', {'id':uneInfo.id, 'type': unType['typeUtilisateur']})) }}
</td>
</tr>
{% endfor %}

Related

Call JavaScript function from Twig (symfony 5)

I'm working on a Symfony5 project and I want to call a JavaScript function and pass an Ajax Request (which I get from a controller) from a Twig.
i create a function phoneverif() in order to do some verification before the validation of form ..like doing a test if the fields are blanks then an alert will appears also the verification of the existence of data in db.
But a first, very simple test already failed, and an error appear :
Uncaught ReferenceError: phoneverif is not defined
onclick http://localhost:8000/super_admin/telephone/nouveau:1
here is my twig code :
{% extends 'superadmin.html.twig'%}
{% block body %}
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Nouveau téléphone</h1>
</div>
<div class="col-md-12">
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i>{{ flashMessage }}</h4>
</div>
{% endfor %}
<!-- general form elements -->
<div>
<!-- form start -->
{{ form_start(form)}}
<div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.userGroup) }}</label>
{{ form_widget(form.userGroup) }}
<span class="text-danger">{{ form_errors(form.userGroup) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.protocol) }}</label>
{{ form_widget(form.protocol) }}
<span class="text-danger">{{ form_errors(form.protocol) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.extension) }}</label>
{{ form_widget(form.extension) }}
<span class="text-danger">{{ form_errors(form.extension) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.dialplanNumber) }}</label>
{{ form_widget(form.dialplanNumber) }}
<span class="text-danger">{{ form_errors(form.dialplanNumber) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.voicemailId) }}</label>
{{ form_widget(form.voicemailId) }}
<span class="text-danger">{{ form_errors(form.voicemailId) }}</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.login) }}</label>
{{ form_widget(form.login) }}
<span class="text-danger">{{ form_errors(form.login) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.pass) }}</label>
{{ form_widget(form.pass) }}
<span class="text-danger">{{ form_errors(form.pass) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.confSecret) }}</label>
{{ form_widget(form.confSecret) }}
<span class="text-danger">{{ form_errors(form.confSecret) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.serverIp) }}</label>
{{ form_widget(form.serverIp) }}
<span class="text-danger">{{ form_errors(form.serverIp) }}</span>
</div>
</div>
</div>
</div>
<div>
<button type="button" class="btn btn-primary" onclick="phoneverif();">Ajouter</button>
<a class="btn btn-warning" href="{{ path('Les_telephones') }}">Retour</a>
</div>
{{ form_end(form)}}
</div>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script type="text/javascript">
function phoneverif() {
if ($("#telephone_extension").val() == '') {
alert('Vous devez saisir une extension');
} else {
if ($("#telephone_dialplanNumber").val() == '') {
alert('Vous devez saisir le dialplan du téléphone');
} else {
if ($("#telephone_login").val() == '') {
alert('Vous devez saisir le login du téléphone');
} else {
if ($("#telephone_pass").val() == '') {
alert('Vous devez saisir le mot de passe du téléphone');
} else {
$.ajax({
type: 'get',
url: Routing.generate('phone_verif', {extension: $("#telephone_extension").val()}),
success: function (data) {
if (data.existe == 1) {
alert('Téléphone non valide');
} else {
$("form").submit();
}
},
error: function() { alert('Erreur lors de l'appel AJAX'); }
});
}
}
}
}
}
</script>
{% endblock %}
function verifphone :
public function Phoneverif1(Request $request , $extension){
if ($request->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$phone = $em->getRepository(Phones::class)->findOneByExtension($extension);
if ($phone) {
$existe = 1;
} else {
$existe = 0;
}
$response = new JsonResponse();
return $response->setData(array('existe' => $existe));
} else {
throw new \Exception('Erreur');
}
}
my fosjsrouting configuration :
config/packages/routing.yaml:
framework:
router:
utf8: true
# Configure how to generate URLs in non-HTTP contexts, such as CLI commands.
# See https://symfony.com/doc/current/routing.html#generating-urls-in-commands
#default_uri: http://localhost
fos_js_routing:
routes_to_expose: [ phone_verif ]
config/routes/fos_js_routing.yaml:
fos_js_routing:
resource: "#FOSJsRoutingBundle/Resources/config/routing/routing.xml"
routes.yaml :
verification_telephone:
path: /verification/phone/{extension}
controller: App\Controller\TelephoneController::Phoneverif1
options:
expose: true
Rename the function and try again
I solved this problem by renaming the function. You try too.
Phoneverif1()
{% extends 'superadmin.html.twig'%}
{% block body %}
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Nouveau téléphone</h1>
</div>
<div class="col-md-12">
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i>{{ flashMessage }}</h4>
</div>
{% endfor %}
<!-- general form elements -->
<div>
<!-- form start -->
{{ form_start(form)}}
<div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.userGroup) }}</label>
{{ form_widget(form.userGroup) }}
<span class="text-danger">{{ form_errors(form.userGroup) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.protocol) }}</label>
{{ form_widget(form.protocol) }}
<span class="text-danger">{{ form_errors(form.protocol) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.extension) }}</label>
{{ form_widget(form.extension) }}
<span class="text-danger">{{ form_errors(form.extension) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.dialplanNumber) }}</label>
{{ form_widget(form.dialplanNumber) }}
<span class="text-danger">{{ form_errors(form.dialplanNumber) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.voicemailId) }}</label>
{{ form_widget(form.voicemailId) }}
<span class="text-danger">{{ form_errors(form.voicemailId) }}</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.login) }}</label>
{{ form_widget(form.login) }}
<span class="text-danger">{{ form_errors(form.login) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.pass) }}</label>
{{ form_widget(form.pass) }}
<span class="text-danger">{{ form_errors(form.pass) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.confSecret) }}</label>
{{ form_widget(form.confSecret) }}
<span class="text-danger">{{ form_errors(form.confSecret) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.serverIp) }}</label>
{{ form_widget(form.serverIp) }}
<span class="text-danger">{{ form_errors(form.serverIp) }}</span>
</div>
</div>
</div>
</div>
<div>
<button type="button" class="btn btn-primary" onclick="Phoneverif1()">Ajouter</button>
<a class="btn btn-warning" href="{{ path('Les_telephones') }}">Retour</a>
</div>
{{ form_end(form)}}
</div>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script type="text/javascript">
function Phoneverif1() {
if ($("#telephone_extension").val() == '') {
alert('Vous devez saisir une extension');
} else {
if ($("#telephone_dialplanNumber").val() == '') {
alert('Vous devez saisir le dialplan du téléphone');
} else {
if ($("#telephone_login").val() == '') {
alert('Vous devez saisir le login du téléphone');
} else {
if ($("#telephone_pass").val() == '') {
alert('Vous devez saisir le mot de passe du téléphone');
} else {
$.ajax({
type: 'get',
url: Routing.generate('phone_verif', {extension: $("#telephone_extension").val()}),
success: function (data) {
if (data.existe == 1) {
alert('Téléphone non valide');
} else {
$("form").submit();
}
},
error: function() { alert('Erreur lors de l'appel AJAX'); }
});
}
}
}
}
}
</script>
{% endblock %}

Grid and cards issues with Bootstrap

I almost finish a very important part in my Django project by creating panels which contains some cards. I'm using Bootstrap 3 (BS3) and I integrated cards from BS4 to BS3.
I'm getting an issue and I would like to get your mind because the behaviour is a little bit weird.
My issue :
As you can see in the animated picture below, there is an offset when I open dropdown from Publication n°1 and Publication n°2. It creates an offset with cards from the second row.
[![enter image description here][1]][1]
My question is : How I can rewrite bootstrap part in my code in order to make a normal behaviour ?
What I would like to get for each card:
This is what I would like for each card : open the card > make an offset to the entire row below
[![enter image description here][2]][2]
What I have and I don't want as behaviour :
And don't have a behaviour like that :
[![enter image description here][3]][3]
My code :
This is my entire code :
{% for category in research_categories|dictsort:'name' %}
<div class="panel-group accordion" id="accordion_{{ category.id }}" role="tablist"
aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion_{{ category.id }}"
href="#collapseOne_{{ category.id }}" aria-expanded="true" aria-controls="collapseOne">
<i class="more-less glyphicon glyphicon-plus"></i>
{{ category }}
{% for item in category.publication.all %}
{% if item.new_publication == True %}
<span class="glyphicon glyphicon-flag"></span>
{% endif %}
{% endfor %}
</a>
</h4>
</div>
<div id="collapseOne_{{ category.id }}" class="panel-collapse collapse" role="tabpanel"
aria-labelledby="#accordion_{{ category.id }}">
<div class="panel panel-default subpanel ">
<div class="row">
{% for element in research_publications|dictsort:'pub_id' %}
{% if element.category|stringformat:"s" == category|stringformat:"s" %}
<div class="col-md-4">
<div class="card" style="width:350px">
<img class="card-img-top" style="width: 350px; height: 350px" src="{{ element.cover.url }}" alt="Card image">
<div class="card-body">
<h4 class="card-title">{{ element }} - {{ element.pub_id }}
{% if element.new_publication == True %}
<span class="glyphicon glyphicon-flag"></span>
{% endif %}
</h4>
<button class="btn btn-default display-document" type="button" data-toggle="collapse"
data-target="#dropdown_{{ element.id }}"
aria-expanded="false"><span
class="glyphicon glyphicon-chevron-down"></span></button>
<div id="dropdown_{{ element.id }}" class="collapse">
<div class="publication-title">
<table class="table table-condensed">
<tbody>
{% for item in test_research %}
{% if item.publication|stringformat:"s" == element|stringformat:"s" %}
<tr>
<td class="col-md-1">
<input type="checkbox" class="fakeRadio" name="DocumentChoice"
value="{{ item.code }}">
</td>
<td class="col-md-5 document-title">{{ item.title }}</td>
<td class="col-md-1 document-language"> {{ item.language }}</td>
<td class="col-md-1">
{% if item.format == 'pdf' %}
<img src="{% static 'app/img/pdf-icon.png' %}"
style="width:20px; height:20px;"/>
{% endif %}
{% if item.format == 'epub' %}
<img src="{% static 'app/img/epub-icon.png' %}"
style="width:20px; height:20px;"/>
{% endif %}
</td>
<td class="col-md-1 document-flag">
{% if item.publication.new_publication == True %}
<span class="glyphicon glyphicon-flag"></span>
{% else %}
<span></span>
{% endif %}
</td>
<td class="col-md-1 document-cover">
{% if item.publication.cover %}
<a href="{{ item.publication.cover.url }}">
<img class="popup_image"
style="width:20px; height:20px; border-radius:2px;"
id="myImg_{{ item.id }}" src="{{ item.publication.cover.url }}">
</a>
{% endif %}
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
And this is a simplified version from my previous code in order to try some things :
{% for category in research_categories|dictsort:'name' %}
<div class="panel-group accordion" id="accordion_{{ category.id }}" role="tablist"
aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion_{{ category.id }}"
href="#collapseOne_{{ category.id }}" aria-expanded="true" aria-controls="collapseOne">
<i class="more-less glyphicon glyphicon-plus"></i>
{{ category }}
</a>
</h4>
</div>
<div id="collapseOne_{{ category.id }}" class="panel-collapse collapse" role="tabpanel"
aria-labelledby="#accordion_{{ category.id }}">
<div class="panel panel-default subpanel ">
<div class="row">
{% for element in research_publications|dictsort:'pub_id' %}
{% if element.category|stringformat:"s" == category|stringformat:"s" %}
<div class="col-md-4">
<div class="card" style="width:350px">
<img class="card-img-top" style="width: 350px; height: 350px" src="{{ element.cover.url }}" alt="Card image">
<div class="card-body">
<h4 class="card-title">{{ element }}</h4>
<button class="btn btn-default display-document" type="button" data-toggle="collapse"
data-target="#dropdown_{{ element.id }}"
aria-expanded="false"><span
class="glyphicon glyphicon-chevron-down"></span></button>
<div id="dropdown_{{ element.id }}" class="collapse">
<div class="publication-title">
<table class="table table-condensed">
<tbody>
{% for item in test_research %}
{% if item.publication|stringformat:"s" == element|stringformat:"s" %}
<tr>
<td class="col-md-5 document-title">{{ item.title }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
I don't know if you need my css part, but I think the issue is maybe due to my bootstrap grid ?
Thank you !
That looks like a clearfix issue. Can you try adding this to your css:
.panel.panel-default.subpanel > .row > .col-md-4:nth-child(3n+1) {
clear: both;
}
Depending on your other code it might need some more variations for different screen sizes.

Symfony Datatables button pages don't work

I'm using DataTables but I don't understand why in table my symfony page don't work when i press page 2,3 etc..
my html code is
{% extends 'base.html.twig' %}
{% block body %}
<div class="container">
{{ form_start(form,{'attr':{'novalidate':'novalidate'}}) }}
<div class="row" >
<div class=" col-md-4 form-group">
{{ form_row(form.data_inizio,{'attr':{'class':'form-control'}}) }}
</div>
<div class=" col-md-4 form-group">
{{ form_row(form.data_fine,{'attr':{'class':'form-control'}}) }}
</div>
<div class="col-md-4 form-group">
{{ form_row(form.Stampa,{'attr':{'class':'form-control'}}) }}
</div>
</div>
</div>
{{ form_end(form) }}
{#</div>#}
<div class=row">
<div class="col-md-6">
<h1>Elenco Analisi</h1>
</div>
<div class="col-md-6">
{# <p style="text-align:right;width:100%;">
<span style="font-weight:bold;">Totale:</span> <input type="text" id="totale" name="totale" maxlength="50" disabled/>
</p>#}
</div>
</div>
<table id="tblSearch" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Id</th>
<th>Codice</th>
<th>Effettuato da</th>
<th>Data</th>
<th>Proprietario</th>
<th>Autorizzata da </th>
<th>Laboratorio</th>
<th>Edit</th>
<th>Stampa</t>
<th>Accettazione</th>
<th>Totale</th>
<th>Totale Scontato</th>
<th>File</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Id</th>
<th>Codice</th>
<th>Effettuato da</th>
<th>Data</th>
<th>Proprietario</th>
<th>Autorizzata da </th>
<th>Laboratorio</th>
<th>Edit</th>
<th>Stampa</th>
<th>Accettazione</th>
<th>Totale</th>
<th>Totale Scontato</th>
<th>File</th>
</tr>
</tfoot>
<tbody>
{% for singola_analisi in analisi %}
<tr>
<td>{{singola_analisi.id}}</td>
<td>{{singola_analisi.codice}}</td>
<td>{{singola_analisi.idUser.nome}}</td>
<td>{{ singola_analisi.data|date('d-m-Y') }}</td>
<td>{{singola_analisi.idProprietario.ragionesociale}} </td>
<td>{{singola_analisi.autoriazzatada.nome}} {{singola_analisi.autoriazzatada.cognome}}</td>
<td>{{singola_analisi.idLaboratorio.nome}}</td>
{# <td><i class="glyphicon glyphicon-plus"></i></td>#}
{# #}
{% if is_granted('ROLE_ADMIN') %}
{# se è sua l'analisi#}
{% if user.id==singola_analisi.iduser.id %}
{% if singola_analisi.stato == 1 %}
<td><i class="fa fa-cogs" aria-hidden="true"></i></td>
<td><i class="fa fa-file" aria-hidden="true"></i></td>
<td>
<button type="button">Accetta</button>
<button style="margin-top:2px;" type="button">Rifiuta </button>
</td>
{% elseif singola_analisi.stato == 2%}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><i class="fa fa-file" aria-hidden="true"></i></td>
<td>
<button type="button">Accetta</button>
<button style="margin-top:2px;" type="button">Rifiuta </button>
</td>
{% else %}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><i class="fa fa-file" aria-hidden="true"></i></td>
<td>Accettata</td>
{% endif %}
{% else %}
{% if singola_analisi.stato == 1 %}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td>In Elaborazione</td>
{% elseif singola_analisi.stato == 2%}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><i class="fa fa-file" aria-hidden="true"></i></td>
<td>
<button type="button">Accetta</button>
<button style="margin-top:2px;" type="button">Rifiuta </button>
</td>
{% else %}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><i class="fa fa-file" aria-hidden="true"></i></td>
<td>Accettata</td>
{% endif %}
{% endif %}
{% else %}
{% if singola_analisi.stato == 1 %}
<td><i class="fa fa-cogs" aria-hidden="true"></i></td>
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><button type="button">Richiedi accettazione</button></td>
{% elseif singola_analisi.stato == 2%}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td>Accettazione in attesa</td>
{% else %}
<td><i class="fa fa-times" aria-hidden="true"></i></td>
<td><i class="fa fa-file" aria-hidden="true"></i></td>
<td>Accettato</td>
{% endif %}
{% endif %}
<td>{{singola_analisi.totale}}</td>
<td>{{singola_analisi.totalescontato}}</td>
<td>
{% if singola_analisi.path != NULL %}
<i class="fa fa-download" aria-hidden="true"></i>
{% else %}
<i class="fa fa-upload" aria-hidden="true"></i>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('js/lista_modulo.js') }}"></script>
{% endblock %}
My js code is:
$( document ).ready(function() {
$('#tblSearch').DataTable( {
"columnDefs": [
{
"targets": [ 0 ],
"visible": false,
"searchable": false,
},
],
"order": [[0, 'desc']],
} );
});
and in my base.html file there is:
{% block stylesheets %}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
{# <link href="{{ asset('css/jqueryautocomplete/autocomplete.css') }}" rel="stylesheet">#}
<link href="{{ asset('css/bootstrap-chosen/bootstrap-chosen.css') }}" rel="stylesheet">
<link href="{{ asset('css/template_base.css') }}" rel="stylesheet">
<link rel="stylesheet" href="{{asset('css/font-awesome/css/font-awesome.min.css')}}">
<link href="{{asset('js/plugins/data-tables/css/jquery.dataTables.min.css')}}" type="text/css" rel="stylesheet" media="screen,projection">
{% endblock %}
{% block javascripts %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="{{asset('js/jqueryautocomplete/chosen.jquery.js')}}"</script>
<script type="text/javascript" src="{{asset('js/plugins/data-tables/data-tables-script.js')}}"></script>
<script type="text/javascript" src="{{asset('js/plugins/data-tables/js/jquery.dataTables.js')}}"></script>
<script type="text/javascript" src="{{asset('js/plugins/data-tables/js/dataTables.bootstrap.js')}}"></script>
{% endblock %}
i try to load also a example from tutorial but still page button don't work .
I see some forum but noting help me to undestard my problem .
Please can help me to understand?
Thanks

Align items in same row boot

I've been trying to show items on the same line in bootstrap here is my code, it's was working before , but i think i messed it up , i'm kinda new to bootstrap, any help would be much appreciated.
<div class="row">
{% for album in all_albums %}
<div class="col-sm-4 col-lg-2">
<div class="thumbnail">
<a href="{% url 'music:detail' album.id %}">
<img src="{{ album.album_logo.url }}" class="img-responsive">
</a>
<div class="caption">
<h2>{{ album.album_title}}</h2>
<h4>{{ album.artist}}</h4>
<!-- Details -->
Details
<!-- Delete -->
<form action="{% url 'music:album-delete' album.id %}" method="post" style="display: inline;">
{% csrf_token %}
<input type="hidden" name="album_id" value="{{ album.id}} "/>
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span> Delete
</button>
</form>
<!--Favorite-->
<a href="#" class="btn btn-default btn-sm btn-favorite">
<span class="glyphicon glyphicon-star">{% if album.is_favorite%}active{% endif %}</span>
</a>
</div>
</div>
</div>
</div>
</div>

Twig {% if statement %} using Symfony 2

How can I access an entity attribute in Twig (using the Winzou Symfony2 tutorial on OpenClassroom)?
Category is an attribute of my class Advert which contains all my adverts. I just want to show an error message if there are no Advert entities.
Here is my accordion that lists "adverts" from my Advert entity. One accordion is for the category incident and the one other is for the category general.
<div class="well">
<div class="accordion" id="accordion2">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseGen">
<h3><i class="icon-chevron-right"></i> Incidents</h3>
</a>
</div>
<div id="collapseGen" class="accordion-body collapse">
<div class="accordion-inner">
<div class="accordion" id="accordion4">
{% for advert in listAdverts %}
{% if advert.category == "incident" %}
<div class="accordion-group">
<div class="accordion-heading decalage">
<a href="{{ path('info_view', {'id': advert.id}) }}">
<h3 class="{{ advert.category }}">{{ advert.title }}</h3>
</a>
<div>
<span>{{ advert.content|truncate(100, true, '...')|raw }}</span>
</div>
<div>
{% if (advert.UpdatedAt is empty) %}
<i>Créé par {{ advert.author }}, le {{ advert.date|date('d/m/Y') }}
à {{ advert.date|date('H:i') }}</i>
{% else %}
<i>Modifié par {{ advert.author }},
le {{ advert.updatedAt|date('d/m/Y') }}
à {{ advert.updatedAt|date('H:i') }}</i>
{% endif %}
</div>
<div>{% if advert.readers is not empty %}
<i>Vu par :
{% for reader in advert.readers %}
{{ reader.username }},
{% endfor %}
{% endif %}</i>
</div>
<br>
<div>
{% if is_granted("IS_AUTHENTICATED_FULLY") %}
<p>
<a href="{{ path('info_edit', {'id': advert.id}) }}"
class="btn btn-default">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
Modifier
</a>
<a href="#myModal{{ advert.id }}" role="button" class="btn btn-danger"
data-toggle="modal">
<i class="fa fa-trash-o" aria-hidden="true"></i>
Supprimer
</a>
</p>
<div id="myModal{{ advert.id }}" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h3>Suppression annonce "{{ advert.title }}"</h3>
</div>
<div class="modal-body">
<p>Voulez-vous vraiment supprimer l'annonce : "{{ advert.title }}" ? </p>
</div>
<div class="modal-footer parente">
<div>Annuler</div>
<div>
<form class="nomargin"
action="{{ path('info_delete', {'id': advert.id}) }}"
method="post">
<input type="submit" value="Supprimer"
class="btn btn-danger"/>{{ form_rest(form) }}
</form>
</div>
</div>
</div>
{% endif %}
</div>
</div>
</div>
{# HERE I DONT KNOW HOW TO DO IT #}
{% else %} No Advert !
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseCo">
<h3><i class="icon-chevron-right"></i> Général</h3>
</a>
</div>
<div id="collapseCo" class="accordion-body collapse">
<div class="accordion-inner">
<div class="accordion" id="accordion4">
{% for advert in listAdverts %}
{% if advert.category == "general" %}
<div class="accordion-group">
<div class="accordion-heading decalage">
<div class="accordion-heading">
<a href="{{ path('info_view', {'id': advert.id}) }}">
<h3 class="{{ advert.category }}">{{ advert.title }}</h3>
</a>
<span>{{ advert.content|truncate(100, true, '...')|raw }}</span>
<div>
{% if (advert.UpdatedAt is empty) %}
<i>Créé par {{ advert.author }}, le {{ advert.date|date('d/m/Y') }}
à {{ advert.date|date('H:i') }}</i>
{% else %}
<i>Modifié par {{ advert.author }},
le {{ advert.updatedAt|date('d/m/Y') }}
à {{ advert.updatedAt|date('H:i') }}</i>
{% endif %}
</div>
<div>{% if advert.readers is not empty %}
<i>Vu par :
{% for reader in advert.readers %}
{{ reader.username }},
{% endfor %}
{% endif %}</i>
</div>
<br>
<div>
{% if is_granted("IS_AUTHENTICATED_FULLY") %}
<p>
<a href="{{ path('info_edit', {'id': advert.id}) }}"
class="btn btn-default">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
Modifier
</a>
<a href="#myModal{{ advert.id }}" role="button" class="btn btn-danger"
data-toggle="modal">
<i class="fa fa-trash-o" aria-hidden="true"></i>
Supprimer
</a>
</p>
<div id="myModal{{ advert.id }}" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h3>Suppression annonce "{{ advert.title }}"</h3>
</div>
<div class="modal-body">
<p>Voulez-vous vraiment supprimer l'annonce : "{{ advert.title }}" ? </p>
</div>
<div class="modal-footer parente">
<div>Annuler</div>
<div>
<form class="nomargin"
action="{{ path('info_delete', {'id': advert.id}) }}"
method="post">
<input type="submit" value="Supprimer"
class="btn btn-danger"/>{{ form_rest(form) }}
</form>
</div>
</div>
</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
My error message " No Adverts !" doesnt appears..
EDIT : According to Mitchel's answer, i've tried this :
{% endif %}
{% else %}<li>No Adverts</li>
{% endfor %}
I don't know why it doesn't work when one advert is in the other category...
{% for advert in listAdverts if advert.category == "incident" %}
{% if listAdverts is empty%}
<li>No Adverts</li>
{% endif %}
{% endfor %}
That code does not work.
I'm not sure to understand your question, but if so, you can maybe try a thing like this according to the documentation:
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no user found</em></li>
{% endfor %}
In Twig a for statement can have an associated else. It basically means that if the for has no results, the else is used. You should put the {% endif %} before the {% else %} and it should work.

Resources