Is anyone here familiar with Twig? The Wordpress theme Realia is supposed to display a custom text field when the price field is left empty. But it doesn't. Here is the code that I think is responsible.
<div class="property span9">
<div class="row">
<div class="image span3">
<div class="content">
{% if wp.get_the_post_thumbnail(property.ID) %}
<img src="{{ wp.aq_resize(wp.wp_get_attachment_url(wp.get_post_thumbnail_id(property.ID), 'full'), 270, 200, 'true') }}" alt="{{ property.post_title }}">
{% else %}
<img src="{{ wp.get_template_directory_uri() }}/assets/img/property-tmp-small.png" alt="{{ property.post_title }}">
{% endif %}
</div>
<!-- /.content -->
</div>
<!-- /.image -->
<div class="body span6">
<div class="title-price row">
<div class="title span4">
<h2>{{ property.post_title }}</h2>
</div>
<!-- /.title -->
<div class="price">
{% if wp.get_post_meta(post.ID, '_property_custom_text').0 %}
{{ wp.get_post_meta(post.ID, '_property_custom_text').0 }}
{% else %}
{{ wp.aviators_price_format(property.meta._property_price.0) }}{% if property.meta._property_price_suffix.0 %}
<span class="suffix">{{ property.meta._property_price_suffix.0 }}</span>{% endif %}</div>
{% endif %}
<!-- /.price -->
</div>
<!-- /.title -->
<div class="location">{{ property.location.0.name }}</div>
<!-- /.location -->
<div class="body">
{{ wp.aviators_core_get_post_teaser(property.ID) }}
</div>
<!-- /.body -->
<div class="info clearfix">
{% if property.meta._property_area.0 %}
<div class="area">
<span class="key">{{ wp.__('Area', 'aviators') }}:</span><!-- /.key -->
<span class="value">{{ property.meta._property_area.0 }}{{ wp.aviators_settings_get_value('properties', 'units', 'area')|raw }}</span><!-- /.value -->
</div><!-- /.area -->
{% endif %}
{% if property.meta._property_bedrooms.0 %}
<div class="bedrooms" title="{{ wp.__('Bedrooms', 'aviators') }}">
<div class="content">{{ property.meta._property_bedrooms.0 }}</div>
</div><!-- /.bedrooms -->
{% endif %}
{% if property.meta._property_bathrooms.0 %}
<div class="bathrooms" title="{{ wp.__('Bathrooms', 'aviators') }}">
<div class="content">{{ property.meta._property_bathrooms.0 }}</div>
</div><!-- /.bathrooms -->
{% endif %}
</div>
<!-- /.info -->
</div>
<!-- /.body -->
</div>
<!-- /.row -->
</div><!-- /.property -->
Try:
{% if property.meta._property_price_suffix == false %}
The table at the end of the documentation at http://twig.sensiolabs.org/doc/tags/if.html is useful.
Related
description = "Display Blog list"
[viewBag]
snippetCode = "post-list.htm"
snippetName = "Display Blog list"
snippetProperties[heading][title] = "Heading"
snippetProperties[heading][type] = "string"
snippetProperties[heading][default] = "News & Events"
snippetProperties[heading][options][] = ""
[blogPosts]
pageNumber = "{{ :page }}"
postsPerPage = 6
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = "article-post"
postPage = "article-post"
==
<?php
function onLoadMore()
{
$this['pageNumber'] = 10;
}
?>
==
<section class="section section-fat news">
<div class="section-header">
<div class="container text-center">
<h2 class="heading-large">{{ heading|raw }}</h2>
</div>
</div>
<div class="container-fluid">
<div id="loadmore" class="row">
{% set posts = blogPosts.posts %}
{# fetch all posts in Blog #}
{% for post in posts %}
<div class="item col-sm-6 col-md-4 col-lg-6">
<a href="{{ post.url }}" class="news-box">
<div class="news-box-container">
<div class="news-box-content">
<h4>{{ post.title }}</h4>
<p>{% if post.categories.count %} in {% endif %} {% for category in post.categories %}{{ category.name }}{% if not loop.last %}, {% endif %} {% endfor %} {{ post.published_at|date('M d, Y') }}</p>
</div>
</div>
<div class="news-box-arrow"><i class="icon-angle-circle-right"></i></div>
<div class="news-box-image" style="background-image: url('{{ post.featured_images[0].thumb(2500,1000) }}');"></div>
</a>
</div>
{% else %}
<div class="text-center">{{ noPostsMessage }}</div>
{% endfor %}
<div class="text-center news-load">
<a data-request="onLoadMore" data-request-update="'load-more': '#loadmore'" data-attach-loading class="btn btn-lg btn-invert">Load More</a>
</div>
</div>
</section>
This is the code of the snippet I created for showing the blogs.
I'm using blog plugin of Rainlab, is there a way that I can customize or modify the code that will help me update the components? I want to add 3 posts to "postsPerPage = 6" every time I clicked the button. I've been trying to find a solution or plugin for this problem but sadly I can't find one.
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 %}
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:
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">
Is there are way I can create an easily maintainable for loop in Twig that works with Bootstrap's layout?
I'm using bootstrap's row functionality, meaning that each row has a div of class "row". This makes things tricky in twig, because I have no way of entering these header divs.
For example, my main page will have several articles, and extends the skeleton template, as shown below. Is there any way I can use this skeleton base and use it multiple times in a for loop?
I've tried by adding the Article block, but it overrides everything inside. Do I have to create another template for this to work?
<div id="main" class="container-fluid">
{% block article %}
<div class="row">
<div class="col-md-3"></div>
<div id="title" class="col-md-6">
{% block title %}
{% endblock title %}
</div>
<div class="col-md-3"></div>
</div>
<div class="row">
<div class="col-md-1"></div>
<div id="left-nav" class="col-md-2">
{% block left_nav %}
{% endblock left_nav %}
</div>
<div class="col-md-6" id="content">
{% block content %}
{% endblock content %}
</div>
<div id="right-nav" class="col-md-2">
{% block right_nav %}
{% endblock right_nav %}
</div>
<div class="col-md-1"></div>
</div>
{% endblock article %}
</div>
How do I iterate and create multiple "Articles" in this context?
Using parent() isn't helpful because it'll just print the start and end tags, wheras I need them to be placed around the correct sections, otherwise the rows won't work.
In other words, in a for loop context, how can I change the values of variables inside child blocks within a parent block?
Apologies if this is unclear, I found it hard to put into words.
Edit:
I know the existence of for loops in twig, I am just unsure of how to implement a solution to this particular problem.
As a simple example of the skeleton template and its output:
Template
<div class="row">
<div class="title">
{% block title %}{% endblock %}
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
</div>
Output
<div class="row">
<div class="title">
This is the first post.
</div>
<div id="content">
Post 1
</div>
</div>
<div class="row">
<div class="title">
This is the second post.
</div>
<div id="content">
Post 2
</div>
</div>
I found a solution that is fairly maintainable, as it still extends the skeleton template, but is flexible enough to support for loops inside a block.
Skeleton
<div id="main" class="container-fluid">
{% block article %}
<div class="row">
<div class="col-md-3"></div>
<div id="whitespace" class="col-md-6">
{% block title %}
{% endblock title %}
</div>
<div class="col-md-3"></div>
</div>
<div class="row">
<div class="col-md-1"></div>
<div id="left-nav" class="col-md-2">
{% block left_nav %}
{% endblock left_nav %}
</div>
<div class="col-md-6" id="content">
{% block content %}
{% endblock content %}
</div>
<div id="right-nav" class="col-md-2">
{% block right_nav %}
{% endblock right_nav %}
</div>
<div class="col-md-1"></div>
</div>
{% endblock article %}
</div>
Article list
{% extends 'skeleton.php' %}
{% block article %}
{% for post in posts %}
<div class="row">
{# Start title #}
<div class="col-md-3"></div>
<div id="title" class="col-md-6">
{{post.title}}
</div>
<div class="col-md-3"></div>
{# End title #}
</div>
<div class="row">
<div class="col-md-1"></div>
{# Start left-nav #}
<div id="left-nav" class="col-md-2">
{{post.left}}
</div>
{# end left-nav #}
{# Start content #}
<div class="col-md-6" id="content">
{{post.getBody}}
</div>
{# end content #}
{# start right-nav #}
<div id="right-nav" class="col-md-2">
{{post.right}}
</div>
{# end right-nav #}
<div class="col-md-1"></div>
</div>
{% endfor %}
{% endblock article %}
{% foreach article in articles %}
<div class="row">
<div class="title">
{{ article.title }}
</div>
<div id="content">
{{ article.content }}
</div>
</div>
{% endfor %}
Is the template assuming articles is an array containing arrays with keys title and content