IPublishedContent
is a strongly typed model for content, media and members and is used to render content in your views for your website.
To access the current page in your templates, copy-paste the below Razor code.
Listing and explanation of IPublishedContent properties and standard helpers for Content and Media.
Methods for IPublishedContent collections and filtering.
A library of extension methods to simplify working with IPublishedContent in collections to modify your HTML output. Examples of using IsHelpers
could be injecting CSS classes for alternating rows or to modify margins.
The IsHelper methods are a set of extension methods for IPublishedContent to help perform quick conditional queries against IPublishedContent nodes in a collection.
IsHelper methods are ternary operators, however they work a little nicer since they can be embedded in properties. They are also quicker to write because fewer brackets are needed for Razor to understand them.
An IsHelper can be invoked as a method of an IPublishedContent
.
Test whether the content is of a content type composed of the given alias.
Test whether the specified templateId
is an allowed template for the current node.
Test whether the specified templateAlias
is an allowed template for the current node.
By default the above template methods are disabled. To enable them, make sure to modify your appsettings.json to include the following JSON config keys inside Umbraco.CMS section:
Test if the current node is equal (by Id) to another node.
Test if the current node is not equal (by Id) to another node.
Test if the current node is a descendant of another node.
Test if the current node is the same as or a descendant of another node.
Test if the current node is an ancestor of another node.
Test if the current node is the same as or an ancestor of another node.
All collections of IPublishedContent
are IEnumerable<IPublishedContent>
. This means that all C# LINQ statements can be used to filter and query the collections.
Returns a collection of child items available in the current culture, below the current content item.
Returns a collection of child items for all cultures, below the current content item, regardless of whether they are available for the current culture.
Returns a collection of child items available in the specified culture with a default of the current one, below the current content item.
Returns all ancestors of the current page (parent page, grandparent and so on)
Returns the first ancestor of the current page
Returns a collection of all ancestors of the current page (parent page, grandparent and so on), and the current page itself
Returns all descendants of the current page (children, grandchildren etc)
Returns all descendants of the current page (children, grandchildren etc), and the current page itself
Filters a collection of content by content type alias
Filtering and Ordering are done with LINQ.
Some examples:
Groups collection by content type alias
Return only the number of items for a collection specified by the integer value.
Return items from the collection after skipping the specified number of items.
You can combine Skip and Take when using for paging operations
Returns the number of items in the collection
Returns a boolean True/False value determined by whether there are any items in the collection
Some filtering and routing behaviour is dependent upon a set of special naming conventions for certain properties. See also: Routing Property Conventions
If you create a checkbox property on a document type with an alias umbracoNaviHide
then the value of this property is used by the IsVisible()
extension method when filtering.
Use case: When displaying a navigation menu for a section of the site, following this convention gives editors the option to 'hide' certain pages from appearing in the section navigation. (hence the unusual umbracoNaviHide property alias!)
Built-in properties, which exists on all content objects by default
Common Examples
Returns the unique Id for the current content item
Returns the Name of the current content item in the current culture
Returns the Name of the current content item in the specified culture, null falls back to the current culture
Returns a strongly typed 'PublishedContentType' object representing the content type the IPublishedContent item is based on, that gives access to the alias
Returns a culture from a configured domain in the content tree.
Returns the parent content item
Returns a comma delimited string of Node Ids that represent the path of content items back to root.
Returns the Level (depth) this content item is in its tree path
Returns the id of the default Template object used with this content item.
There are extension methods to retrieve template alias (Model.GetTemplateAlias())
Returns the index the page is on, compared to its siblings
Returns the Url to the page.
Example: Getting a Danish Url for a site where a Danish language has been set up.
Example: Getting an Absolute Danish Url for a site where a Danish language has been set up.
Returns the Url encoded name of the page (slug) of the current culture
Returns the Url encoded name of the page (slug) of the specified culture
Returns the id of the Umbraco backoffice user that performed the last update operation on the content item.
Returns the name of the Umbraco backoffice user that initially created the content item.
Returns the id of the Umbraco backoffice user that initially created the content item
Returns the name of the Umbraco backoffice user that initially created the content item.
Returns the DateTime the page was created
Returns the DateTime the page was modified
All content and media items contain a reference to all the data defined by their Document Type. Custom property access is achieved using variations of the method: Value
Returns the property value for the specified property alias
The type returned of this property value is object
. This is fine in most cases since when using the above syntax, Razor will automatically execute a ToString()
on the result value.
See Model.Value<T>(string)
for how to return a strongly typed object for the property
Returns the property value for the specified property alias converted to 'T' - the requested output type of the property value.
For example, to return the string
result of "siteName":
If the current content item doesn't have the requested value, use an alternative 'fallback' value in its place.
Each of the examples below make use of an injected PublishedValueFallback. This is achieved by adding the following at the top of your Razor file:
This parameter is optional, but can make unit testing easier.
If a content page has a 'title' property, to fallback to use the 'Name' of the content item if the 'title' is not populated. Set the Fallback type to be Fallback.ToDefaultValue, and set the DefaultValue accordingly:
or to a specific value
Look for a property value on the current page. If it doesn't exist look for the property value on the parent page. Then the parent's parent page and so on. This approach allows specifying 'global property values' all the way up the content tree. These values can be overridden in different sections or individual pages.
If working with variants - fallback to a different language value - if perhaps the value hasn't been populated yet for the current language:
Use Fallback.To() to 'combine' Fallback options.
The following would first look for a 'title' property on all ancestors, before defaulting to the current page's name:
There are a few helpful methods to help check if a property exists, has a value or is null.
Returns a boolean value representing if the IPublishedContent has a property with the specified alias.
Returns a boolean value representing if the IPublishedContent property has had a value set.
It's possible to use 'Fallbacks' with HasValue: