1

Ok I know how to list radio buttons and use it to display a particular value:

{% set newsType = craft.fields.getFieldbyHandle('newsType') %}
{% for option in newsType.settings.options %}
    {% if option.value == 'newsValue' %}
        <h3 class="news-type">{{  option.label }}</h3>
    {% endif %}
{% endfor %}

(I am not in an existing entry context.) So I would like to know if there is a shortcut to directly get this label from the value? Like that syntax :

{% set newsTypeLabel = craft.fields.getFieldbyHandle('newsType').option('newsValue').label) %}
4

You could make a macro and use that as a "shortcut":

{% macro optionLabel(fieldHandle, optionValue) %}

    {%- set field = craft.fields.getFieldbyHandle(fieldHandle) %}
    {%- for option in field.settings.options %}
        {%- if option.value == optionValue %}
            {{- option.label }}
        {%- endif %}
    {%- endfor %}

{%- endmacro %}
{% import _self as macros %}

<h3>{{ macros.optionLabel('newsType', 'newsValue') }}</h3> 
  • Interesting yes, I did not know about twig macros yet. – migswd Oct 6 '15 at 11:53
1

If you just want to get the selected radio button’s label, in the context of an existing entry, you can do this:

{% if entry.newsType|length %}
    <h3 class="news-type">{{ entry.newsType.label }}</h3>
{% endif %}

(Documented here: http://buildwithcraft.com/docs/radio-buttons-fields#templating)

  • Thx Brandon but thats the problem: I am not in the existing entry context : I have an entry type filter, I want to display the entry type (radio buttons) label that I am filtering on (from craft.request.segment(2)). – migswd Oct 8 '15 at 6:15
  • 1
    In that case the other answer is your best bet :) – Brandon Kelly Oct 8 '15 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.