In this tutorial you will learn how to create Layout Overrides for Custom Fields, when displaying them with Articles Anywhere.

Introduction

With the Pro version of Articles Anywhere, you can display any Joomla! Custom Field of an article using the name of that specific field.

Let's say you want to show the value of the custom field "Habitat" - that has a field name habitat-area - of article "Lions". You can simply do:

{article title="Lions"}[habitat-area]{/article}

If you assigned a Layout to your custom field in its Render Options, Articles Anywhere will account for it and output the field using that layout (without the label).

But in the data tag you are able to customize the output of the custom field by setting an alternate Layout. Let's take a look at how this is done:

Create Alternate Field Layout

First, to know exactly how to create an alternative layout file, and to see some layout examples, follow our tutorial on How to create an Alternate Layout for Custom Fields in Joomla.

Now, if you assigned this layout to the fields in its Render Options, this would also apply to the standard output of the field. Which we don't want, since we only want to use this alternate layout in Articles Anywhere, without affecting the standard output.

Using the Layout in Articles Anywhere

As mentioned, by default Articles Anywhere will use the Layout assigned to that custom field in its Render Options.

However, in the data tag you can override the set layout with another one (to be placed inside templates/my_template/html/layouts/com_fields/field/), by simply inserting the name of the file in the layout="..." attribute:

[habitat-area layout="habitat-field"]

Value Layout

Example: List Field

Customizing Value Layouts is especially useful if you need further control on custom field types that have multiple values, such as in fields of type "List", or "Checkboxes".

In Articles Anywhere, you can now use the new Foreach Structure feature on List Fields to have full control over the output of the values:

<ul>
{foreach data="habitat-area"}
<li>[row]</li>
{/foreach}
</ul>

Check the dedicated page for details on how to use it, which includes how to output the multiple values in a bullet points list.

Example: Articles Field

Articles Field from Regular Labs is a Joomla! custom field type that gives you the ability to choose and display a list of articles, allowing you to directly link articles to each other.

The Pro version of Articles Field already allows to fully customize the output of the linked articles in the article page, thanks to its Custom HTML Layout feature. But what if you would like to output an Articles Field with a different/alternate layout when displaying it through Articles Anywhere? You can, too!

You can override the custom_html parameter to pass another Custom HTML Layout to the field. 

So if you have an Articles Field called [speaker], you can simply override the Custom HTML Layout set in the field parameters by doing:

[speaker custom_html="<a href='[url]'>[title]</a>"]

You can combine the data available for the Articles Field Custom HTML Layout with common presentational HTML. Links, images, headings, paragraphs, divisions, spans, lists, tables, and inline formatting are supported.

The HTML supplied through custom_html is sanitized after its data tags are replaced. Scripts, styles, embedded content, forms, event-handler attributes such as onclick, and unsafe URL schemes such as javascript: are removed. Use CSS classes instead of inline style attributes.

Because the custom_html attribute itself uses double quotes in these examples, use single quotes around HTML attribute values inside it. Encoding the angle brackets as &lt; and &gt; prevents the editor from changing the tag syntax; Articles Anywhere decodes them before safely rendering the HTML.

You can even add/override separators between the articles returned by Articles Field, if you desire:

[speaker separator=", " last-separator=" and " custom_html="<a href='[url]'>[title]</a>"]