Include content from other sections
This guide aims to walk you through what's possible with the linkTarget and contentInSection Handlebars Helpers.
You may want to re-use content in various places on your site.
A great way to do this may be to mirror content between sections, but this won't always give you the control you need.
For example, you may have a content type on your homepage that allows an editor to select a curated news article taht you want to promote.
This guide outlines how you can use Handlebars to achieve exactly that.
Imagine I have a content type on my homepage called “Select Promo News”
This content type has one element called “Select News Item” which is a section/content link.
The purpose of this is for a user to select a specific news content item from the News section, and have that be output on the current page
Here’s the text/html layout of my “Select Promo News” content type:
{{#with (linkTarget element="Select News Item")}}
{{!-- Assuming the user has selected a single content item of the content type called "NEWS" --}}
{{embed layout="text/homepage-promo"}}
{{/with}}
The above code is processing that embed as if it’s on the NEWS content type because we’re inside the with helper.
Here’s an expanded example:
{{publish element="Name"}} (Outputs the name of the "Select Promo News" Content item)
{{#with (linkTarget element="Select News Item")}}
{{!-- Assuming the user has selected a single content item of the content type called "NEWS" --}}
{{publish element="Name"}} (Outputs the name of the selected "NEWS" content item BECAUSE I'M INSIDE THE WITH BLOCK')
{{/with}}
That’ll work fine if the user selects a CONTENT link. But what if they select a SECTION Link.
Well, we have to check for that in our code. The way we can tell the difference is by checking the “Content ID”.
If the content ID is greater than 0 we know that the user selected a Content Item.
Otherwise we know they selected a Section.
{{#with (linkTarget element="Select News Item")}}
{{#gt (contentId) 0}} {{!-- Check if the content Id is greater than 0 --}}
{{!-- This is a content Link --}}
{{embed layout="text/homepage-promo"}}
{{else}}
{{!-- This is a section Link --}}
DO SOMETHING ELSE INSTEAD.
MAYBE OUTPUT AN ERROR?
{{/gt}}
{{/with}}
But remember, when we’re inside the with everything is being processed as if I’m inside the selected section.
This example shows how expressions inside and outside the with Helper will be processed differently.
{{sectionName}} (This will output "Home" because I've added the content to the Home Section)
{{#with (linkTarget element="Select News Item")}}
{{#gt (contentId) 0}}
{{!-- This is a content Link --}}
{{embed layout="text/homepage-promo"}}
{{else}}
{{!-- This is a section Link --}}
{{sectionName}}
THE ABOVE EXPRESSION will output "News" because the user selected the "News" section
and all Handlebars Helpers inside the `with` block are being processed as if they're in that selected section.
{{/gt}}
{{/with}}This is a really flexible approach that could be used in many ways throughout your site. Any time you need to get information from one page and output it in another, consider this approach.