Skip to main content
Home  › ... Razor

Content Items / Entities

Tutorial HomeContent
Requirements

Working with Block Contents

In most cases a template will run in a context - so something prepared data for the template, which should now be visualized. These examples assume you're working with 2sxc, which let's editors work with content - and your template only needs to visualize it. The current content item (if it's just one) 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.

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+) Selected: Typed (2sxc 16+) Switch to 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.RazorTyped

@MyItem.Img("Mugshot", settings: "Square", width: 100, imgClass: "rounded-circle")
<ul>
  <li>
    Name:
    @MyItem.String("FirstName")
    @MyItem.String("LastName")
  </li>
  <li>
    Birthday:
    @MyItem.DateTime("Birthday").ToString("d")
  </li>
  <li>
    Award:
    @MyItem.Child("Awards").String("Name")
  </li>
</ul>

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.

Work with Content Items - MyItem

MyItem is the object which contains the first thing added by the editor on the current block.

Every view/template receives prepared data, usually on the MyItem object.

⬇️ Result | Source ➡️

Douglas Adams on MyItem

Id, Guid and Title are built-in properties

  1. Id: 48832
  2. Guid: 36726e4e-21cd-4c00-9ce8-72080f8935da
  3. Title: Douglas

Use Get(…) if you don't care about the var type...

  1. Name: Douglas
  2. Birthday: 3/11/1952 12:00:00 AM

... or Get<T>(…) which will try to treat as the expected type.

  1. Birthday Get<string> 03/11/1952 00:00:00
  2. Birthday Get<DateTime> 3/11/1952 12:00:00 AM
  3. Birthday Get<bool> False
  4. Birthday Get<int> 0

Use typed methods such as .String(…) if you care about the variable type.

  1. Name (strings): Douglas Adams
  2. Birthday: 3/11/1952
  3. Is Alive: False
  4. Fav Num. Int: 42
  5. Fav Num. Float: 41.99

Use fallback: … to handle empty values or conversion problems.

  1. Name (strings): Douglas
  2. Name (int): 12345

Use .Attribute(…) to safely encode properties. Mouse over this to see the effect.

@inherits Custom.Hybrid.RazorTyped

<h4>Douglas Adams on <code>MyItem</code></h4>
<p>
  Id, Guid and Title are built-in properties
</p>
<ol>
  <li>Id:             @MyItem.Id</li>
  <li>Guid:           @MyItem.Guid</li>
  <li>Title:          @MyItem.Title</li>
</ol>

<p>Use <code>Get(…)</code> if you don't care about the var type...</p>
<ol>
  <li>Name:           @MyItem.Get("FirstName")</li>
  <li>Birthday:       @MyItem.Get("Birthday")</li>
</ol>

<p>... or <code>Get&lt;T&gt;(…)</code> which will try to treat as the expected type.</p>
<ol>
  <li>Birthday Get&lt;string&gt;    @(MyItem.Get<string>("Birthday"))</li>
  <li>Birthday Get&lt;DateTime&gt;  @(MyItem.Get<DateTime>("Birthday"))</li>
  <li>Birthday Get&lt;bool&gt;      @(MyItem.Get<bool>("Birthday"))</li>
  <li>Birthday Get&lt;int&gt;       @(MyItem.Get<int>("Birthday"))</li>
</ol>

<p>Use typed methods such as <code>.String(…)</code> if you care about the variable type.</p>
<ol>
  @* use .String(…), .DateTime(…), .Int(…) to make it typed *@
  <li>Name (strings): @MyItem.String("FirstName") @MyItem.String("LastName")</li>
  <li>Birthday:       @MyItem.DateTime("Birthday").ToString("d")</li>
  <li>Is Alive:       @MyItem.Bool("IsAlive")</li>

  @* use .Int(…), .Long(…), .Float(…) etc. for numbers *@
  <li>Fav Num. Int:   @MyItem.Int("FavoriteNumber")</li>
  <li>Fav Num. Float: @MyItem.Float("FavoriteNumber")</li>
</ol>

<p>Use <code>fallback: …</code> to handle empty values or conversion problems.</p>
<ol>
  @* this has no effect, as the value works *@
  <li>Name (strings): @MyItem.String("FirstName", fallback: "unknown")
  <li>Name (int):     @MyItem.Int("FirstName", fallback: 12345)
</ol>


<p title='@MyItem.Attribute("FirstName")'>
  Use <code>.Attribute(…)</code> to safely encode properties.
  Mouse over this to see the effect.
