Shopify - capture of Handlebars result not equal in liquid if - handlebars.js

So I am able to capture a handlebars result into a liquid variable and output the value, documentation states it is always a string. When I test the captured variable against another string variable that should be equal it always returns false. Not sure why this isn't working, anyone able to lend an assist?
{%- raw -%}
{{#items}}
{{#each tags}}{%- endraw -%}
{%- capture strTag -%}{%- raw -%}{{ this }}{%- endraw -%}{%- endcapture -%}
Captured Tag: {{ strTag }} <!-- will spit out the tag, it is being captured -->
{%- assign test_tag = "test tag" -%}
{%- if strTag == test_tag -%} <!-- this never fires as true, but both strTag and test_tag will output as "test tag" -->
{{ strTag }} matched {{ test_tag }}
{%- else -%}
{{ strTag }} did not match {{ test_tag }} <!-- this is what always returns as "test tag did not match test tag" -->
{%- endif -%}{%- raw -%}
{{/each}}
{/items}}{% endraw %}

Related

Form Themes. What differ functions: {{ form_label(foo) }} vs {{ block('form_label') }}?

In form themes once there is such notation:
{%- block form_row -%}
{{- form_label(form) -}}
{{- form_errors(form) -}}
{{- form_widget(form) -}}
{%- endblock form_row -%}
and once such:
{%- block number_widget -%}
{{ block('form_widget_simple') }}
{%- endblock number_widget -%}
The block() function evokes a block, so what form_widget(form) function does?
The function is using a parameter (that is form), while the block is not. So, you can't use {{ block('form_label') }} to render a specific label for a form, but you can use it in another block (where likely you want to customize somehow all your labels)

how to check in twig if a form field has label

when a form field is generated via an iteration:
{% for field in form %}
{{ form_widget(field); }}
{% endform %}
then it will encapsulates a div block surround the field's input type, and a label (if it has a label).
I want to seperate this into label, and widget:
<div class="form-group">
{% if field.vars.label is not null %}
{{ form_label(field) }}
{% endif %}
{{ form_widget(field) }}
</div>
but the field.vars.label is always null. BTW when I generates without the condition, then it's shows labels correctly.
How do I get the label's value for the condition check?
Thank you in advance.
Change the label to false and the form_label() twig function will automatically ignore it.
Here's a code snippet from the form_div_layout.html.twig file (see line 2):
{%- block form_label -%}
{% if label is not same as(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif -%}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
{%- endif -%}
{%- endblock form_label -%}
According to the doc of Form Variables Reference,
Try with:
{% if field.vars.attr.label is not null %}
instead of:
{% if field.vars.label is not null %}

Customising form labels in Symfony

I have a symfony page where I display a form, and I would like to add an
* in the labels of all fields that are required.
So I have my form.php.twig, that looks like this:
{% extends "::layout.php.twig" %}
{% block body %}
<div class="row">
{% use 'form_div_layout.html.twig' with form_label as base_form_label %}
{% block form_label %}
{{ block('base_form_label') }}
{% if required %}
<span class="required" title="This field is required">*</span>
{% endif %}
{% endblock %}
</div>
{% endblock %}
I followed the exact documentation of the symfony cookbook on how to customise labels, which is http://symfony.com/doc/current/cookbook/form/form_customization.html#cookbook-form-theming-methods.
But I keep getting this error
Variable "label" does not exist in form_div_layout.html.twig at line 206
I don't have any label variable in the code of my form.php.twig so I don't see why I get this error. And when I remove the
{{ block('base_form_label') }}
I get
Variable "required" does not exist in ATPlatformBundle:Session:create.php.twig
Can anyone help me on this? I don't see where is my mistake? I am not looking to customise it with css, but to add the * .
I have checked the form_div_layout.html.twig at line 206 and this is what there is
{%- block form_label -%}
{% if label is not sameas(false) -%}
{% if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif %}
{% if required -%}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif %}
{% if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ translation_domain is sameas(false) ? label : label|trans({}, translation_domain) }}</label>
{%- endif -%}
{%- endblock form_label -%}
Thank you in advance.
Did you try defining the label from inside your form builder?! The below field is a required one because unless you mention required => false, your field will be rendered as required.
Something like:
->add('name', 'text', array('label' => '* name'))
With Twig, you should have to test the existence of a variable before using it:
Variable "required" does not exist
{% if required is defined and ... }
You need to test this because you field isn't always required.
if you want further information, you have this page of the documentation:
http://twig.sensiolabs.org/doc/tests/defined.html
Ok, so at the end, I didn't manage to change labels within my form.php.twig (which is the template I use to display my form), but I used another technique.
I created a new file named fields.php.twig, which I put in MyBundle/Resources/views/Form.
At the beginning of my fields.php.twig, I then added
{% extends 'form_div_layout.html.twig' %}
and below it, I added
{% block form_label %}
{{ parent() }}
{% if required %}
<span> * </span>
{% endif %}
{% endblock form_label %}
Then in my form.php.twig, I added
{% form_theme form 'MyBundle:Form:fields.php.twig' %}
{{ form_start(form) }}
Myform here with {{ form_label(form.property) }}
{{ form_errors(form.property) }}
{{ form_widget(form.property) }}
{{ form_end(form) }}
It works perfectly, but I had to create the fields.php.twig.

Symfony2 - form theming based on field name

I want to create a choice_widget_collapsed theme, but only for one type of field, it can be checked by name, or by class name (it's entity field). Other choice field should be rendered by standard widget.
I've tried FIELDNAME_widget, CLASSNAME_widget, and I've searched in Google, but with no results.
EDIT
This is code of choice_widget_colapsed:
{% block choice_widget_collapsed -%}
{% if required and empty_value is none and not empty_value_in_choices and not multiple %}
{% set required = false %}
{% endif %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none -%}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
{%- endif %}
{%- if preferred_choices|length > 0 -%}
{% set options = preferred_choices %}
{{- block('choice_widget_options') -}}
{% if choices|length > 0 and separator is not none -%}
<option disabled="disabled">{{ separator }}</option>
{%- endif %}
{%- endif -%}
{% set options = choices -%}
{{- block('choice_widget_options') -}}
</select>
{%- endblock choice_widget_collapsed %}
I want to modify HTML of this widget, only for fields named color.
Problem solved.
When you want to change html of widget, based by field name, you have to overwrite block:
{% block _FORMNAME_FIELDNAME_widget -%}
{# your html #}
{%- endblock %}

How can you output HTML as entities in Twig?

I have widget examples that I need to output the HTML of so that people can copy and paste them onto their own websites. How can I escape the output so that it doesn't get rendered?
Ended up doing this, the key to the problem is using the escape filter as a block, the rest accounts for need for no spaces around <pre/><code/>'s to get the desired output:
<pre>
{%- spaceless -%}
<code>
{%- filter escape -%}
{% spaceless %}
{% include 'ACMEDemoBundle:Example:widget.html.twig' %}
{% endspaceless %}
{%- endfilter -%}
</code>
{%- endspaceless -%}
</pre>

Resources