Sorting data from table field octobercms - sorting

I want to show data in order of column where the num are given in octobercms,
default.htm
{% set post = __SELF__.post %}
<div class="content">{{ post.content_html|raw }}</div>
{% if post.featured_images.count %}
<div class="featured-images text-center">
{% for image in post.featured_images %}
<p>
<img
data-src="{{ image.filename }}"
src="{{ image.path }}"
alt="{{ image.description }}"
style="max-width: 100%" />
</p>
{% endfor %}
</div>
{% endif %}
<p class="info">
Posted
{% if post.categories.count %} in
{% for category in post.categories %}
{{ category.name }}{% if not loop.last %}, {% endif %}
{% endfor %}
{% endif %}
on {{ post.published_at|date('M d, Y') }}
</p>
<p class="info">
{% for blocks in post.blocks %} // here the output of this loop must show in order which are given in column in same table which is relation_sort_order
<b> {{ blocks.title}}</b>
{{ blocks.content_html|raw }}
{% endfor %}
</p>
how i can show the blocks in number in relation_sort_order field

Assuming that the post variable is a custom Model object, and blocks is a relation on that object, the best way to do what you are asking to is to update the relationship definition in the model class that defines your post object.
You didn't include the code for that, but it probably looks something like this:
public $hasMany = [
'blocks' => 'Acme\Blog\Models\Block'
]
You can update that to include a sort order by default, see this section of the OctoberCMS documentation
But in short, you would change the above to this:
public $hasMany = [
'blocks' => [
'Acme\Blog\Models\Block',
'order' => 'relation_sort_order asc'
]
];
Then anytime in you accessed the blocks relationship from your Post class, it would automatically sort it in that order.

Related

Loop through liquid advanced custom field to return multiple metafield values

I've built the following liquid for loop to retrieve & output data from a repeating advanced custom field in Shopify. The ACF namespace is faq, and contains heading and content data. My current loop is as follows:
<div class="feed-faqs">
{% if page.metafields.faq != blank %}
{% assign faqs = page.metafields.faq %}
{% for item in faqs %}
{% assign i = forloop.index %}
<div class="item item--{{ i }}">
{{ heading[i] }}
{{ content[i] }}
</div>
{% endfor %}
{% endif %}
</div>
However, on the frontend, this loop returns the following:
<div class="feed-faqs">
<div class="item item--1">
</div>
<div class="item item--2">
</div>
</div>
Is what I'm trying to achieve (to output multiple values from a repeating ACF field) possible with this approach, and if so, where have I gone wrong in fetching the header & content data?
Worked it out, so leaving this answer for anyone else in the future:
<div class="feed--faqs">
{% if page.metafields.faq != blank %}
{% assign faqs = page.metafields.faq.heading %}
{% for value in faqs %}
{% assign i = forloop.index0 %}
<div class="item item--{{ i }}">
<h4>{{ page.metafields.faq.heading[i] }}</h4>
<p>{{ page.metafields.faq.content[i] }}</p>
</div>
{% endfor %}
{% endif %}
</div>
Metafield value type is set to 'Json String'.
For reference, I'm using the ArenaCommerce Advanceds Custom Fields app: https://apps.shopify.com/advanced-custom-field.

Checking whether product also exists in a different collection in a Shopify Theme

I have a collection.list.liquid file.
This loops through ALL the products in the store.
{% for product in collection.products %}
I have created another collection of products that are in a collection cleverly titled 'Special Offers Products', with the url and handle '/special-offers-products'.
Within my FOR loop I want to make an IF statement that simply asks each product within the FOR loop if it is also within this side collection 'Special Offers Products'. If this is TRUE display a little block of code that is essentially a tag that says 'Special Offer!'.
Childishly I have tried (and failed):
{% if product.collections contains 'special-offers-products' %}
<div class="special-offer-banner">
Special<br/>
Offer!
</div>
{% endif %}
I am now struggling to think of another way I could run this IF.
Here is my full product FOR loop:
{% for product in collection.products %}
<div class="single-product">
<div class="product-image" style="background-image: url({{ product.featured_image.src | img_url: 'large' }});">
</div>
{% if product.collections contains 'special-offers-products' %}
<div class="special-offer-banner">
Special<br/>
Offer!
</div>
{% endif %}
<div class="product-information">
<p class="product-title">{{ product.title }}</p>
<p class="product-vendor">{{ product.vendor }}</p>
<p class="product-price">{{ product.price | money }}</p>
{% unless product.available %}
<br><strong>sold out</strong>
{% endunless %}
<div class="product-buttons">
{% include 'view-button' %}
{% comment %}{% include 'add-to-cart-button' %}{% endcomment %}
</div>
</div>
</div>
{% else %}
<p>no matches</p>
{% endfor %}
How can I to correctly check if the product that is currently being looped over is also placed within another collection?
Edit
I do not want to know how to create a separate collection of products using the 'Special Offers Products' collection, this needs to be done within the same ALL products collection loop.
You were actually very close.
What's confusing you is that product.collections returns the collections object and not the collection handle.
So you can do this instead.
{%- assign collection_handles = product.collections | map: 'handle' -%}
{% if collection_handles contains 'special-offers-products' %}
<div class="special-offer-banner">
Special<br/>
Offer!
</div>
{% endif %}
This should work with your current logic then.

