Attributes

Attribute Filters

If you've worked with Rock for any length of time, you know Attributes are a key feature of its extensibility. Lava allows you to work with them in several powerful ways.

Finding Attributes Rock v2.0+

Most places that support Lava allow you to enable a "Debug" mode that lists all of the merge fields available. Fields that have "Attributes" show you those that are available; like so:

Using Attributes In Lava Rock v2.0+

Now that you know what's available to you, let's look at several ways we can access and use these attributes.

To get the attribute from a lava object, use the syntax below.

{{ CurrentPerson | Attribute:'BaptismDate' }}

This will return a string representation of the attribute. Sometimes you may want more from attributes though; especially if the attribute is a type like "Person," which has several more properties. To gain access to these properties you can add an optional filter qualifier, where the qualifier value maps to the property you want to display.

Mentor: {{ CurrentPerson | Attribute:'Mentor','LastName' }}

Attribute Inception Rock v3.0+

Hopefully, you're starting to see the power Lava with attributes. But what if you're wanting the attributes of an attribute? Take for instance the "School" attribute on a person. This attribute is actually a defined value that can have its own properties and attributes. You can access these additional attributes like this:

{% assign school = CurrentPerson | Attribute:'School','Object' %}
{{ school.Value }} // displays a property
{{ school | Attribute:'Grades' }} // displays an attribute

Note that the optional parameter "Object" tells the filter to return the full object and not just a string.

Looping Over Attributes Rock v3.0+

With the help of the enable debug window you often know exactly what attributes are available for you to use. Sometimes though the attributes available will change based on the item that's merged. For instance groups will often have different attributes available depending on the specific group that is provided. If you would like to list out all of the attributes available for the specific merge field you can use the Lava below.

{% for attribute in CurrentPerson.AttributeValues %}
    {{ attribute.AttributeName }}: {{ attribute.ValueFormatted }}
{% endfor %}

Global Attributes Rock v2.0+

Global attributes are a little different. You can access them using the notation below:

{{ 'Global' | Attribute:'OrganizationName' }}

What's Under the Hood? Rock v2.0+

When you get an attributes value you're often getting it's nicely formatted value. Sometimes you may wish to get it's underlying stored value. This is often helpful when you need to use it as a link or perhaps use it in a query. Using the 'raw' syntax below will give you the underlying value.

{{ 'Global' | Attribute:'OrganizationName','RawValue'  }}

Yeah But I Really Wanted a Link... Rock v2.0+

If it's a link you want it's a link you'll get. Well... most of the time. If the attribute inherits from 'ILinkableFieldType' (currently only 'Person' in core) or is an image or file you get exactly what you're looking for. For instance the 'Image' attribute would return a link ('http://rock.rocksolidchurchdemo.com/GetImage.ashx?guid=AF17CB8E-BA9C-4D2E-AB49-1FBE8A94A49D') to the image.

{{ 'Global' | Attribute:'OrganizationName','Url'  }}

OK... How Do I Read An Attribute That's A Key/Value Pair? Rock v4.0+

Key/value pair attributes are crazy powerful, but can be a little tricky in Lava. Never fear though... the code sample below makes them easy to work with.

{% assign favoriteLinks = CurrentPerson | Attribute: 'FavoriteLinks' %}

{% for link in favoriteLinks %}
  <p>
    {% assign linkkv = link | PropertyToKeyValue %}
    Website: {{ linkkv.Key }}<br />
    Url: {{ linkkv.Value }}
  </p>
{% endfor %}