Quick Reference for all APIs
This is a quick CheatSheet to provide you with a reference to most APIs you'll use.
This uses a base class of @inherits Custom.Hybrid.Razor14 and would also apply to ...Razor12. Most samples do not work for the new @inherits Custom.Hybrid.Razorpro as that provides different objects and methods. that uses a different syntax.
Switch to Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)
- Show firstName (string): Terry
-
Show birthday (DateTime):
4/28/1948 12:00:00 AM -
Show birthday (DateTime, formatted):
1948-04-28 - Show age (int): 77
- Show decades (int): 7
- Show decadesFloat (float): 7.7
- Show decadesInt (int): 7
- Basic set variable and show
Create variables and show using
@variableName
⬇️ Result | Source ➡️
Show old/not old based on Terry's age using
@if and @else:
this guy is old
Ternare show old/not old based on age ? ... : :
this guy is old
- Razor Conditions such as if, if-else and ? :
if/else and ternary operator
(condition ? true : false)
Demostrates C# 6 and 7 in Razor. C# 7.3 must be installed this to work - see docs.
⬇️ Result | Source ➡️
C# 6 Features
Demonstrates C# 6 features in Razor views.
Call various properties / functions C# 6
- Expression bodied Function: 62
- Expression bodied Property: 62
- Auto property initializer: 62
- Read-Only property initializer: 62
Show old/not old based on Terry's age using
@if and @else:
this guy is old
Ternare show old/not old based on age ? ... : :
this guy is old
⬇️ Result | Source ➡️
Loop through Pets using
@foreach (var thing in list)
- dog
- cat
- mouse
Loop through Pets using
@for (counter; condition; increment)
- dog - owned by Daniel
- cat - owned by John
- mouse - owned by Markus
- Loops -
forandforeachLoops using
for()andforeach()
⬇️ Result | Source ➡️
Output values (HTML-source shown)
- this is text, it doesn't have tags
- this string <em>has</em> html <strong>tags</strong>
Output values (HTML is used; maybe security risk)
- this is text, it doesn't have tags
- this string has html tags
- Work with HTML Output
Learn the difference of showing variables with
@variableand@Html.Raw(variable), and re-use very simple snippets
⬇️ Result | Source ➡️
You can just use any Emoji in your source. Or you pick them with ternary operations.
- This is just an emoji: 🚀
- Pick emoji from true ⇒ ✔️
- Pick emoji from true ⇒ ❌
- Working with 😃 Emojis / Emoticons
Show Emojis in your output or use them for showing true/false
Reuse Razor and CSharp Files
Don't-Repeat-Yourself (DRY) by reusing code in Razor and helper C# files.
⬇️ Result | Source ➡️
Reuse some functions inside this file
- Boolmoji(true) ⇒ 👍🏽
- Boolmoji(false) ⇒ 👎🏽
- Random number function: 84
- Random number function: 48
- this is a bold item in a list using a Template Delegate
- another bold item in a list using a Template Delegate
- Code - Function and Similar Helpers
Normal C# functions are the basic block for reusing code.
- Use Shared C# Code
Use a shared razor file to hold multiple functions / helpers, and call them one-by-one as needed.
- Reuse Code and Razor Templates
Explains the basics of re-using code across templates and code files.
Work with Content Items - MyItem
MyItem is the object which contains the first thing added by the editor on the current block.
Work with Settings and Resources
Settings can be configured at many levels, the most-local (eg. the App) would override the most global (eg. Presets).
Set Page Title, Keywords etc.
⬇️ Result | Source ➡️
Note: you won't see any output here. To see the effect, look at the page source.
Output is Invisible as it only affects the HTML Head
This sample modifies the HTML head, so it's not visible here.
To see the effect, look at the source in the browser
- Set Page Title, Keywords, Descriptions etc.
Get/Set Page Title, Keywords, Description and set meta-tags and more.
- Set <base> tag in header
Add a <base> tag to the header. This is important for SPA JS applications.
- Page Icons for Favicon, Apple/Android
Add various combinations of icons to the page header
- Meta and other Tags in header
Add all kinds of meta-tags to the header.
Set OpenGraph Headers etc.
⬇️ Result | Source ➡️
This uses Kit.Page.SetTitle(...) and other methods to modify the HTML sent to the browser.
It sets various aspecs such as title or FavIcons.
meta title,meta description,meta keywords- favicon
- some JsonLd for google
- OpenGraph headers for FaceBook, Twitter, etc.
Output is Invisible as it only affects the HTML Head
This sample modifies the HTML head, so it's not visible here.
To see the effect, look at the source in the browser
- Set Open-Graph headers for Social Media using the IPageService
Add Open-Graph data headers for Facebook, Twitter and other sharing-systems
- JSON-LD Headers for SEO
Add JSON-LD (Linked Data) headers for Google
A core challenge is to activate JavaScripts only when needed - and when all dependencies are loaded. This is what turnOn does. It checks if all specified JS objects exist, and then triggers the JS code.
⬇️ Result | Source ➡️
The following text will be replaced once the JS is triggered:
This example passes data to the JS, so it can be parameterized:
We can also pass in more sophisticated data:
Work with the new UniqueKey with JavaScript - new v16.04 🌟
The UniqueKey is a new feature in 2sxc 16.04 which allows you to create unique IDs for HTML elements. The value of UniqueKey is the same in all Razor and C# files for the same Content-Block.
⬇️ Result | Source ➡️
This separate Razor will create a div for the resulting message, based on the same UniqueKey which is -DGrH60r
(script didn't run yet - this elements id is demo-uniquekey-msg--DGrH60r)
Sometimes you need a UniqueKey which also depends on other objects. For example, you may need to have a UniqueKey which also uses another value - or many. This is done using @Kit.Key.UniqueKeyWith(...).
⬇️ Result | Source ➡️
-
Unique Key with
12345:-DGrH60r-n12345 -
Unique Key with
"hello":-DGrH60r-s-583028946 -
Unique Key with
"bad chars in id ! % / 👍🏽":-DGrH60r-s-929439254 -
Unique Key with
"this is a long text and should be shortened":-DGrH60r-s1583847656 -
Unique Key with
12345and"hello":-DGrH60r-n12345-s-583028946
If the UniqueKey is based on known object types such as Entities, this works very well. For example, you may need to loop through a list of items, and each item needs a unique key.
⬇️ Result | Source ➡️
-
Title:
Hitchhikers Guide to the Galaxy- UniqueKey:-DGrH60r-eid7md5eRsH -
Title:
Good Omens- UniqueKey:-DGrH60r-eidcAcM-LSz -
Title:
Phishing for Phools- UniqueKey:-DGrH60r-eidlkSe34Oo -
Title:
The Last Continent- UniqueKey:-DGrH60r-eid_EnbtOtU