Adding attributes to Shopify's {% form %} liquid tag - forms

Is it possible to add attributes to Shopify's {% form %} liquid tag? I want to give all my forms id's so that I don't have to rely on parent elements to target their styles.

Yes you can add attributes.
Example:
{% form 'contact', id: "test" %}

Related

How to customize one specific field in Symfony Forms

I am rendering a form that contains several choice fields. I want to render one specific choice field differently from the other ones.
I've tried to use:
{% form_theme form 'MyBundle:Form:my-choice-field.html.twig' %}
{{ form_widget(field) }}
But that changes the look and feel of all choice fields in that form. If I place form_theme right next to the field I want to render differently, it does nothing.
I have thought of creating a new Form Type but it seems overkilling, I just want a different style for that specific field. Any ideas?
In the doc there is a section describing how to personalize an individual field, for example to customize the name field of a product entity only:
{% form_theme form _self %}
{% block _product_name_widget %}
<div class="text_widget">
{{ block('form_widget_simple') }}
</div>
{% endblock %}
{{ form_widget(form.name) }}
Here, the _product_name_widget fragment defines the template to use for the field whose id is product_name (and name is product[name]).
You can also override the markup for an entire field row using the same method:
{% form_theme form _self %}
{% block _product_name_row %}
<div class="name_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endblock %}
{{ form_row(form.name) }}

How to exceed 255 characters limit of product tag in Shopify?

I am looking for a way to exceed the 255 characters limit on product tag in Shopify admin. Please suggest any way of doing so.
In general this does not sound like a good idea. If you are trying to embed extra information per item you might want to look at putting that in a snippet file and then formatting your tag like __extra File1 and then a snippet like the following in your product template:
{% for tag in product.tags %}
{%if tag contains '__extra' %}
{% assign snip = tag | remove_first : '__extra ' %}
{% include snip %}
{% endif %}
{% endfor %}
This would allow you to share large chunks of information per product while not blowing up your tags.
If you adopt this approach then you'll also want to go through your theme and make sure you filter out tags beginning with '__'. e.g.
{% for tag in collection.all_tags %}
{% assign tag_pref = tag | slice:0,2 %}
{% unless tag_pref == '__' %}
... do your tag related layout
{% endunless %}
{% endfor %}

How to customize both DIV and TABLE form widgets without duplicating code in symfony

Trying to customize Symfony form rendering, I have the following in my config.yml
twig:
form_themes:
- 'form/fields.html.twig'
The form/fields.html.twig contains a customized number widget:
{%- block number_widget -%}
<input type="text" {{ block('widget_attributes') }} value="{{ value|filter }}" />
{%- endblock number_widget -%}
Everything works fine as long as I use default form layout (div). However, if I want the form to have table layout, I try to add something like this in the template:
{% form_theme 'form_table_layout.html.twig' %}
The form gets displayed as table, but then my customized number_widget is no longer customized!
I have also tried:
{% form_theme 'my_cystom_theme.html.twig' %}
with my_cystom_theme.html.twig containing:
{% use 'form_table_layout.html.twig' %}
Did not help.
Question:
Is there a way to make application wide customization of a widget for both div and table layout forms without duplicating the code?
The solution is to use a custom tempalte with multiple 'use' blocks:
create custom table-layout form, for example form/table_layout.html.twig, such as:
{# original table layout template #}
{% use 'form_table_layout.html.twig' %}
{# our custom field widgets #}
{% use 'form/fields.html.twig' %}
{# define more custom widgets for table layout here #}
In order to render a table layout form with our custom widgets, use this in the template:
{# form_theme 'form/table_layout.html.twig' #}
Do the same for div layout. Custom widgets from form/fields.html.twig will be used in both table and div layouts without duplicating the code.

Customize form_row for different form types

I want to set different form_row layout for different form type. I found in templates block named "choice_widget_collapsed", but it render only select tag with options.
I cannot find where this block is being used. Actually it seems that it is rendered instead of form_widget block. I suppose there is somewhere switch/if structure which checks form type and renders appropriate block, but i dont know where to find this switch, or dont know how to check input type inside form_row block.
I know that block type can be found inside form.vars.block_prefixes array, but this sux, because its position may change in the future as it was already.
So the question is: how can i make form_row display different thing depending on form field type?
You should override the normal block in a theme of yours. This block should work like this:
{% block form_appropriate_block %}
{% spaceless %}
{% if form.vars.widget = 'myIntendedWidgetType' %}
[yourTemplate]
{% else %}
{{ parent() }}
{% endif %}
{% endspaceless %}
{% endblock form_appropriate_block %}
And then, in your template, activate your theme using:
{% form_theme form 'MyBundle:Form:formTheme.html.twig' %}
In this way, your form theme is used only when needed, and if the type is not the one you want, it falls back to the normal behaviour.

Shopify Liquid - if product.template contains

I am trying to retrieve the template for a specific product while NOT on the product template page. I am on the collection page and am trying to adjust the Quick shop feature to only show the add to cart for products no associated with a custom template. For example, I've assigned a 'consultation' template to a group of products. However because I am no longer on the product template page, the Quick shop doesn't recognize the consultation product. Any ideas on how I can make this work other than using tags, types and collections?
Thank you!!
On collection page , products for that collection are called in a loop, something like
{% for product in collection.products %}
...
{% assign prodtemplate = product.template_suffix %}
{% if prodtemplate contains 'consultation' %}
{% else %}
//Add to Cart code
{% endif %}
...
{% endfor %}