Skip to main content

Content Tutorials

Tutorial Homecontent

Working with Content

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.


Show Content of the Current template

  • Name: Douglas Adams
  • Birthday: 3/11/1952

Show all Items assigned to current template

If a view has many items assigned (called a list) these should be retrieved from Data["Default"]. You will usually want to convert it to a dynamic list using AsDynamic(...). This example also uses Text.Has(...) to only show a picture if it really has a mugshot.

  • Douglas Adams
  • Terry Pratchett
  • Neil Gaiman
  • George Akerlof
  • Raphael Müller (not an author)
  • Ed Hardy

Show sub-items of current items

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

  • Douglas Adams
  • Terry Pratchett
  • Neil Gaiman
  • George Akerlof (awards: Nobel Peace Prize)
  • Raphael Müller (not an author)
  • Ed Hardy

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials.

<!-- unimportant stuff, hidden -->
@using Connect.Razor.Blade;
@using System.Linq;
@using Dynlist = System.Collections.Generic.IEnumerable<dynamic>;

<h2>Working with Content</h2>
In most cases a template will run in a... <!-- unimportant stuff, hidden -->


<hr>
<h3>Show Content of the Current template</h3>
<img src="@Content.Mugshot?w=100&h=100&mode=crop" align="right"  style="border-radius: 50%">
<ul>
  <li>Name: @Content.FirstName @Content.LastName</li>
  <li>Birthday: @Content.Birthday.ToString("d")</li>
</ul>

Show all Items assigned to current... <!-- unimportant stuff, hidden -->
<ul>
@foreach(var person in AsDynamic(Data["Default"])) {
  <li>
    @if(Text.Has(person.Mugshot)) {
      <img src="@person.Mugshot?w=50&h=50&mode=crop" width="50px" style="border-radius: 50%">
    }
    @person.FirstName @person.LastName
  </li>
}
</ul>

Show sub-items of current items This... <!-- unimportant stuff, hidden -->

<ul>
@foreach(var person in AsDynamic(Data["Default"])) {
  <li>
    @if(Text.Has(person.Mugshot)) {
      <img src="@person.Mugshot?w=50&h=50&mode=crop" width="50px" style="border-radius: 50%">
    }
    @person.FirstName @person.LastName
    @if(person.Awards.Count > 0 ) {
      // we just want the award names - to understand this, look at the LINQ tutorial
      var awardNames = (person.Awards as Dynlist).Select(a => a.Name);
      <span>
        (awards: @string.Join(",", awardNames))
      </span>
      

    }
  </li>
}
</ul>

<!-- unimportant stuff, hidden -->