Last active
December 22, 2015 23:49
-
-
Save zpqrtbnk/6549963 to your computer and use it in GitHub Desktop.
Umbraco strongly typed published content experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// demo of a basic strongly typed published content for an article | |
// you just need to drop the following class anywhere in your code | |
// and every strongly typed methods returning IPublishedContent will | |
// actually return an Article instance (which is an IPublishedContent) | |
// we want the class named Article though the content type alias is 'newsArticle' | |
[PublishedContentModel("newsArticle")] | |
public partial class Article : PublishedContentModel | |
{ | |
public Article(IPublishedContent content) | |
: base(content) | |
{} | |
// that syntax may be verbose BUT it's fast and should be generated anyway | |
public string Title { get { return this.GetPropertyValue<string>("title"); } } | |
public string BodyText { get { return this.GetPropertyValue<string>("body"); } } | |
} | |
// use it | |
public class JustTesting | |
{ | |
public void Test() | |
{ | |
// get the titles of the articles at the root | |
var rootContent = UmbracoContext.Current.ContentCache.GetAtRoot(); | |
var rootArticles = rootContent.OfType<Article>(); | |
var titles = rootArticles.Select(a => a.Title); | |
// get the titles of all the articles below node 1234 | |
var node = UmbracoContext.Current.ContentCache.GetById(1234); | |
var articles = node.Descendants<Article>(); | |
titles = articles.Select(a => a.Title); | |
} | |
} | |
// and of course in just any Razor view: | |
@model.Title | |
@model.Children<SubArticle>.DateOfFirstRelease.ToString("yyyyMMdd"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment