Use rules on fields and editables to gain more control over what fields are shown in the editor.
Often, you may have editables and fields that are not needed within a module all of the time. You can use rules on these tags to control when they are shown and hidden, in a similar way to how you can use rules to control which HTML is included or removed from an email.
It's also possible to hide fields by nesting them. For example having CTA fields inside a checkbox that turns the button on and off. When the checkbox is turned on the fields show, when it's turned off the nested fields are hidden. For example:
<field name="CTA_toggle" label="Turn CTA on or off" type="checkbox"> <field name="CTA_text" label="CTA text" type="text"></field> </field>
This approach only works with fields, not editables, and only if you have all the fields grouped together (so they can be nested inside another field).
You can also use rules on fields to have more control over what appears in the editor. For example, if you have a module where you can switch between 2 and 3 columns of content, and you don't want the fields for the third column to show when the email only has two.
Or if you have a dropdown to control how many bullet points you want to include, you can hide any fields that aren't being used.
<field name="columns" label="number of columns" type="dropdown"> <choice value="2" label="Two"></choice> <choice value="3" label="Three"></choice> </field> <editable name="column_3" label="Column 3" rule="{% unless columns == 3 %}{% remove %}{% endunless %}"> <field name="headline" type="text"></field> <field name="image" type="src"></field> <field name="body" type="text"></field> <field name="cta_text" type="text"></field> </editable>
Here, the editable which is for the third column is only included when 3 is selected in the dropdown. If anything else is selected (in this case 2 is the only other option) then these fields will be removed.
<field name="bullets" label="number of bullets" type="dropdown"> <choice value="1" label="One"></choice> <choice value="2" label="Two"></choice> <choice value="3" label="Three"></choice> <choice value="4" label="Four"></choice> </field> <editable name="bullet_one"> </editable> <editable name="bullet_two" rule="{% if bullets < 2 %}{% remove %}{% endif %}"> </editable> <editable name="bullet_three" rule="{% if bullets < 3 %}{% remove %}{% endif %}"> </editable> <editable name="bullet_four" rule="{% if bullets < 4 %}{% remove %}{% endif %}"> </editable>
In this example, the editable for the first bullet point is always visible as it does not have any rules on it. Each subsequent editable is only shown if the relevant choice has been selected. So the second editable is only shown if the dropdown is set to 2 or higher, the third is shown if the dropdown is 3 or higher, and so on and so forth.
If you use a rule on an editable, all the fields inside it will also be removed. If you use a rule on a field only that field will be removed, unless there are nested fields in that field - these will be removed too.
You cannot use any remove_outer or show_outer rules on fields and editables. You can use remove, show_if, show_unless, remove_if and remove_unless rules.
For full details on rules see the rules and control flow tags pages.