With 2SexyContent 5.3, we introduced the relationship feature. This means that some sets of information can be related to each other. For example, a blog entry that has a specific author or multiple categories.
We’ve also created a video about relationship management, watch it here.
Preparing the Content Types
First, create the Content Types you want to connect. These types could look like this, if you have a blog entry and author:
Blog Entry
- Title (String)
- Description (String with WYSIWYG option)
Author
- Name (String)
- Description (String with multiline-option)
- Image (File)
To enable users to choose an author for each blog entry, create a field named Author with the new field-type Entity.
Then configure the EntityType field for the author, set it to Author (the name of the Content Type for authors).
After that, you should be able to choose an author for every entry you edit:
It’s also possible to enable the selection of multiple entities. Check the AllowMultiValue (configuration of the entity field).
Using in a template
Now that you can enter the data with relationships, you might want to output this in a Razor templates (Token templates do currently not support relationships).
If you have the same content type structure as in the example above, you can paste this into your template:
<br /><br /><h1>@Content.Title</h1><br /><p>@Html.Raw(Content.Description)</p><br /><br />@* If an author is selected, show it *@<br />@if(Content.Author.GetType() != typeof(String) && Content.Author.Count > 0) {<br /> var Author = Content.Author[0];<br /> <h2>About the author @Author.Name</h2><br /> <img src="@Author.Image?h=100&w=100" alt="@Author.Name" /><br /> <br /> <br /> @Author.Description<br />}<br /><br />
This will show the author, if it has been selected. Line 5 makes sure the template still works if no author has been selected. Line 6 defines the variable Author as the first Author in Content (note: The field Author could also contain multiple authors).
To list all assigned authors (or any other list of assigned entities), wrap the author HTML into a foreach loop:
<br /><br />@* Show all authors *@<br />@if(Content.Author.GetType() != typeof(String) && Content.Author.Count > 0) {<br /> foreach(var Author in Content.Author)<br /> {<br /> <div><br /> <h2>About the author @Author.Name</h2><br /> <img src="@Author.Image?h=100&w=100" alt="@Author.Name" /><br /> <br /> <br /> @Author.Description<br /> </div><br /> }<br />}<br /><br />
Have fun trying out the new relationship features, we hope you like it! If you have any questions, feel free to contact us at info@2sexycontent.org or visit 2sexycontent.org
Raphael