Skip to main content
Home  › ... Razor

Content Items / Entities

Tutorial HomeContent
Requirements

Working with Entity (Item) Values

Every item a.k.a. object is called an Entity. Entities will have properties like NameProductNumber or Birthday, depending on the Content-Type they represent.

How your Razor Code receives Entities

When things are configured correctly, the right entities are given to your Razor-template so you can just show them or perform calculations as you need them. These are the common ways for the entities to be provided to your template:

  1. On simple templates, the UI allows users to add/edit content of a specific type (configured in the View). This content Entity is always available in your code in the variable called MyItem or Content on older Razors.
  2. On templates which expect many items of the same type, MyItem/Content contains the first item only. To access all (for iterating and showing each one) the Entities are in a variable called MyItems (Data on older Razors).
  3. If your code needs to find other data in the App, it can access it through App.Data.
  4. If your code want to get Entities which have been pre-selected/filtered etc. through a query, it will get them from Query

In this example the template is configured to contain an Entity of the type Person and has these fields:

  • FirstName: a string (text)
  • LastName: a string
  • Birthday: a date
  • Mugshot: a link to an image file
  • Awards: a reference to one or more other Entities which describe awards

So the following examples will show how to put the values in these fields into the HTML.

In the new typed RazorPro the current item is on a variable called MyItem. It's a typed object, so to read properties you need to specify what you want it, eg. @MyItem.String("FirstName").
This sample also uses the @MyItem.Picture(...) to automatically show a responsive image on the page.

In the dynamic Razor base classes lke Razor14 the current item is always available on the variable called Content. It's a dynamic object, so you can just type things like @Content.FirstName to access the properties.
This sample also uses the @Kit.Image.Img(...) to automatically show a responsive image on the page.

The samples can differ based on your Razor base class or if you're running an old version.
Switch to Strong-Typed (2sxc 17.06+) Switch to Typed (2sxc 16+) Selected: Dynamic (Razor14 or below)

Show Entity Values from the current Data

Showing values from Data (aka. Entities) is very easy. Normally they are accessed through Item (new Razor) or DynamicEntity (older Razor) objects. 

⬇️ Result | Source ➡️

  • Name: Douglas Adams
  • Birthday: 3/11/1952
  • Award: Hugo Award
@inherits Custom.Hybrid.Razor14

@Kit.Image.Img(Content.Field("Mugshot"), settings: "Square", width: 100, imgClass: "rounded-circle")
<ul>
  <li>Name: @Content.FirstName @Content.LastName</li>
  <li>Birthday: @Content.Birthday.ToString("d")</li>
  <li>Award: @Content.Awards.Name</li>
</ul>

@*
  Note that Awards refers to other Entities of the type PersonAwards and has properties like Name.
  This example shows the award Name using @Content.Awards.Name.
  This only makes sense when you expect one award.
  In other tutorials you'll see how to work with such related Entities if there are more than one.
*@

View Configuration

This is how this view would be configured for this sample.

  • Content/Item ContentType: Persons
  • Content/Item Data:
    1. Douglas (ID: 48832)

Note that Awards refers to other Entities of the type PersonAwards and has properties like Name. The above example showed the award Name using @Content.Awards.Name - which makes sense when you only expect one award. In other tutorials you'll see how to work with such related Entities if there are more than one.