Channel Field

Last updated: September 24th 2019

To populate data on front-end, there is several ways to enhance and customize output. You can just use {my_channel_field_short_name} under your {exp:channel:entries} tag to get output of your SDF field. There is some parameter too to enhance/customize your output.

Single line code

Under your channel entry, You can just pass {my_channel_field_short_name} to fetch value of given field. You can also pass other variables defined in JSON to get respected item of JSON for given value.

Single line codes examples:
{my_channel_field_short_name} // Get Value
{my_channel_field_short_name:value} // Get Value
{my_channel_field_short_name:label} // Get Label
{my_channel_field_short_name:custom_field} // Get custom_field passed in JSON for given value

Tagpair method to get field data

Mostly, If you are using checkboxes, you will have more than one values to comes in result. You can manage it by tagpair method. Using tagpair method has some advantages. you can define prefix, Get total number of values and count etc.

There is few parameters that you can use in tagpair method:

prefix

Passing prefix will be used as prefix of field variable sub fields. Default field prefix will be item

{my_super_dynamic_field prefix="msd"}
    {msd:value}
    {msd:label}
    {msd:custom_field}
{/my_super_dynamic_field}

markup

Markup will just wrap your output in either <ul> or <ol>.

{my_super_dynamic_field markup="ul"} // or markup="ol"

// Output (data: option1|option3)
<ul>
    <li>option1</li>
    <li>option3</li>
</ul>

separator

For Multi relationships or Checkboxes field, Default separator for single line code is comma (,). If you want to change it to something else you can use this parameter

{my_super_dynamic_field separator="|"} 

// Output (data: option1|option3)
option1|option3
{my_super_dynamic_field separator="&"} 

// Output (data: option1|option3)
option1&option3

limit

If you have checkboxes selected as a SDF fieldtype, You can use this parameter to get limited number of values.

{my_super_dynamic_field limit="1"}
{my_super_dynamic_field limit="1"}
    {item:value}
    {item:label}
    {item:custom_field}
{/my_super_dynamic_field}

backspace

Backspace use to remove last n number of characters from the output. If you are using tagpair method, you can pass this parameter to cut final characters:

{my_super_dynamic_field limit="5" backspace="2"}{item:value}, {/my_super_dynamic_field}

// Output (data: option1|option3)
option1, option3

Variables:

Variables you can use Tagpair method: (assuming item as a prefix.)

item

In tagpair, {item} will work as {value}.

{my_super_dynamic_field}
    {item}
{/my_super_dynamic_field}

count

Count of current loop index.

{my_super_dynamic_field}
    {item:count}
{/my_super_dynamic_field}

label

Label of current value that you have passed in JSON.

{my_super_dynamic_field}
    {item:label}
{/my_super_dynamic_field}

value

Value of current selected option.

{my_super_dynamic_field}
    {item:value}
{/my_super_dynamic_field}

custom_field

In JSON, You can pass anything that you can fetch at frontend. It can be named anything.

{my_super_dynamic_field}
    {item:custom_field}
{/my_super_dynamic_field}

total_results

Total results of current tagpair loop. If you have 5 values selected in channel entry, value of this variable would be 5.

{my_super_dynamic_field}
    {item:total_results}
{/my_super_dynamic_field}

absolute_results

Absolute results will return accurate total of selected values in channel entry, Only difference between this parameter is, it not affects limit parameter like total_results do.

{my_super_dynamic_field}
    {item:absolute_results}
{/my_super_dynamic_field}

Examples:

Here is some examples of use this fieldtype in different ways:

Single Line Code:
{my_super_dynamic_field}

// Output for Radio and Select dropdown (data: option1)
option1

// Output for Checkboxes (data: option1|option3)
option1, option3
Wrap output in ul/ol:
{my_super_dynamic_field markup="ul"} // or markup="ol"

// Output (data: option1|option3)
<ul>
    <li>option1</li>
    <li>option3</li>
</ul>
Tagpair method to get data:
{my_super_dynamic_field}
    {if item:count == 1} Total results are: {item:total_results} <br>{/if}
    {item:count} : {item} - {item:label} - {item:value} - {item:custom_field} <br>
{/my_super_dynamic_field}

// Output (data: option1|option3)
Total results are: 2
1 : option1 - Option 1 - option1 - Custom Field DATA
2 : option3 - Option 3 - option3 - Custom Field DATA
Use Prefix in tagpair code:
{my_super_dynamic_field prefix="msd"}
    {if msd:count == 1} Total results are: {msd:total_results} <br>{/if}
    {msd:count} : {msd} - {msd:label} - {msd:value} - {msd:custom_field}  <br>
{/my_super_dynamic_field}

// Output (data: option1|option3)
Total results are: 2
1 : option1 - Option 1 - option1 - Custom Field DATA
2 : option3 - Option 3 - option3 - Custom Field DATA
Pass Limit parameter:
{my_super_dynamic_field limit="1"}
    {if item:count == 1} Displaying {item:total_results} from total of {item:absolute_results}<br>{/if}
    {item:count} : {item} - {item:label} - {item:value} - {item:custom_field} <br>
{/my_super_dynamic_field}

// Output (data: option1|option3)
Displaying 1 from total of 2
1 : option1 - Option 1 - option1 - Custom Field DATA
Convert on your own way:
{my_super_dynamic_field limit="5" backspace="2"}{item:value}, {/my_super_dynamic_field}

// Output (data: option1|option3)
option1, option3