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 %}
Related
I display the content of the category and I display the button "displays more" if I have other content but the button has been marked according to the number of ads in the category, and when I paste the button outside "for" I get always this button even when the category content is empty
<div class="pedagogical pedagogical--category js-pedagogical-items" data-type="category" data-nbdisplayed="4">
{% for categoryId in questionCategories %}
<div class="row pedagogical__items" data-type="category" data-value="{{ categoryId }}">
{% for tool in tools if tool.questionCategory == categoryId %}
<div class="col-12 col-md-6 pedagogical__item__wrapper">
{% include 'components/tool-preview-item.html.twig' with {'tool' : tool} %}
</div>
<div class="col-12 text-center">
<button class="btn btn-outline-secondary js-show-more" data-type="category" data-value="{{ categoryId }}">{{ 'show-more'|trans({}, 'action') }}</button>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
I think you just need to add a check before rendering your button:
<div class="pedagogical pedagogical--category js-pedagogical-items" data-type="category" data-nbdisplayed="4">
{% for categoryId in questionCategories %}
<div class="row pedagogical__items" data-type="category" data-value="{{ categoryId }}">
{% set has_items = 'false' %}
{% for tool in tools if tool.questionCategory == categoryId %}
{% set has_items = 'true' %}
<div class="col-12 col-md-6 pedagogical__item__wrapper">
{% include 'components/tool-preview-item.html.twig' with {'tool' : tool} %}
</div>
{% endfor %}
{% if has_items == 'true' %}
<div class="col-12 text-center">
<button class="btn btn-outline-secondary js-show-more" data-type="category" data-value="{{ categoryId }}">{{ 'show-more'|trans({}, 'action') }}</button>
</div>
{% endif %}
</div>
{% endfor %}
</div>
How can I group output result by "item.hotel.name" ?
{% for item in pagination.items %} <!-- Вывод результатов по отелям -->
<div class="row">
{{item.hotel.name->item2 }}
<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>
</div>
{% endfor %}
Now:
Result:
I have simple edit form where the user will have to fill up required fields. The problem is the validation of required fields doesn't show, and also the fields or data don't change.
UPDATE I var_dump the $form->isSubmitted() and it shows bool(false)
Here's my controller:
public function editAction(Request $request, $id) {
$company = $this->getDoctrine()
->getRepository('SwipeBundle:Company')
->find($id);
if(!$company) {
throw $this->createNotFoundException(
'No Company found for id '.$id
);
}
$form = $this->createForm(CompanyType::class, $company, array(
'action'=>$this->generateUrl('swipe_backend_company_edit', array('id'=>$company->getId())),
'method'=>'PUT'
));
if ($request->getMethod() == "POST") {
if ($form->isSubmitted()) {
// $em->persist($company);
// $em->flush();
echo "Update";
}
}
return $this->render('Backend/Company/edit.html.twig', array(
'form'=>$form->createView(),
'company'=>$company
));
}
And here's my twig template:
{% extends '::Backend/base.html.twig' %}
{% block body %}
<!-- Section -->
<section class="sections">
<!-- Side Bar -->
{% include '::Backend/side_menu_bar.html.twig' %}
<!-- Wrapper -->
<div id="administrator" class="wrapper">
<div class="mt40 pt30"> <!-- Container -->
<h1 class="mb10 bold">Edit Company</h1>
<p class="mb30">Fill up all the required fields for Company.</p>
{% if not form.vars.valid %}
<p class="alert note-error">
There are errors in your form. Please check the fields marked in red.
</p>
{% endif %}
<div class="alert note-error">
<p>Fields with asterisk (*) are required</p>
</div>
{% set url = path('swipe_backend_company_edit', { 'id': company.id }) %}
<form novalidate method="post" action="{{ url }}" class="p20 card mb30">
<div class="sections pb30 pt10">
<fieldset class="col span6">
{% set attr = {} %}
{% if form_errors(form.name) is not empty %}
{% set attr = attr|merge({ 'class': 'alert error'}) %}
{% endif %}
<label for="" class="input-required">
<strong>Company Name<span class="highlight-red">*</span>
</strong>
</label>
{{ form_widget(form.name, { 'attr': attr } ) }}
{% if not form.name.vars.valid %}
<p class="mt10" style="color: #DC2B1B;">
{{ form.name.vars.errors[0].message }}
</p>
{% endif %}
</fieldset>
<fieldset class="col span6">
<strong>
{{ form_label(form.website) }}
</strong>
{{ form_widget(form.website) }}
</fieldset>
</div>
<div class="sections pb30 pt10">
{% set attr = {} %}
{% if form_errors(form.email_address) is not empty %}
{% set attr = attr|merge({ 'class': 'alert error'}) %}
{% endif %}
<label for="" class="input-required">
<strong>Company Email Address<span class="highlight-red">*</span>
</strong>
</label>
{{ form_widget(form.email_address, { 'attr': attr } ) }}
{% if not form.email_address.vars.valid %}
<p class="mt10" style="color: #DC2B1B;">
{{ form.email_address.vars.errors[0].message }}
</p>
{% endif %}
</div>
<div class="sections pb30 pt10">
<fieldset class="col span6">
<strong>
{{ form_label(form.telephone_no) }}
</strong>
{{ form_widget(form.telephone_no) }}
</fieldset>
<fieldset class="col span6">
<strong>
{{ form_label(form.mobile_no) }}
</strong>
{{ form_widget(form.mobile_no) }}
</fieldset>
</div>
<div class="sections pb30 pt10">
{% set attr = {} %}
{% if form_errors(form.address) is not empty %}
{% set attr = attr|merge({ 'class': 'alert error'}) %}
{% endif %}
<label for="" class="input-required">
<strong>Company Address<span class="highlight-red">*</span>
</strong>
</label>
{{ form_widget(form.address, { 'attr': attr } ) }}
{% if not form.address.vars.valid %}
<p class="mt10" style="color: #DC2B1B;">
{{ form.address.vars.errors[0].message }}
</p>
{% endif %}
</div>
<div class="text-right mt20 mb10">
<button class="btn btn-positive mt10 mr5">Update Company</button>
<button class="btn btn-positive mt10 mr5">Cancel</button>
</div>
{{ form_rest(form) }}
</form>
</div> <!-- Container End -->
</div> <!-- Wrapper End -->
</section> <!-- Section End -->
{% endblock %}
Only one action for edit / Update the record
public function editAction($id) {
$em = $this->getDoctrine()->getManager();
$company = $em
->getRepository('SwipeBundle:Company')
->find($id);
if(!$company) {
throw $this->createNotFoundException(
'No Company found for id '.$id
);
}
$form = $this->createForm(CompanyType::class, $company, array(
'action'=>$this->generateUrl('swipe_backend_company_update', array('id'=>$company->getId())),
'method'=>'PUT'
));
if ($request->getMethod() == "POST") {
if ($form->isValid()) {
$em->persist($company);
$em->flush();
}
}
return $this->render('Backend/Company/edit.html.twig', array(
'form'=>$form->createView(),
'company'=>$company
));
}
and also Change the form action in twig same a edit action
Make sure this is useful to you.
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 a controller that does this
/**
* #Route("/AjaxAddQuestionForm/{section}")
* #ParamConverter("section", class="AppBundle:Section")
*/
public function ajaxAddQuestionFormAction(Request $request, $section)
{
$question = new Question();
$addQuestionForm = $this->createForm(new AddQuestionType(), $question);
return $this->render('AppBundle:Form:ajaxAddQuestionForm.html.twig', array(
'section' => $section,
'addAjaxQuestionForm' => $addQuestionForm->createView(),
));
}
The ajaxAddQuestionForm.html.twig file looks like this
{% embed 'modal.html.twig' %}
{% block labelledby %}addnewquestion{% endblock %}
{% block modalId %}addnewquestion{% endblock %}
{% block modalHead %}
{{ 'client.modal.head'| trans }}
{% endblock %}
{% block modalBody %}
{{form_start(addAjaxQuestionForm)}}
<div>
{{form_widget(addAjaxQuestionForm.section, {value: section.id})}}
</div>
<div class="form-group">
{{form_label(addAjaxQuestionForm.name, null, {'label_attr': {'class': 'form-label'}}) }}
<span class="help"></span>
<div class="controls">
{{form_widget(addAjaxQuestionForm.name, {'attr':{'class':'form-control'}})}}
</div>
</div>
<div class="form-group">
{{form_label(addAjaxQuestionForm.category, null, {'label_attr': {'class': 'form-label'}}) }}
<span class="help"></span>
<div class="controls">
{{form_widget(addAjaxQuestionForm.category, {'attr':{'width':'100%'}})}}
</div>
</div>
{% endblock %}
{% block modalFooter %}
<button class="btn btn-default" data-dismiss="modal" type="button">{{'client.form.cancel'| trans}}</button>
<input class="btn btn-primary create" type="submit" value="{{'client.form.add.client'| trans}}">
{{form_end(addAjaxQuestionForm)}}
{% endblock %}
{% endembed %}
And finally I have an edit.html.twig that looks like this
<div aria-hidden="true" aria-labelledby="{% block labelledby %}{% endblock %}" class="modal fade" id="{% block modalId %}{% endblock %}" role="dialog" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>
<h4 class="modal-title" id="myModalLabel">{% block modalHead %}{% endblock %}</h4>
</div>
<div class="modal-body">
{% block modalBody %}
{% endblock %}
</div>
<div class="modal-footer">
{% block modalFooter %}
{% endblock %}
</div>
</div>
</div>
</div>
{% include 'AppBundle:Form:ajaxAddQuestionForm.html.twig' %}
Now I get the following error:
Variable "addAjaxQuestionForm" does not exist in src/AppBundle/Resources/views/Form/ajaxAddQuestionForm.html.twig at line 9
Any idea what is wrong with my code?
In your edit.html.twig (assuming your addAjaxQuestionForm variable exists in it), you need to pass it on to the included twig.
{% include 'AppBundle:Form:ajaxAddQuestionForm.html.twig' with {'addAjaxQuestionForm': addAjaxQuestionForm} %}
Try to use
{{ render(controller('AppBundle:Section:ajaxAddQuestionFormAction',{'section':section})) }}
Instead of Include and you must add "section" parameter to ajaxAddQuestionFormAction.
We can help you more than that if you don't publish us all the code that concern the part of your problem.
I hope that helps
You should use render controller method instead of include
{{ render(controller(
'AppBundle:Form:ajaxAddQuestionForm',
{ 'section': section }
)) }}
read more about it here: http://symfony.com/doc/current/book/templating.html#embedding-controllers