symfony2 form templating - webforms

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).

Related

How can I render a template inside asyncEach in nunjucks

{% asyncEach example in component.examples %}
{% include "./example.njk" %}
{% endeach %}
// example.njk
<article class="spectrum-CSSExample">
<h3 id="{{example.slug}}" class="spectrum-CSSExample-heading spectrum-Heading spectrum-Heading--sizeS">
{{example.name}}
<div class="{{ spectrum-StatusLight spectrum-StatusLight--sizeM spectrum-CSSExample-status spectrum-StatusLight--{{ util.getStatusLightVariant(example.status) }}">
{{example.status}}
</div>
</h3>
{# {% include "./exampleContent.njk" %} #}
</article>
In example.njk example is undefined. How can I pass example inside a include like this?
example is always undefined inside example.njk

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.

How to show current tags filter in Shopify only if the current tags are more than 1

I need help please with showing the tags filter only if the current tags are more than 1,
Currently my filter shows even in the case on a single tag. I tried the following but couldn't find the exact liquid syntax:
<dd>
<ol>
{% for t in tags %}
{% assign tag = t | strip %}
{% assign tag_value = tag | handleize %}
{% if current_tags contains tag %}
{% if current_tags > 1 %}
<li>
<input type="checkbox" value="{{ tag_value }}" checked/>
<label>{{ tag }}</label>
</li>
{% else %}
{% if collection.tags contains tag %}
<li>
<input type="checkbox" value="{{ tag_value }}"/>
<label>{{ tag }}</label>
</li>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</ol>
</dd>
Thanks in advance
You can use this if condition to check current tags are more than 1
{% if current_tags.size > 1 %}
{% endif %}

Sorting data from table field octobercms

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.

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' %}

Resources