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.
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
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item Data:
- Douglas (ID: 5338)
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, the current Content-item
- Content item Name: Douglas Adams
- Content item Birthday: 3/11/1952
- Content item Award: Hugo Award
- Content item Mugshot URL: /oqtane-tutorials/app/Tutorial-Razor/adam/Tm5yNs0hAEyc6HIID4k12g/Mugshot/douglas adams.png
- Content item Mugshot Raw String: file:212
Loop the persons in the Query for this view
- Douglas Adams
- Terry Pratchett
- Neil Gaiman
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item Data:
- Douglas (ID: 5338)
- Query: QuickRef-Persons-Selected
Details for Query: QuickRef-Persons-Selected
This query will retrieve Terry Pratchett on one stream, and 3 other persons an another stream.
- 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.
- Working with Block Contents
Show content which was entered for this module
⬇️ Result | Source ➡️
Douglas Adams on Content
Note: The Biography contains a LOT of HTML...
Teaser (using .Raw() for Umlauts):
Douglas Noël Adams (11 March 1952 – 11 May 2001) was an English author, humorist , and…
(drag size to see responsiv behavior)
Douglas Noël Adams (11 March 1952 – 11 May 2001) was an English author, humorist, and screenwriter, best known for The Hitchhiker's Guide to the Galaxy. Originally a 1978 BBC radio comedy, The Hitchhiker's Guide to the Galaxy developed into a "trilogy" of five books that sold more than 15 million copies in his lifetime.
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item Data:
- Douglas (ID: 5338)
The Presentation property only exists if the view is configured to use Presentation. In which case this is used to specify additional information how something should look or be presented.
⬇️ Result | Source ➡️
A Person on Content
Note that the following should be (probably green)
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: QuickRefContent
- Content/Item IsList: True
- Content/Item Data:
- Content number 1 (ID: 5861) - Presentation: green (ID: 5857)
- Content two (ID: 5862) - Presentation: red (ID: 5864)
- Content three (ID: 5866) - Presentation: cyan (ID: 5865)
- 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.
- Working with Block Contents
Show content which was entered for this module
Child items such as Awards
are accessed using Child("Awards")
. There are many ways to work with them.
⬇️ Result | Source ➡️
Use .Awards
to get one
- Award ID:
- Award Name: Hugo Award
- Award Name (Path): Hugo Award
Use .Awards
to get all
Award Count: 2
- Award: Hugo Award
- Award: Inkpot Award
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item Data:
- Douglas (ID: 5338)
- 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.
- Working with Block Contents
Show content which was entered for this module
Properties can contain urls such as /abc.jpg
or file references like file:72
.
⬇️ Result | Source ➡️
Douglas Adams, the current item (MyItem)
Use .Url(…)
to resolve file references such as file:72
- Mugshot field Value: /oqtane-tutorials/app/Tutorial-Razor/adam/Tm5yNs0hAEyc6HIID4k12g/Mugshot/douglas adams.png
- Mugshot URL: /oqtane-tutorials/app/Tutorial-Razor/adam/Tm5yNs0hAEyc6HIID4k12g/Mugshot/douglas adams.png
-
Mugshot Picture
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item Data:
- Douglas (ID: 5338)
Properties can contain urls such as /abc.jpg
or file references like file:72
.
⬇️ Result | Source ➡️
Douglas Adams, the current item (Content)
- File name: douglas adams
- File extension: png
- Size (bytes): 39125
- SizeInfo: 38.2 KB
Every file-field is actually a folder...
- Files count: 2
- Sub-Folders: 0
...which could hold many files.
If you want to show them, you need Kit.Image...
- douglas adams
- not-panicking
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item Data:
- Douglas (ID: 5338)
⬇️ Result | Source ➡️
Inspect the fields of an Item
- ✅ LastName: Adams
- ✅ IsAlive: False
- ✅ Haters: ToSic.Sxc.Data.DynamicEntity
- ✅ Birthday: 3/11/1952 12:00:00 AM
- ✅ FirstName: Douglas
- ✅ Awards: ToSic.Sxc.Data.DynamicEntity
- ✅ Sex:
- ✅ Mugshot: /oqtane-tutorials/app/Tutorial-Razor/adam/Tm5yNs0hAEyc6HIID4k12g/Mugshot/douglas adams.png
- ✅ Biography: <p><strong><img class="wysiwyg-right wysiwyg-50" src="/Portals/tutorial-razor/adam/Tutorial-Razor/Tm5yNs0hAEyc6HIID4k12g/Biography/douglas_adams_portrait.jpg" data-cmsid="file:douglas_adams_portrait.jpg">Douglas Noël Adams</strong> (11 March 1952 – 11 May 2001) was an English author, <a title="Humorist" href="https://en.wikipedia.org/wiki/Humorist" target="_blank" rel="noopener">humorist</a>, and screenwriter, best known for <i><a title="The Hitchhiker's Guide to the Galaxy" href="https://en.wikipedia.org/wiki/The_Hitchhiker%27s_Guide_to_the_Galaxy" target="_blank" rel="noopener">The Hitchhiker's Guide to the Galaxy</a></i>. Originally a 1978 <a title="The Hitchhiker's Guide to the Galaxy (radio series)" href="https://en.wikipedia.org/wiki/The_Hitchhiker%27s_Guide_to_the_Galaxy_(radio_series)" target="_blank" rel="noopener">BBC radio comedy</a>, <i>The Hitchhiker's Guide to the Galaxy</i> developed into a "trilogy" of five books that sold more than 15 million copies in his lifetime. <sup id="cite_ref-radioacad_2-0" class="reference"></sup></p>
Let's do some manual inspection
- ✅ "FirstName" exits and not empty?
- 🔲 "FirstName" exits and not empty?
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item Data:
- Douglas (ID: 5338)
In some scenarios you need may expect that data isn't there. In that case, you can create fake items to use in your razor.
⬇️ Result | Source ➡️
Inspect the fields of an Item
- ✅ FirstName: John
- ✅ LastName: Doe
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 AsList(Data)
.
If a view has many items assigned (called a list) these should be retrieved from Data
- if you don't specify a stream, it's treated as Data["Default"]
. You will usually want to convert it to a dynamic list using AsList(...)
. This example also uses Text.Has(...)
to only show a picture if it really has a mugshot.
⬇️ Result | Source ➡️
Loop persons which were added to this view
- Douglas Adams
- Terry Pratchett
- Neil Gaiman
⬇️ Result | Source ➡️
-
Create a heading tag the size specified in Presentation
Content number 1
FYI: HeadingWe are one!
-
Create a heading tag the size specified in Presentation
Content two
FYI: Heading noneTwo be or !2B
-
Create a heading tag the size specified in Presentation
Content three 🌟
FYI: Heading noneThree's the charm
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: QuickRefContent
- Content/Item IsList: True
- Content/Item Data:
- Content number 1 (ID: 5861) - Presentation: green (ID: 5857)
- Content two (ID: 5862) - Presentation: red (ID: 5864)
- Content three (ID: 5866) - Presentation: cyan (ID: 5865)
⬇️ Result | Source ➡️
Check out this list
- Douglas
- Terry
- Neil
View Configuration
This is how this view would be configured for this sample.
- Content/Item ContentType: Persons
- Content/Item IsList: True
- Content/Item Data:
- Douglas (ID: 5338)
- Terry (ID: 5339)
- Neil (ID: 5340)
- Header Type: QuickRefContentHeader
- Header Item: Check out this list (ID: 5860)
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