</p>

View Configuration

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

  • Content/Item ContentType: Persons
  • Content/Item Data:
    1. Douglas (ID: 48832)
  1. Working with Entity (Item) Values

    Every thing is an Entity. Here some basic examples how to show values like Name, Birthday etc. of such an Entity.

  2. Working with Block Contents

    Show content which was entered for this module

⬇️ Result | Source ➡️

Error Showing Content - please login as admin for details.

Work with List of Items - MyItems

Every view/template receives prepared data, either entered by the user on this page, or provided through a query. It can contain many items - so to get the list, use MyItems

⬇️ Result | Source ➡️

Loop persons which were added to this view

  • Douglas Adams
  • Terry Pratchett
  • Neil Gaiman
@inherits Custom.Hybrid.RazorTyped

<h4>Loop persons which were added to this view</h4>
<ul>
  @foreach (var person in MyItems) {
    <li>
      @person.Picture("Mugshot", settings: "Square", width: 50, imgClass: "rounded-circle")
      @person.String("FirstName") @person.String("LastName")
    </li>
  }
</ul>

View Configuration

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

  • Content/Item ContentType: Persons
  • Content/Item IsList: True
  • Content/Item Data:
    1. Douglas (ID: 48832)
    2. Terry (ID: 48833)
    3. Neil (ID: 48834)

⬇️ Result | Source ➡️

  • Content number 1
    FYI: Heading none

    We are one!

  • Content two

    FYI: Heading h5

    Two be or !2B

  • Content three 🌟

    FYI: Heading h6

    Three's the charm

@inherits Custom.Hybrid.RazorTyped

<ul>
  @foreach (var item in MyItems) {
    var pres = item.Presentation;
    
    var hType = pres.String("HeadingType");
    var title = item.Title + (pres.Bool("Highlight") ? " 🌟" : "");
    <li>
      @* Create a heading tag the size specified in Presentation *@
      @if (pres.IsNotEmpty("HeadingType")) {
        @Kit.HtmlTags.Custom(hType).Wrap(title)
      } else {
        @title
      }
      <br>
      <em>FYI: Heading @(pres.IsEmpty("HeadingType") ? "none" : pres.String("HeadingType"))</em>
      <div style='color: @pres.Attribute("Color")'>
        @item.Html("Contents")
      </div>
    </li>
  }
</ul>

View Configuration

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

  • Content/Item ContentType: QuickRefContent
  • Content/Item IsList: True
  • Content/Item Data:
    1. Content number 1 (ID: 49356) - Presentation: green (ID: 49352)
    2. Content two (ID: 49357) - Presentation: red (ID: 49359)
    3. Content three (ID: 49361) - Presentation: cyan (ID: 49360)

⬇️ Result | Source ➡️

Check out this list

  • Content number 1
  • Content two
  • Content three
@inherits Custom.Hybrid.RazorTyped

<h3>@MyHeader.Title</h3>
<ul>
  @foreach (var item in MyItems) {
    <li>
      @item.Title
    </li>
  }
</ul>

View Configuration

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

  • Content/Item ContentType: QuickRefContent
  • Content/Item IsList: True
  • Content/Item Data:
    1. Content number 1 (ID: 49356)
    2. Content two (ID: 49357)
    3. Content three (ID: 49361)
  • Header Type: QuickRefContentHeader
  • Header Item: Check out this list (ID: 49355)

This example builds on the last one, and additionally shows awards these authors have won, which is on the Awards property.

⬇️ Result | Source ➡️

  • Douglas Adams (awards: Hugo Award,Inkpot Award)
  • Terry Pratchett
  • Neil Gaiman
@inherits Custom.Hybrid.RazorTyped

<ul>
  @foreach (var person in MyItems) {
    <li>
      @person.Picture("Mugshot", settings: "Square", width: 50, imgClass: "rounded-circle")
      @person.String("FirstName") @person.String("LastName")
      @if (person.Children("Awards").Any()) {
        // we just want the award names
        // to understand this, look at the LINQ tutorial
        var awardNames = person.Children("Awards").Select(a => a.String("Name"));
        <span>
          (awards: @string.Join(",", awardNames))
        </span>
      }
    </li>
  }
</ul>

View Configuration

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

  • Content/Item ContentType: Persons
  • Content/Item IsList: True
  • Content/Item Data:
    1. Douglas (ID: 48832)
    2. Terry (ID: 48833)
    3. Neil (ID: 48834)