How does the loops and variables in Jekyll?

When I compare in if the post.category is as if there was a post that is the variable is more when I put {{post.category}} it prints the name. It's like if he did not exist in most if I put in if site.post.category handle all values. The problem is that I want to get the current post to compare if that category. I want to list the posts by category.
<div class="posts">
{% for post in site.posts %}
{% if post.category == Eventos %}
<hr>
<div class="post">
<h1 class="post-title">
<a href="{{ site.url }}{{post.url}}">
{{ post.title }}
</a>
</h1>
<span class="post-date">{{ post.date | date_to_string }} / {{ post.category }}</span> {{ post.excerpt }}
<h6>Leia mais...</h6>
</div>
{% endif %}
{% endfor %}
</div>
Try with categories !
{% if post.categories | contains: 'Eventos' %}

symfony2 form templating

I want to render a form. HTML for a field row should be like this:
<li class="text">
<label for="fieldname">
<div>
<input type="text" ... />
</div>
</li>
when the field type is text the li.class have to be the same.
I overwrite the field_row block:
{% block field_row %}
{% spaceless %}
<li class="text">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock field_row %}
but how to replace the class value?
You can try to attach a public member to your FormType class (If present...) and call it from the twig template.
Maybe also the attributes array of a form can be accessed in a twig template...
class YourType extends AbstractType
{
public $class = 'text';
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('fieldname');
}
//...
}
And
{% block field_row %}
{% spaceless %}
<li class="{{ form.class }}">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock field_row %}
Just replace the "field" word with the name of the type you want to modify.
You do it like this for text fields, but it's the same for any type:
{% block text_row %}
{% spaceless %}
<li class="text">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock text_row %}
or like this for textareas:
{% block textarea_row %}
{% spaceless %}
<li class="textarea">
{{ form_label(form, label|default(null)) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock textarea_row %}
The important part is the block name, it should be the same as the name of the type you want to modify. The "field_row" is the default for all field types if there is no exact matching name.
This also works for form-types you defined yourself (the ones that inherit from AbstractType, that's why it's important you add a name to your form-types, see http://symfony.com/doc/2.0/book/forms.html#creating-form-classes).

Nested liquid loops in a Jekyll archive page not working. Using an outer loop variable inside the inner's condition

I am working with the jekyll static site builder, and I am having
difficulty performing the following:
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
{% for post in site.categories[{{ category }}] %}
<li> {{ post.title }}</li>
{% endfor %}
&#8617
{% endfor %}
I have a post category in my jekyll site called 'test', and I can
display posts from it with the following:
{% for post in site.categories.test %}
<li> {{ post.title }}</li>
{% endfor %}
However, i want to build an archive page automatically, and in order
to do this, I need to embed the category from the outer loop (the loop
that visits all the categories), and use it inside the inner loop to
access posts from that specific category. What do I need to do to get
the first snippet to work how I want it to?
EDIT: Alternatively, is there another way to get the results that I want?
When you do for category in site.categories ,
category[0] will give you the category name,
category[1] will give you the list of posts for that category.
That's the way Liquid handles iteration over hashes, I believe.
So the code you are looking for is this one:
{% for category in site.categories %}
<h2 id="{{ category[0] }}-ref">{{ category[0] }}</h2>
<ul>
{% for post in category[1] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
<p>↩</p>
{% endfor %}
I've taken the liberty of fixing some markup issues - I've added <ul>...</ul> around the post link list, a <p> around the last link, a semi-colon after the 8617, and also fixed the id at the top (was missing the -ref part).
Regards!
How about...
{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
<ul>
{% for post in site.posts %}
{% if post.category == category[0] %}
<li> {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
&#8617
{% endfor %}
Sure, it's pretty inefficient and generates a bunch of extra whitespace, but it gets the job done.
[The original was missing the tags. Just added them. Also, to get ride of the whitespace, one can collapse everything from for post in site.posts to endfor onto a single line.]
{% for post in site.categories.category %}
- OR -
{% for post in site.categories.category[0] %}
Also, I'm not sure why kshep's example doesn't work...

Resources