I have 2 Entities: Clinic, Vet, for which I created CRUD templates with app/console generate:doctrine:crud
I first created the Clinic entity and created my "admin template" in AgriHealth/AhpBundle/Resources/views/admin.html.twig and then extend this in
AgriHealth/AhpBundle/Resources/views/Clinic/index.html.twig:
{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
This worked.
Then I created Entity Vet and ran the crud generator. Again I'm extending:
AgriHealth/AhpBundle/Resources/views/Vet/index.html.twig:
{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
But this seems to be ignored, as the layout from my admin template doesn't come through. I have tried:
app/console cache:clear
renaming admin.html.twig: causes an error in both views as expected
I must be missing something? Any ideas?
Twig code below:
src/AgriHealth/AhpBundle/Resources/views/admin.html.twig
{% extends '::base.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('bundles/agrihealthahp/css/admin.css') }}" rel="stylesheet" />
<link href="{{ asset('bundles/agrihealthsecurity/css/admin.css') }}" rel="stylesheet" />
{% endblock %}
{% block body -%}
<div class="row" id="header">
<div class="small-12 columns">
<h1><a href=""><img src="/bundles/agrihealthahp/images/agrihealth-logo.png" />
<span>Animal Health Planner</span></a></h1>
</div>
</div>
<div class="row" id="menu">
<div class="small-12 columns">
</div>
</div>
<div class="row" id="content">
<div class="small-12 columns">
{% block admin %}{% endblock %}
</div>
</div>
<div class="row" id="black_footer">
<div class="small-12 medium-5 columns footer-black-1">
<div class="moduletable">
<div class="custom">
<p>www.agrihealth.co.nz</p></div>
</div>
</div>
<div class="small-12 medium-7 columns ">
<div class="left footer-black-2">
<div class="moduletable">
<div class="custom">
<p>0800 821 421</p></div>
</div>
</div>
<div class="right footer-black-3">
<div class="moduletable">
<div class="custom">
<p><sup></sup><sup><img style="line-height: 1.1;" src="/bundles/agrihealthahp/images/agrihealth_white.png" alt="agrihealth white"></sup></p></div>
</div>
</div>
</div>
</div>
{% endblock %}
src/AgriHealth/AhpBundle/Resources/views/Clinic/index.html.twig:
{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
{% block admin -%}
<h1>Clinics</h1>
<ul class="actions">
<li>
<a href="{{ path('clinic_new') }}">
Add Clinic
</a>
</li>
</ul>
<table class="records_list">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Fax</th>
<th>After Hours</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.name }}</td>
<td>{{ entity.phone }}</td>
<td>{{ entity.fax }}</td>
<td>{{ entity.afterhours }}</td>
<td>{{ entity.email }}</td>
<td>
<ul class="actions">
<li>
edit
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
src/AgriHealth/AhpBundle/Resources/views/Vet/index.html.twig:
{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
{% block body -%}
<h1>Vets</h1>
<ul class="actions">
<li>
<a href="{{ path('vet_new') }}">
Add Vet
</a>
</li>
</ul>
<table class="records_list">
<thead>
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Clinic</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td><a href="{{ path('vet_edit', { 'id': entity.id }) }}">{{ entity.firstname }} {{ entity.lastname }}</td>
<td>{{ entity.mobile }}</td>
<td>{{ entity.clinic }}</td>
<td>
<ul class="actions">
<li>
edit
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
Check the source of the page for the stylesheets included in admin.html.twig.
If they are there, the problem is that you are overriding your body block.
Change
src/AgriHealth/AhpBundle/Resources/views/Vet/index.html.twig:
{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
{% block body -%}
to
{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
{% block admin -%}
If your stylesheets are not in the page there must be another problem .
Related
I've been working on an online learning website for a school project. I've had trouble passing variales when clicking on a lesson, belonging to a section, inside a course. The idea is that on a course page (formation.html.twig), there's a summary of the course, and you can click on the lessons which are in a sidebar menu. It should then display a page with the same menu, and the content of the lesson showing in place of the course summary (lesson.html.twig).
I've updated the controller so the url goes:
formations/consulter-formationid-sectionid-lessonid
In the view, I wrote {{ path('app_formations_lesson', {'formation':formation.id, 'section': section.id, 'id':lesson.id}) }}
It's working alright for the url and the right values show when I click, but the page refreshes and won't load the right twig view (lesson.html.twig), it loads the same view I'm currently on (formation.html.twig).
FormationsController:
#[Route('/formations/consulter-{id}', name: 'app_formations_see')]
public function see($id): Response
{
$formation = $this->doctrine->getRepository(Formation::class)->findOneById($id);
$section = $this->doctrine->getRepository(Section::class)->findAll();
$lesson = $this->doctrine->getRepository(Lesson::class)->findAll();
return $this->render('formations/formation.html.twig', [
'formation' => $formation,
'sections' => $section,
'lessons' => $lesson
]);
}
#[Route('/formations/consulter-{formation}-{section}-{id}', name: 'app_formations_lesson')]
public function seeLesson($id): Response
{
$lesson = $this->doctrine->getRepository(Lesson::class)->findOneById($id);
return $this->render('formations/lesson.html.twig', [
'lesson' => $lesson
]);
}
formation.html.twig
{% extends 'base.html.twig' %}
{% block title %}{{ formation.title }}{% endblock %}
{% block content %}
<div class="formationcontainer text-center">
{% block sidebar %}
{% include "./formations/sidebar.html.twig" %}
{% endblock %}
<h1>{{ formation.title }} par {{ formation.user.firstname }} {{ formation.user.lastname }}</h1>
{{ formation.description }}
<hr>
<h2>Sommaire</h2>
<div class="tableau">
<table class="table">
{% for section in formation.sections %}
<thead class="table-success">
<tr>
<th scope="col">{{ section.name }}</th>
</tr>
</thead>
<tbody>
{% for lesson in section.lessons %}
<tr>
<td>{{ lesson.title }}</td>
</tr>
{% endfor %}
</tbody>
{% endfor %}
</table>
</div>
</div>
{% endblock %}
lesson.html.twig
{% extends 'base.html.twig' %}
{% block title %}titre de la leçon{% endblock %}
{% block content %}
<div class="formationcontainer text-center">
{% block sidebar %}
{% include "./formations/sidebar.html.twig" %}
{% endblock %}
<h1>Nom de la leçon</h1>
<hr>
<h2>Vidéo</h2>
<h2>Contenu</h2>
</div>
{% endblock %}
sidebar block
<!-- Sidear for lesson pages -->
<nav class="flex-shrink-0flex-shrink-0 p-3 bg-white sidenav">
<button class="btn btn-success" id="sidenav-btn" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarCollapse" aria-expanded="false" aria-controls="collapseOne">
Sommaire
</button>
<div class="list-unstyled ps-0 ul-custom navbar-collapse collapse show" id="sidebarCollapse" aria-expanded="true">
<li class="mb-1">
{% for section in formation.sections %}
<ul class="list-unstyled align-items-center rounded fw-normal">
<li>{{ section.name }}</li>
</ul>
<div>
{% for lesson in lessons %}
<ul class="list-unstyled fw-normal pb-1 small">
<li>{{ lesson.title }}</li>
</ul>
{% endfor %}
</div>
{% endfor %}
</li>
<li class="border-top my-3"></li>
<li class="mb-1">
<ul class="list-unstyled fw-normal pb-1 small">
<li>retour à la liste des formations</li>
</ul>
</li>
</div>
</nav>
Where does this issue come from? How can I display the right view?
Thanks a lot!
edit: I swapped the code in the controller to match the seeLesson function first.
#[Route('/formations/consulter-{formation}-{section}-{id}', name: 'app_formations_lesson')]
public function seeLesson($id): Response
{
$formation = $this->doctrine->getRepository(Formation::class)->findOneById($id);
$section = $this->doctrine->getRepository(Section::class)->findAll();
$lesson = $this->doctrine->getRepository(Lesson::class)->findOneById($id);
return $this->render('formations/lesson.html.twig', [
'lesson' => $lesson,
'formation' => $formation,
'sections' => $section
]);
}
#[Route('/formations/consulter-{id}', name: 'app_formations_see')]
public function see($id): Response
{
$formation = $this->doctrine->getRepository(Formation::class)->findOneById($id);
$section = $this->doctrine->getRepository(Section::class)->findAll();
$lesson = $this->doctrine->getRepository(Lesson::class)->findAll();
return $this->render('formations/formation.html.twig', [
'formation' => $formation,
'sections' => $section,
'lessons' => $lesson
]);
}
It's rendering the wrong page because your URLs are too similar.
When trying to determine what method to execute, Symfony matches the requested URL against the route definitions above the controller methods from top to bottom.
If you're requesting for example /formations/consulter-1-2-3,
Symfony first tries to match this against /formations/consulter-{id}.
This matches if you substitute 1-2-3 for {id}, so it executes the see method.
To fix you have two options:
Either switch the order of the 2 methods in your controller so that the method seeLesson is defined above the see method. Symfony will then first try to match against the route for the seeLesson method.
Or keep the method order as is, but add a constraint to the {id} parameter in the route for the see method, to specify that the matched {id} can only consist of digits:
#[Route('/formations/consulter-{id}', name: 'app_formations_see', requirements: ['id' => '\d+'])]
Now i hav this. How do I group by the field hotel.name?
And yes, I saw this post, but it does not work.
Twig Loop Grouping
{% for item in pagination.items %} <!-- Вывод результатов по отелям -->
<div class="row">
<div class="col-lg-1">{{ loop.index }}</div>
<div class="col-lg-6">
<span class="text-capitalize">{{ item.hotel.name }}</span>
<span class="text-primary">{{ item.roomName }}</span>
{% if(item.mealName) %}
<span class="text-muted">({{ item.mealName }})</span>
{% endif %}
</div>
<div class="col-lg-2">{{ item.price }} {{ item.currency }}</div>
</div>
{% endfor %}
I need get this:
We were advised to use the groop_by, but I do not understand how.
{% for item,
group in pagination|group_by(=>_.hotel.name)
%} <!-- Вывод результатов по отелям -->
<div class="row">
<div class="col-lg-1">{{ loop.index }}</div>
<div class="col-lg-6">
<span class="text-capitalize">{{ item.hotel.name }}</span>
<span class="text-primary">{{ item.roomName }}</span>
{% if(item.mealName) %}
<span class="text-muted">({{ item.mealName }})</span>
{% endif %}
</div>
<div class="col-lg-2">{{ item.price }} {{ item.currency }}</div>
</div>
{% endfor %}
Controller:
public function resultsAction($searchId, $page)
{
$em = $this->getDoctrine()->getManager();
$objSearchRequest = $em->find('UtsHotelBundle:SearchRequest', $searchId);
if(!$objSearchRequest){
$this->createNotFoundException();
}
$objSearchForm = $this->createForm('uts_hotel_search_request', $objSearchRequest);
$templateVars = array(
'searchForm' => $objSearchForm->createView(),
'request' => $objSearchRequest
);
if($objSearchRequest->isComplete() || $objSearchRequest->isOld()){
$repository = $em->getRepository('UtsHotelBundle:SearchResult');
$query = $repository->createQueryForPagination($searchId);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query, $page, 50);
$templateVars['pagination'] = $pagination;
}
return $this->render('UtsHotelBundle:Default:results.html.twig', $templateVars);
}
Answer:
{% set date = null %}
{% set otelindex=0 %}
{% for item in pagination.items %} <!-- Вывод результатов по отелям -->
{% if date != item.hotel.name %}
{% set date = item.hotel.name %}
{% set otelindex=otelindex+1 %}
<div class="row">
<div class="col-lg-6">
<h4>{{ otelindex }}{{ ". " }}{{item.hotel.name}}</h4>
</div>
<div class="col-lg-2">
<h4> {{ item.mealName }}{{ item.price }} {{ item.currency }}</h4>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-lg-6">
<span class="text-primary">{{ item.roomName }}</span>
{% if(item.mealName) %}
<span class="text-muted">({{ item.mealName }})</span>
{% endif %}
</div>
<div class="col-lg-2">{{ item.price }} {{ item.currency }}</div>
</div>
{% endfor %}
Basically in their shopping cart I would like to display the image they upload to print but i can't seem to figure it out..... I know i need to add in something along the lines of <img src="http://path/to/thumbnails/myimage.jpg"> but i don't know what to add in place of "http://path/to/thumbnails/myimage.jpg" within this code to display the image they upload if there even is anything? All help appreciated! Thanks (sorry if this is a silly question and plain obvious)
{% comment %}
This is your /cart template. If you are using the Ajaxify Cart plugin,
your form (with action="/cart") layout will be used in the drawer/modal.
For info on test orders:
- General http://docs.shopify.com/manual/your-store/orders/test-orders
- Shopify Payments - http://docs.shopify.com/manual/more/shopify-payments/testing-shopify-payments
<!-- Bold: Options 4-1 -->
<script>function update_qty_builder(builder_id, qty){ jQuery('.'+builder_id+"_qty").val(qty.value); } function remove_product_builder(builder_id){ jQuery('.'+builder_id+"_qty").val(0); jQuery('.'+builder_id+"_qty").parents("form").submit(); }</script>
{% include 'bold-cart-handler' %}
<!-- // end Options 4-1 -->
{% endcomment %}
{% if cart.item_count > 0 %}
<form action="/cart" method="post" novalidate class="cart">
<div class="section-header">
<h1 class="section-header__title">{{ 'cart.general.title' | t }}</h1>
</div>
<div class="cart__row medium-down--hide cart__header-labels">
<div class="grid--full">
<div class="grid__item large--one-half push--large--one-half">
<div class="grid--full">
<div class="grid__item one-third medium-down--one-third">
<span class="h4">{{ 'cart.label.price' | t }}</span>
</div>
<div class="grid__item one-third medium-down--one-third text-center">
<span class="h4">{{ 'cart.label.quantity' | t }}</span>
</div>
<div class="grid__item one-third medium-down--one-third text-right">
<span class="h4">{{ 'cart.label.total' | t }}</span>
</div>
</div>
</div>
</div>
</div>
{% comment %}
Loop through products in the cart
{% endcomment %}
{% for item in cart.items %}
<!-- Bold: Options 4-2 -->
{% include 'boldoptions' with 'step2' %}
<!-- // end Options 4-2 -->
<tr style="{% include 'boldoptions' with 'step4' %}" class="{% include 'boldoptions' with 'step3' %}">
<div class="cart__row" data-id="{{ item.id }}">
<div class="grid--full cart__row--table-large">
<div class="grid__item large--one-half">
<div class="grid">
<div class="grid__item one-third">
<a href="{{ item.url | within: collections.all }}" class="cart__image">
{% comment %}
More image size options at:
- http://docs.shopify.com/themes/filters/product-img-url
{% endcomment %}
<!-- Bold: Options 4-5 -->
{% if builder[0] %}
<img src="{{ builder[1] }}" alt="{{ builder[0] }}" />
{% else %}
<img src="{{ item | img_url: 'medium' }}" alt="{{ item.title | escape }}">
</a>
<!-- Bold: Options 4-6 -->
{% include 'boldoptions' with 'step6' %}
<!-- // end Options 4-6 -->
{% endif %}
<!-- // end Options 4-5 -->
</div>
<div class="grid__item two-thirds">
<a href="{{ item.url }}" class="h4">
{{ item.product.title }}
</a>
{% unless item.variant.title contains 'Default' %}
<br>
<small>{{ item.variant.title }}</small>
{% endunless %}
{% if settings.cart_vendor_enable %}
<p>{{ item.vendor }}</p>
{% endif %}
{% comment %}
Optional, loop through custom product line items if available
For more info on line item properties, visit:
- http://docs.shopify.com/support/your-store/products/how-do-I-collect-additional-information-on-the-product-page-Like-for-a-monogram-engraving-or-customization
{% endcomment %}
{% include 'product_customizer_cart' %}
{% if item.properties.size > 0 %}
{% for p in item.properties %}
{% unless p.last == blank %}
{{ p.first }}:
{% comment %}
Check if there was an uploaded file associated
{% endcomment %}
{% if p.last contains '/uploads/' %}
{{ p.last | split: '/' | last }}
{% else %}
{{ p.last }}
{% endif %}
<br>
{% endunless %}
{% endfor %}
{% endif %}
<a href="{% include 'boldoptions' with 'step9' %}" data-id="{{ item.id }}" class="{% include 'boldoptions' with 'step10' %} cart__remove" {% include 'boldoptions' with 'step11' %}>
<small>{{ 'cart.general.remove' | t }}</small>
</a>
</div>
</div>
</div>
<div class="grid__item large--one-half">
<div class="grid--full cart__row--table-large">
<div class="grid__item one-third">
<span class="cart__mini-labels">{{ 'cart.label.price' | t }}</span>
<span class="h5">{% include 'boldoptions' with 'step12' %}</span>
</div>
<div class="grid__item one-third text-center">
<span class="cart__mini-labels">{{ 'cart.label.quantity' | t }}</span>
{% comment %}
Added data-id for the ajax cart implementation only.
{% endcomment %}
<input type="number" name="updates[]" id="updates_{{ item.id }}" class="{% include 'boldoptions' with 'step7' %}" value="{{ item.quantity }}" min="0" data-id="{{ item.id }}" {% include 'boldoptions' with 'step8' %}>
</div>
<div class="grid__item one-third text-right">
<span class="cart__mini-labels">{{ 'cart.label.total' | t }}</span>
<span class="h5">{% include 'boldoptions' with 'step13' %}</span>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
<div class="cart__row">
<div class="grid">
{% comment %}
Optional, add a textarea for special notes
- Your theme settings can turn this on or off. Default is on.
- Make sure you have name="note" for the message to be submitted properly
{% endcomment %}
{% if settings.cart_notes_enable %}
{% assign noteSize = cart.note | size %}
<div class="grid__item large--five-twelfths">
<button type="button" class="text-link cart__note-add{% if noteSize > 0 %} is-hidden{% endif %}">
{{ 'cart.label.add_note' | t }}
</button>
<div class="cart__note{% if noteSize > 0 %} is-active{% endif %}">
<label for="CartSpecialInstructions">{{ 'cart.general.note' | t }}</label>
<textarea name="note" class="input-full" id="CartSpecialInstructions">{{ cart.note }}</textarea>
</div>
</div>
{% endif %}
<div class="grid__item text-right{% if settings.cart_notes_enable %} large--seven-twelfths{% endif %}">
<p>
<span class="cart__subtotal-title">{{ 'cart.general.subtotal' | t }}</span>
<span class="h5 cart__subtotal">{{ cart.total_price | money }}</span>
</p>
<p><em>{{ 'cart.general.shipping_at_checkout' | t }}</em></p>
<input type="submit" name="update" class="btn--secondary update-cart" value="{{ 'cart.general.update' | t }}">
<input type="submit" name="checkout" class="btn" value="{{ 'cart.general.checkout' | t }}">
{% if additional_checkout_buttons %}
<div class="cart__additional_checkout">{{ content_for_additional_checkout_buttons }}</div>
{% endif %}
</div>
</div>
</div>
</form>
{% else %}
{% comment %}
The cart is empty
{% endcomment %}
<h2>{{ 'cart.general.title' | t }}</h2>
<p>{{ 'cart.general.empty' | t }}</p>
<p>{{ 'cart.general.continue_browsing_html' | t }}</p>
{% endif %}
<!-- Bold: Options 4-14 -->
{% include 'bold-cart-modal' %}
<!-- // end Options 4-14 -->
You have to include the full publicly reachable image path in the src attribute of the image tag
<img src="http://path/to/thumbnails/myimage.jpg">
I have the a form twig template, where I want to parse a specific fields help text with the raw filter (it contains html). The field is called postcode in a form called Clinic
According to here http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field
Form template:
{% extends 'AgriHealthAhpBundle::admin.html.twig' %}
{% form_theme form 'AgriHealthAhpBundle:Form:fields.html.twig' %}
{% block _clinic_postcode_row %}
<div class="row">
test<div class="small-12 medium-3 columns label">{{ form_label(form) }}</div>
<div class="small-12 medium-6 columns widget">
{{ form_widget(form) }}
<div class="error">
{{ form_errors(form) }}
</div>
</div>
<div class="small-12 medium-3 columns help">
{% if help is defined %}
{{ help|raw }}
{% endif %}
</div>
</div>
{% endblock %}
{% block admin -%}
<h1>New Clinic</h1>
{{ form(form) }}
<div class="row form_actions">
<div class="small-12 medium-offset-3 medium-2 columns submit">
<button type="submit" id="agrihealth_ahpbundle_clinic_submit_visible" name="agrihealth_ahpbundle_clinic[submit]">Create</button>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#agrihealth_ahpbundle_clinic_submit_visible').click(function(){
jQuery('form[name="agrihealth_ahpbundle_clinic"]').submit();
});
});
</script>
<div class="small-12 medium-2 columns cancel">
<a href="{{ path('clinic') }}">
Cancel
</a>
</div>
<div class="small-12 medium-2 end columns cancel">
<a href="{{ path('clinic') }}">
Back to List
</a>
</div>
</div>
{% endblock %}
AhpBundle/Resources/views/Form/fields.html.twig
{% block form_row %}
{% spaceless %}
<div class="row">
<div class="small-12 medium-3 columns label">{{ form_label(form) }}</div>
<div class="small-12 medium-6 columns widget">
{{ form_widget(form) }}
<div class="error">
{{ form_errors(form) }}
</div>
</div>
<div class="small-12 medium-3 columns help">
{% if help is defined %}
{{ help }}
{% endif %}
</div>
</div>
{% endspaceless %}
{% endblock form_row %}
Anyone can see what I have overlooked, I tried
{% block _clinic_postcode_row %}
and
{% block _Clinic_postcode_row %}
Solution
As per accepted answer, the form row block needs to fully idetified with shorthand bundle name. The easiest way is to view the source code of the form and identify the text used in each input field and the form name="":
Replace
{% form_theme form 'AgriHealthAhpBundle:Form:fields.html.twig' %}
with
{% form_theme form with ['AgriHealthAhpBundle:Form:fields.html.twig', _self] %}
Since you are decorating the row inside action template, while applying separate form template you need to specify multiple templates
You also need to specify a fully qualified path name to your row block such as
{% block _agrihealth_ahpbundle_clinic_postcode_row %}
I need straightforward solution for dynamically set number of records per page with Knp Pagination Bundle.
I read the this page records per page allow user to choose - codeigniter pagination about dynamically set per page limits and I know I need a drop down with hyperlink inside each item that send a request to server and server use parameter on this request to set limit per page on knp pagination bundle. But I don't know exactly how to handle this actions on server and also and more harder to me how to create items on drop down related to the total number on my query.
my controller:
public function indexAction()
{
$page_title = Util::getFormattedPageTitle("Customers");
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM CustomersBundle:Customer a WHERE a.enable = 1 ORDER BY a.id";
//$query = $em->createQuery($dql);
//$customers = $query->execute();
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
3/*limit per page*/
);
// parameters to template
return $this->render('CustomersBundle:Default:index.html.twig', array(
'page_title' => $page_title,
'pagination' => $pagination,
'image_path' => CustomersConstants::$customers_image_thumb_path
));
}
And my view code is:
{% extends '::base.html.twig' %}
{% block title %}
{{ page_title }}
{% endblock %}
{% block body %}
<h1>
{{ "customers" }}
</h1>
<br />
{% for customer in pagination %}
<a href="{{ customer.url }}" target="_blank">
<div dir="rtl" class="st_simple_box" id="customer{{customer.id}}" >
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="vertical-align: top; width: 115px;">
{% if customer.imageName is not empty %}
<img class="newsimage" src="{{asset(image_path) ~ customer.imageName}}" alt="No Image/>
{% else %}
<img class="newsimage" src="{{asset('images/vakil_default_small.png') ~ customer.imageName}}" alt="No Image"/>
{% endif %}
</td>
<td style="text-align: center;">
<p><span style="font-family: Tahoma;">{{customer.titleFa}}</span></p>
</td>
<td style="text-align: justify;">
<p><span style="font-family: Tahoma;">{{customer.descriptionFa}}</span></p>
</td>
</tr>
</table>
</div>
</a>
{% endfor %}
<div class="navigation">
<span>
{{ knp_pagination_render(pagination) }}
</span>
<!--Here is my drop down html code-->
</div>
<br />
{% endblock %}
**
Edited on 12 March 2014
||||
||||
||||
\\\///
\\//
\/
Is there any way to set MaxItemPerPage on cookie to have a integrated variable and use this variable or change it while Knp Pagination is showed up on twig file.
the answer was correct but because of I used pagination in many bundles to paginate between my entities so I need an integrate variable to change all of them. I use your answer and customize sliding.html.twig on Knp Pagination to reach this code for "customized_sliding.html.twig"
<div class="ui small pagination menu">
<div style="clear: both;">
{% if pageCount > 1 %}
<div class="pagination" style="float: right;">
{% if first is defined and current != first %}
<a href="{{ path(route, query|merge({(pageParameterName): first})) }}" class="small icon item">
<i class="small double angle right icon"></i>
</a>
{% endif %}
{% if previous is defined %}
<a href="{{ path(route, query|merge({(pageParameterName): previous})) }}" class="small icon item">
<i class="small angle right icon"></i>
</a>
{% endif %}
{% for page in pagesInRange %}
{% if page != current %}
<a href="{{ path(route, query|merge({(pageParameterName): page})) }}" class="small item">
{{ page }}
</a>
{% else %}
<a class="small active item">
{{ page }}
</a>
{% endif %}
{% endfor %}
{% if next is defined %}
<a href="{{ path(route, query|merge({(pageParameterName): next})) }}" class="small icon item">
<i class="small angle left icon"></i>
</a>
{% endif %}
{% if last is defined and current != last %}
<a href="{{ path(route, query|merge({(pageParameterName): last})) }}" class="small icon item">
<i class="small double angle left icon"></i>
</a>
{% endif %}
</div>
{% endif %}
<div style="float: left;">
<select name="maxItemPerPage" id="maxItemPerPage">
<option selected="true" style="display:none;">Number Per Page</option>
<option id="10">5</option>
<option id="20">10</option>
<option id="30">20</option>
</select>
</div>
</div>
<script type="text/javascript">
//on select change, you navigate to indexAction and send the parameter maxItemPerPage
$('#maxItemPerPage').change(function(){
{% set currentPath = path(app.request.attributes.get('_route')) %}
var url = "{{path(app.request.attributes.get('_route'),{'maxItemPerPage': '_itemNum'})}}";
var item = $('#maxItemPerPage').find(":selected").text();
jQuery(location).attr('href', url.replace('_itemNum',item ));
});
</script>
</div>
I want to fetch and store MaxItemPerPage from cookie so there is no need to change all bundles code.
For example in my controller I don't know have to fetch the $maxItemPerPage from cookie
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
$maxItemPerPage/*limit per page*/
);
And I need change the value of maxItemPerPage on cookie by changing the value of html tag by javascript and redirect the page to same controller and no need to send maxItemPerPage to controller.
This can be done easily (if i understood well)
public function indexAction($maxItemPerPage=20)
{
$page_title = Util::getFormattedPageTitle("Customers");
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM CustomersBundle:Customer a WHERE a.enable = 1 ORDER BY a.id";
//$query = $em->createQuery($dql);
//$customers = $query->execute();
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
$maxItemPerPage /*limit per page*/
);
// parameters to template
return $this->render('CustomersBundle:Default:index.html.twig', array(
'page_title' => $page_title,
'pagination' => $pagination,
'image_path' => CustomersConstants::$customers_image_thumb_path
));
}
in the view
{% extends '::base.html.twig' %}
{% block title %}
{{ page_title }}
{% endblock %}
{% block javascript%}
<script type="text/javascript">
//on select change, you navigate to indexAction and send the parameter maxItemPerPage
$('#maxItemPerPage').change(function(){
var url = '{{path('controller_index_route','maxItemPerPage':_itemNum)}}';
var item = $('#maxItemPerPage').find(":selected").text();
window.location.href = url.replace('_itemNum',item );
})
</script>
{% endblock %}
{% block body %}
<h1>
{{ "customers" }}
</h1>
<br />
{% for customer in pagination %}
<a href="{{ customer.url }}" target="_blank">
<div dir="rtl" class="st_simple_box" id="customer{{customer.id}}" >
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="vertical-align: top; width: 115px;">
{% if customer.imageName is not empty %}
<img class="newsimage" src="{{asset(image_path) ~ customer.imageName}}" alt="No Image/>
{% else %}
<img class="newsimage" src="{{asset('images/vakil_default_small.png') ~ customer.imageName}}" alt="No Image"/>
{% endif %}
</td>
<td style="text-align: center;">
<p><span style="font-family: Tahoma;">{{customer.titleFa}}</span></p>
</td>
<td style="text-align: justify;">
<p><span style="font-family: Tahoma;">{{customer.descriptionFa}}</span></p>
</td>
</tr>
</table>
</div>
</a>
{% endfor %}
<div class="navigation">
<span>
{{ knp_pagination_render(pagination) }}
</span>
<!--Here is my drop down html code-->
</div>
<br />
<select name="maxItemPerPage" id="maxItemPerPage">
<option id="10">10</option>
<option id="20">20</option>
<option id="30">30</option>
</select>
{% endblock %}