Interface ITyped
This describes objects which usually wrap other objects to provide strictly typed access to properties.
have typed Methods to read properties like .String(propName)
.
It's usually the result of a AsTyped(something)
or AsItem(...)
command.
It's meant to help Razor etc. access unknown or dynamic objects in a typed way.
[PublicApi]
[JsonConverter(typeof(DynamicJsonConverter))]
public interface ITyped
Remarks
New in 16.02.
Methods
Attribute(string, NoParamOrder, string, bool?)
Return a value as a raw HTML string for using inside an attribute.
Usage like title='@item.Attribute("Title")'
It will do a few things such as:
- Ensure dates are in the ISO format
- Ensure numbers are in a neutral format such as
14.27
and never14,27
- Html encode any characters which would cause trouble such as quotes
IRawHtmlString Attribute(string name, NoParamOrder noParamOrder = default, string fallback = null, bool? required = null)
Parameters
name
stringName of the property
noParamOrder
NoParamOrderfallback
stringValue to use if the property specified by
name
doesn't existrequired
bool?throw error if
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- IRawHtmlString
Bool(string, NoParamOrder, bool, bool?)
Get a property and return the value as a bool
.
If conversion fails, will return default false
or what is specified in the fallback
.
bool Bool(string name, NoParamOrder noParamOrder = default, bool fallback = false, bool? required = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
booloptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- bool
Value as
bool
ContainsKey(string)
Check if this typed object has a property of this specified name. It's case-insensitive.
bool ContainsKey(string name)
Parameters
name
stringthe name like
Image
; some objects also support path to sub-property likeAuthor.Name
Returns
Remarks
Adding in 16.03 (WIP)
DateTime(string, NoParamOrder, DateTime, bool?)
Get a property and return the value as a DateTime
.
If conversion fails, will return default 0001-01-01
or what is specified in the fallback
.
DateTime DateTime(string name, NoParamOrder noParamOrder = default, DateTime fallback = default, bool? required = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
DateTimeoptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- DateTime
Value as
DateTime
Decimal(string, NoParamOrder, decimal, bool?)
Get a property and return the value as a decimal
.
If conversion fails, will return default 0
or what is specified in the fallback
.
decimal Decimal(string name, NoParamOrder noParamOrder = default, decimal fallback = 0, bool? required = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
decimaloptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- decimal
Value as
decimal
Double(string, NoParamOrder, double, bool?)
Get a property and return the value as a double
.
If conversion fails, will return default 0
or what is specified in the fallback
.
double Double(string name, NoParamOrder noParamOrder = default, double fallback = 0, bool? required = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
doubleoptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- double
Value as
double
Float(string, NoParamOrder, float, bool?)
Get a property and return the value as a float
.
If conversion fails, will return default 0
or what is specified in the fallback
.
float Float(string name, NoParamOrder noParamOrder = default, float fallback = 0, bool? required = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
floatoptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- float
Value as
float
Get(string, NoParamOrder, bool?, string)
Get a property.
object Get(string name, NoParamOrder noParamOrder = default, bool? required = null, string language = null)
Parameters
name
stringthe property name like
Image
- or path to sub-property likeAuthor.Name
(new v15)noParamOrder
NoParamOrderrequired
bool?throw error if
name
doesn't exist, see Convention: Property Required Name Checkslanguage
stringOptional language like
de
,de-ch
orde,en
to determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- object
The result if found or null; or error if the object is in strict mode
Remarks
- parameter
languages
added in 17.10
Get<TValue>(string, NoParamOrder, TValue, bool?, string)
Get a value using the name - and cast it to the expected strong type. For example to get an int even though it's stored as decimal.
Since the parameter fallback
determines the type TValue
you can just write this like
`something.Get("Title", fallback: "no title")
TValue Get<TValue>(string name, NoParamOrder noParamOrder = default, TValue fallback = default, bool? required = null, string language = null)
Parameters
name
stringthe property name like
Image
- or path to sub-property likeAuthor.Name
(new v15)noParamOrder
NoParamOrderfallback
TValuethe fallback value to provide if not found
required
bool?throw error if
name
doesn't exist, see Convention: Property Required Name Checkslanguage
stringOptional language like
de
,de-ch
orde,en
to determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- TValue
The typed value, or the
default
likenull
or0
if casting isn't possible.
Type Parameters
TValue
The expected type, like
string
,int
, etc. Note that you don't need to specify it, if you specify thefallback
property.
Remarks
- Added in v15
- parameter
languages
added in 17.10
Int(string, NoParamOrder, int, bool?)
Get a property and return the value as a int
.
If conversion fails, will return default 0
or what is specified in the fallback
.
int Int(string name, NoParamOrder noParamOrder = default, int fallback = 0, bool? required = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
intoptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- int
Value as
int
IsEmpty(string, NoParamOrder, string)
Check if this typed object has a property of this specified name, and has real data.
The opposite version of this is IsNotEmpty(...)
Important
This method is optimized for use in Razor-like scenarios. It's behavior is super-useful but maybe not always expected.
- If the value is a string, and is empty or only contains whitespace (even
) it is regarded as empty. - If the returned value is an empty list (e.g. a field containing relationships, without any items in it) it is regarded as empty.
If you need a different kind of check, just .Get(...)
the value and perform the checks in your code.
bool IsEmpty(string name, NoParamOrder noParamOrder = default, string language = null)
Parameters
name
stringthe property name like
Image
; some objects also support path to sub-property likeAuthor.Name
noParamOrder
NoParamOrderlanguage
stringOptional language like
de
,de-ch
orde,en
to determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- bool
true
if the property exists and has a real value. If it returned an empty list, it will also returnfalse
Remarks
- Added in 16.03
language
parameter added in 17.10
IsNotEmpty(string, NoParamOrder, string)
Check if this typed object has a property of this specified name, and has real data.
The opposite version of this is IsEmpty(...)
Important
This method is optimized for use in Razor-like scenarios. It's behavior is super-useful but maybe not always expected.
- If the value is a string, and is empty or only contains whitespace (even
) it is regarded as empty. - If the returned value is an empty list (e.g. a field containing relationships, without any items in it) it is regarded as empty.
If you need a different kind of check, just .Get(...)
the value and perform the checks in your code.
bool IsNotEmpty(string name, NoParamOrder noParamOrder = default, string language = null)
Parameters
name
stringthe property name like
Image
; some objects also support path to sub-property likeAuthor.Name
noParamOrder
NoParamOrderlanguage
stringOptional language like
de
,de-ch
orde,en
to determine which values to check. Will ignore languages not in the data model. On items that don't have ML data it will be ignored. new v17.10
Returns
- bool
true
if the property exists and has a real value. If it returned an empty list, it will also returnfalse
Remarks
- Added in 16.03
language
parameter added in 17.10
Keys(NoParamOrder, IEnumerable<string>)
Get all the keys available in this Model (all the parameters passed in). This is used to sometimes run early checks if all the expected parameters have been provided.
IEnumerable<string> Keys(NoParamOrder noParamOrder = default, IEnumerable<string> only = null)
Parameters
noParamOrder
NoParamOrderonly
IEnumerable<string>Only return the keys specified here, if found. Typical use:
only: new [] { "Key1", "Key2" }
. Useful to check if all or any specific keys exist.
Returns
Remarks
Added in 16.03
Long(string, NoParamOrder, long, bool?)
Get a property and return the value as a long
.
If conversion fails, will return default 0
or what is specified in the fallback
.
long Long(string name, NoParamOrder noParamOrder = default, long fallback = 0, bool? required = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
longoptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- long
Value as
long
String(string, NoParamOrder, string, bool?, object)
Get a property and return the value as a string
.
If conversion fails, will return default null
or what is specified in the fallback
.
string String(string name, NoParamOrder noParamOrder = default, string fallback = null, bool? required = null, object scrubHtml = null)
Parameters
name
stringproperty name
noParamOrder
NoParamOrderfallback
stringoptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name ChecksscrubHtml
objectIf
true
, will remove all HTML tags from the string. Ifp
will remove allp
tags, ifdiv,span
will remove these tags. This is the same as usingKit.Scrub.All(...)
or.Only(...). For more detailed scrubbing, use the
Kit.Scrub`
Returns
- string
Value as
string
Url(string, NoParamOrder, string, bool?)
Get a url from a field. It will do sanitation / url-corrections for special characters etc.
On TypedItems it will also auto-convert values such as file:72
or page:14
.
string Url(string name, NoParamOrder noParamOrder = default, string fallback = null, bool? required = null)
Parameters
name
stringThe field name.
noParamOrder
NoParamOrderfallback
stringoptional fallback if conversion fails
required
bool?throw error if the
name
doesn't exist, see Convention: Property Required Name Checks
Returns
- string
A url converted if possible. If the field contains anything else such as
hello
then it will not be modified.