Customize form_row for different form types - forms

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.

Related

DBT Macros to perform computations

i was trying to write a macros which would be capable of doing various computations and return the final result.
Does macros accept only Block level codes like if or for loops? and not set of individual statements?
please help me with this
Thanks
There's a lot you can do with macros. Most native python operations are possible, albeit in a clunky non-pythonic way (no list comprehensions here).
For example, one can call any method of a python builtin data type:
{% set my_base_models = [] %}
{% for i in graph.nodes.values() %}
{% if i["name"].startswith('base_model') %}
{% do my_list.append(i) %}
{% endif %}
{% endfor %}
Key points:
call {% do <obj>.<method>() %} with any state-altering method (eg. pop() , append(), etc) to execute it
Put results into variables with {% set <var> = <expression> %}
Check out out filters and tests in standard jinja to filter and test mappings (dicts) and lists
Check out the list of dbt specific jinja context variables and methods

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

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

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.

How do you setup one or more different form_row templates in Symfony 2?

I have a project in Symfony2 where I have lots of different forms that do lots of different things and one thing I am struggling with at the moment is working out how I can set two different templates to handle form_rows.
I have specified one form_row template in a fiels.html.twig file that handles form rows as follows:
{% block form_row %}
{% spaceless %}
<div class="form-element{% if errors %} form-element-error{% endif %} widget-group clearfix">
<div class="widget widget-1-of-3">
{{ form_label(form) }}
</div>
<div class="widget widget-2-of-3">
{{ form_widget(form) }}
{% if errors %}
<small>{{ form_errors(form) }}</small>
{% else %}
{% if help is defined %}
<small>{{ help }}</small>
{% endif %}
{% endif %}
</div>
</div>
{% endspaceless %}
{% endblock form_row %}
This works great for me for general forms, but I also have a number of form elements that I have in tables as well. This is generally for groups of form data that I am updating.
In this case I then want to be able to specify using a different form_rows template.
I understand that I could specify the row via the field name or group, but I have a lot and I just want a more general way of doing this.
Does anyone know how this could be achieved.
You can have a lot of form themes defined in different templates.
Just add in your twig file on the top where you want to use theme:
{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}
{{ form_widget(form) }}
where form_theme file can be any twig file with theme definition.
Now form widget is decorated with attached theme.