Skip to content

Instantly share code, notes, and snippets.

@kgiszewski
Last active March 11, 2020 14:12
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kgiszewski/8863822 to your computer and use it in GitHub Desktop.
Save kgiszewski/8863822 to your computer and use it in GitHub Desktop.
Archetype Template Use Cases
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Archetype.Models;
@using Archetype.Extensions;
@{
Layout = null;
}
//use case #1 - Covers a single Archetype
@foreach (var fieldset in Model.Content.GetPropertyValue<ArchetypeModel>("a1"))
{
<!-- get a property by name -->
<div>@fieldset.GetValue("mntp")</div>
<!-- or -->
<!-- if using a property editor with a value converter -->
<div>@fieldset.GetValue<IEnumerable<IPublishedContent>>("mntp")</div>
}
//use case #2 - Covers nested Archetypes
@foreach (var fieldset in Model.Content.GetPropertyValue<ArchetypeModel>("a1"))
{
<div>@fieldset.GetValue("firstName")</div>
<!-- get nested Archetype -->
foreach(var nestedFs in fieldset.GetValue<ArchetypeModel>("movieList")){
<h3>@nestedFs.GetValue("title")</h3>
<div>@nestedFs.GetValue("text")</div>
}
}
//use case #2; be able to iterate over the properties, however this will not pass value through converters
@foreach (var fieldset in Model.Content.GetPropertyValue<ArchetypeModel>("a1"))
{
<!-- will not go through value converters -->
foreach (var prop in fieldset.Properties)
{
<div>@prop.Value</div>
}
}
@mattbrailsford
Copy link

In example 3, you can change:

var nested = fieldset.GetValue<Archetype>(prop.Alias);

to just

var nested = prop.GetValue<Archetype>();

@kgiszewski
Copy link
Author

Users of Archetype 0.5.1-alpha and before will have to append a .Fieldsets to the end of Model.Content.GetPropertyValue<Archetype>("a1")

@kgiszewski
Copy link
Author

As of Archetype 1.0.1-beta, the models type name has been changed from Archetype to ArchetypeModel. Also the namespace no longer contains .umbraco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment