[EN] mailworx Razor Syntax
The syntax of mailworx Razor templates
All mailworx-specific methods and properties are called via the base class "Model".
In addition, it is also possible to use all basic functions of C#.
Overview of the model:
Model.Campaign -campaign data
Subscriber -Subscriber data Sections -section data Editor -Editor-Functions Template -Template data and auxiliary functions Utilities -auxiliary functions
Auxiliary functions
General
Model.IsWebVersion
Return value
bool
Indicates whether the campaign is currently open in the browser.
@if(Model.IsWebVersion) {
...
}
Model.IsInEditMode
Return value:
bool
Indicates whether the campaign is currently in edit mode (campaign editor).
We recommend using a table structure to create the newsletter and set a maximum width.
@if (Model.IsInEditMode)
{
@Raw("<table width="600" border="0" cellpadding="0" cellspacing="0" align="center"><tr><td>")
}
...
@if (Model.IsInEditMode)
{
@Raw("</td></tr></table>")
}
Model.IsTestmail
Return value:
bool
This property returns True
, if the campaign is sent as a test email, sent to a test group, opened in edit mode or opened in the newsletter archive. For real mailings, False is set, unless the subscriber is deleted, in which case the personalisation of the preview subscriber is used and the campaign is classified as a test email.
@if(Model.IsTestmail) {
...
}
Model.IsForwardedMail
Return value:
bool
This property returns True
, if the campaign has been forwarded.
@if(Model.IsForwardedMail) {
...
}
Campaigns
Model.Campaign.Name
Return value:
string
Returns the name of the campaign.
<div>
@Raw(Model.Campaign.Name)
</div>
Model.Campaign.Id
Return value:
Guid
Returns the unique ID of the campaign.
<div>The unique ID of the campaign: @Raw(Model.Campaign.Id.ToString())</div>
Model.Campaign.Subject
Return value:
string
Returns the entered campaign subject or the subject variants that are intended for the current mailing in the A/B split test.
<head>
<title>@Raw(Model.Campaign.Subject)</title>
</head>
Model.Campaign.SenderAddress
Return value:
string
Returns the sender address of the current campaign.
<div>This campaign was sent from the address @Raw(Model.Campaign.SenderAddress).</div>
Model.Campaign.SenderName
Return value:
string
Returns the sender name of the current campaign.
<div>This campaign was sent from the address @Raw(Model.Campaign.SenderName).</div>
Model.Campaign.ShowTableOfContents
Return value:
bool
Specifies whether the table of contents in this campaign should be displayed using the "Show table of contents" option in the campaign properties.
<p>@if(Model.Campaign.ShowTableOfContents) {<br> <table cellpadding="0" cellspacing="0" border="0"><br> @foreach(var sec in Model.Sections.NonSingletonSections) {<br> switch(sec.DefinitionKey) {<br> case "article":<br> <tr><br> <td>@sec.GetValue("template_field_for_article_headline")</td><br> </tr><br> break;<br> case "teaser":<br> <tr><br> <td>@sec.GetValue("template_field_for_teaser_headline")</td><br> </tr><br> break;<br> }<br> }<br> </table><br>}</p>
Model.Campaign.ShowChangeProfile
Return value:
bool
Specifies whether the "Change data" button should be visible in the campaign or not.
Model.Campaign.ShowForward
Return value:
bool
Specifies whether the "Forward" button should be visible in the campaign or not.
Model.Campaign.ShowUnsubscribe
Return value:
bool
Specifies whether the "Unsubscribe" button should be visible in the campaign or not.
Model.Campaign.ShowAnyButton
Return value:
bool
Specifies whether one of the following buttons should be displayed to hide the environment HTML construct if no buttons are to be displayed.
@if(Model.Campaign.ShowAnyButton) {
<table cellpadding="0" cellspacing="0" border="0">
<tr>
@if(Model.Campaign.ShowChangeProfile) {
<td>@Raw(Model.Template.GetChangeProfileLink("Daten ändern"))</td>
}
@if(Model.Campaign.ShowForward) {
<td>@Raw(Model.Template.GetForwardLink("Weiterleiten"))</td>
}
@if(Model.Campaign.ShowUnsubscribe) {
<td>@Raw(Model.Template.GetUnsubscribeLink("Abmelden"))</td>
}
</tr>
</table>
}
Model.Campaign.ChangeProfileId
Return value:
Guid
Returns the ID of the form selected as the data modification form.
<div>The unique ID of the data change form: @Raw(Model.Campaign.ChangeProfileId.ToString())</div>
Model.Campaign.CreatedDate
Return value:
DateTime
Returns the creation date of the campaign.
...
<td>@Model.Campaign.CreatedDate.ToString("dd.MM.yyyy")</td>
...
Model.Campaign.ShortCulture
Return value:
string
Returns the language of the campaign in 2-character language format. Here are the most common examples: de, en, fr, es, ...
switch(Model.Campaign.ShortCulture) {
case "de":
...
break;
case "en":
...
break;
}
Model.Campaign.SendType
Return value:
Eworx.Mailworx.Architecture.Infrastructure.SendType
Returns the dispatch type of the current campaign so that you can respond to it in the template.
switch(Model.Campaign.SendType) {
case Eworx.Mailworx.Architecture.Infrastructure.SendType.Manual:
<div>This campaign will be sent manually..</div>
break;
case Eworx.Mailworx.Architecture.Infrastructure.SendType.EventDependent:
<div>This campaign is sent as an event of an action..</div>
break;
case Eworx.Mailworx.Architecture.Infrastructure.SendType.ABSplit:
<div>This campaign will be sent as an A/B split test..</div>
break;
}
Subscriber
Model.Subscriber.IsOptIn
Return value:
bool
Indicates whether the subscriber is logged in or not.
Model.Subscriber.OptOutDate
Return value:
DateTime?
Indicates the time since the subscriber unsubscribed. If the subscriber is subscribed, this property returns zero.
Model.Subscriber.LastProfileChange
Return value:
DateTime?
Indicates the time of the last update of the subscriber data. If the subscriber's data has never been updated, this property returns zero.
Model.Subscriber.Mailformat
Return value:
Eworx.Mailworx.Architecture.Infrastructure.Mailformat
Specifies the subscriber's email format..
switch(Model.Subscriber.Mailformat) {
case Eworx.Mailworx.Architecture.Infrastructure.Mailformat.Multipart:
...
break;
case Eworx.Mailworx.Architecture.Infrastructure.Mailformat.HTML:
...
break;
case Eworx.Mailworx.Architecture.Infrastructure.Mailformat.Text:
...
break;
}
Model.Subscriber.Id
Return value:
Guid
Returns the ID of the subscriber.
Model.Subscriber.AccountId
Return value:
Guid
Returns the ID of the client to which the subscriber is assigned.
Model.Subscriber.ShortCulture
Return value:
string
Returns the subscriber's language in 2-character language format. Here are the most common examples: de, en, fr, es, ...
switch(Model.Subscriber.ShortCulture) {
case "de":
...
break;
case "en":
...
break;
}
Model.Subscriber.GetValue(string fieldName)
Return value:
string
Returns the value of the field with the internal name ‘fieldName
’.
...
<td>
Your email address is @Raw(Model.Subscriber.GetValue("email"))
</td>
...
<td>
Your email address is [email protected]
</td>
Section
Model.Sections.GetSingletonValue(string fieldName)
Return value:
string
Returns the entered value of the field with the internal name ‘fieldName
’, which was entered in a non-repeatable section.
All fields from non-repeatable sections are available throughout the template and may only exist once; duplicate fields in a template will cause malfunctions
...
<td>
Here is the value of the field "li_preheader": @Raw(Model.Sections.GetSingletonValue("li_preheader"))
</td>
...
Model.Sections.GetAttributeOfSingletonImageValue(string fieldName, string attribute)
Return value:
string
Returns the entered value of the attribute of the image field with the internal name ‘fieldName
’, which was entered in a non-repeatable section.
The following attributes of an image can be queried:
-
width
-
height
-
src
-
alt
...
@Raw(Model.Sections.GetAttributeOfSingletonImageValue("li_image", "width")) ...
Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection.GetValue(string fieldName)
Return value:
string
Returns the entered value of the field with the internal name ‘fieldName´
’ that was entered in a repeatable section.
Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection.GetAttributeOfImageValue(string fieldName, string attribute)
Return value:
string
Returns the entered value of the attribute of the image field with the internal name ‘fieldName
’ that was entered in a repeatable section.
The following attributes of an image can be queried:
width
height
src
alt
Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection.DefinitionKey
Return value:
string
Returns the type name of the section and is used to distinguish which layout should be used.
Model.Sections.GetSectionForSingletonField(string fieldName)
Return value:
Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection
Returns the section containing the field with the internal name ‘fieldName
’.
Model.Sections.Sections
Return value:
List<Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection>
Returns all sections of the current campaign as a list. All repeatable and non-repeatable sections are included here.
@foreach(var sec in Model.Sections.Sections) {
...
}
Model.Sections.SingletonSections
Return value:
List<Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection>
Returns all non-repeatable sections of the current campaign as a list.
@foreach(var sec in Model.Sections.SingletonSections) {
...
}
Model.Sections.NonSingletonSections
Return value:
List<Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection>
Returns all repeatable sections of the current campaign as a list. This list is mostly used when no sections are defined in the template.
@if(Model.Campaign.ShowTableOfContents) {
<table cellpadding="0" cellspacing="0" border="0">
@foreach(var sec in Model.Sections.NonSingletonSections) {
switch(sec.DefinitionKey) {
case "article":
<tr>
<td>@sec.GetValue("template_field_for_article_headline")</td>
</tr>
<tr>
<td>@sec.GetAttributeOfImageValue("template_field_for_article_image", "src")</td>
</tr>
break;
case "teaser":
<tr>
<td>@sec.GetValue("template_field_for_teaser_headline")</td>
</tr>
break;
}
}
</table>
}
Model.Sections.GetSectionsOfType(string definitionKey)
Return value:
List<Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection>
Returns all sections of the same type ‘definitionKey
’ as a list.
<table cellpadding="0" cellspacing="0" border="0">
@foreach(var sec in Model.Sections.GetSectionsOfType("article")) {
<tr>
<td>@sec.GetValue("template_field_for_article_headline")</td>
</tr>
<tr>
<td>@sec.GetValue("template_field_for_article_text")</td>
</tr>
}
</table>
Model.Sections.GetSectionsOfArea(string areaName)
Return value:
List<Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection>
Returns the list of all sections in the “areaName
” area. Required to define different content areas in a template.
<table cellpadding="0" cellspacing="0" border="0">
@foreach(var sec in Model.Sections.GetSectionsOfArea("Oben")) {
if(sec.DefinitionKey == "article") {
<tr>
<td>@sec.GetValue("template_field_for_article_headline")</td>
</tr>
<tr>
<td>@sec.GetValue("template_field_for_article_text")</td>
</tr>
}
}
</table>
Editor
Model.Editor.SectionStart(Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection section, string tagName, int tdColspan, int tableWidth)
Return value:
string
Starts the beginning of the editable area of a repeatable section in the editor. This is where the buttons for editing (properties, copy, move, etc.) the section are created.
The ‘tagName
’ is the name of the first HTML tag in the section.
‘tdColspan
’ is an optional parameter and specifies how much colspan the first HTML tag (if it is a td tag) has. The default value is 1 if nothing is specified.
The parameter ‘tableWidth
’ can be used to specify a width for the generated structure, as otherwise a fit-to-content behaviour is triggered here.
@Raw(Model.Editor.GetAreaPlaceholder("", "table"))
@foreach (var sec in Model.Sections.NonSingletonSections) {
@Raw(Model.Editor.SectionStart(sec, "table"))
switch(sec.DefinitionKey) {
case "article":
<table>
...
</table>
break;
case "teaser":
<table>
...
</table>
break;
}
@Raw(Model.Editor.SectionEnd(sec, "table"))
}
Model.Editor.SectionEnd(Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection section, string tagName)
Return value:
string
Used to end the editable area of the section. Only the section and the corresponding tag name need to be specified here.
Model.Editor.GetAreaPlaceholder(string areaName, string tagName)
Return value:
string
Defines an area in which the sections are inserted. If no areas are required in the template, the areaName must be left blank.
If multiple areas are used, they must not be contained in the same parent element, otherwise the insertion and sorting of the sections will not work correctly.
Model.Editor.GetEditor(string fieldName, Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection section)
Return value:
string
Unlike Model.Sections.GetSingletonValue & Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection.GetValue, which are only used to read field values, the field value can be edited in the campaign editor.
Depending on the field type used, an editor is launched. Not all available field types can be edited directly in the editor using WYSIWYG. If the field type does not support editing, only the value is read.
If no section or null is passed as the second parameter, mailworx attempts to load a field with the internal name fieldName from the non-repeatable sections.
The following types support the WYSIWYG editor:
-
Text
-
Note
-
Image
@Raw(Model.Editor.GetAreaPlaceholder("", "table"))
@foreach (var sec in Model.Sections.NonSingletonSections) {
@Raw(Model.Editor.SectionStart(sec, "table"))
switch(sec.DefinitionKey) {
case "article":
@Raw(Model.Editor.GetEditor("template_field_for_article_image", sec)) @Raw(Model.Editor.GetEditor("template_field_for_article_headline", sec)) @Raw(Model.Editor.GetEditor("template_field_for_article_text", sec))
break;
...
}
@Raw(Model.Editor.SectionEnd(sec, "table"))
}
Template
Model.Template.CreateWebVersionLink()
Return value:
string
Generates the URL to display the campaign in the browser. This link is personalised for each subscriber.
<a href="@Model.Template.CreateWebVersionLink()">Browseransicht</a>
Model.Template.GetTemplatePath(string resource)
Return value:
string
Creates the URL for the corresponding resource in the template directory.
If no parameter is passed, only the path to the directory itself is read.
<img src="@Model.Template.GetTemplatePath("logo.png")" />
<img src="@Raw(Model.Template.GetTemplatePath())logo.png" />
Model.Template.GetAppPath()
Return value:
string
Provides the URL to mailworx; the configured subdomain is used here.
Model.Template.GetStaticLinkUrl(string internalStaticLinkName)
Return value:
string
Provides the URL for the static link in the template.
<a href="@Raw(Model.Template.GetStaticLinkUrl("staticlink"))">Statischer Link</a>
Model.Template.GetSectionAnchor(Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection section)
Return value:
string
Generates an anchor for the passed section.
@foreach (var sec in Model.Sections.NonSingletonSections) {
@Raw(Model.Template.GetSectionAnchor(sec))
...
}
Model.Template.GetAnchorToSectionStart(Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection section)
Return value:
string
Creates an opening a-tag with the anchor to the passed section.
@if(Model.Campaign.ShowTableOfContents) {
<table cellpadding="0" cellspacing="0" border="0">
@foreach(var sec in Model.Sections.NonSingletonSections) {
switch(sec.DefinitionKey) {
case "article":
<tr>
<td>@Model.Template.GetAnchorToSectionStart(sec)<span>@sec.GetValue("template_field_for_article_headline")</span>@Model.Template.GetAnchorToSectionEnd()</td>
</tr>
break;
case "teaser":
<tr>
<td>@Model.Template.GetAnchorToSectionStart(sec)<span>@sec.GetValue("template_field_for_teaser_headline")</span>@Model.Template.GetAnchorToSectionEnd()</td>
</tr>
break;
}
}
</table>
}
Model.Template.GetAnchorToSectionEnd()
Return value:
string
Create a closing a-tag for Model.Template.GetAnchorToSectionStart, as Razor checks for opening and closing tags and cannot compile if the number of opening and closing tags does not match.
Model.Template.GetSectionAnchorHrefValue(Eworx.Mailworx.Administration.Templates.RazorTemplates.RazorSection section)
Return value:
string
Creates the URL for the anchor to the passed section.
@if(Model.Campaign.ShowTableOfContents) {
<table cellpadding="0" cellspacing="0" border="0">
@foreach(var sec in Model.Sections.NonSingletonSections) {
switch(sec.DefinitionKey) {
case "article":
<tr>
<td><a href="@Model.Template.GetSectionAnchorHrefValue(sec)" style="color:red;">@sec.GetValue("template_field_for_article_headline")</a></td>
</tr>
break;
case "teaser":
<tr>
<td><a href="@Model.Template.GetSectionAnchorHrefValue(sec)" style="color:red;">@sec.GetValue("template_field_for_teaser_headline")</a></td>
</tr>
break;
}
}
</table>
}
Model.Template.GetOpenTracker()
Return value:
string
Create the opening and reading tracker for the campaign. It is best to insert the tracker at the end of the template.
ATTENTION! If this is not included in the template, the statistics will be generated incorrectly and mailworx will not be able to log all responses.
...
@Raw(Model.Template.GetOpenTracker())
</body>
</html>
Model.Template.GetChangeProfileLink(string text)
Return value:
string
Creates the "Update preferences" link (entire a-tag) with the text provided.
@Raw(Model.Template.GetChangeProfileLink("update preferences"))
Model.Template.GetForwardLink(string text)
Return value:
string
Creates the "Forward" link (entire a-tag) with the text provided.
@Raw(Model.Template.GetForwardLink("forward"))
Model.Template.GetUnsubscribeLink(string text)
Return value:
string
Creates the "Unsubscribe" link (entire a-tag) with the text provided.
@Raw(Model.Template.GetUnsubscribeLink("unsubscribe"))
Model.Template.GetChangeProfileURL()
Return value:
string
Returns the "Update preferences" link. This must be manually specified in an a-tag as an href attribute..
@Raw(Model.Template.GetChangeProfileURL())
Model.Template.GetForwardURL()
Return value:
string
Returns the "Forward" link. This must be specified manually in an a-tag as an href attribute.
@Raw(Model.Template.GetForwardURL())
Model.Template.GetUnsubscribeURL()
Return value:
string
Returns the "Unsubscribe" link. This must be manually specified in an a-tag as an href attribute.
@Raw(Model.Template.GetUnsubscribeURL())
Utilities
Model.Utilities.UrlEncode(string text)
Return value:
string
Converts a string for transmission in a URL
@Raw(Model.Utilities.UrlEncode("Text"))
Model.Utilities.UrlDecode(string text)
Return value:
string
Converts a string back from a URL.
@Raw(Model.Utilities.UrlDecode("Text"))
Model.Utilities.ConvertColor(string color, char typ)
Return value:
float
Returns the converted value of the colour passed. The colour must be passed in the format #rrggbb, e.g.: #CC0000.
A character must be passed for the typ
parameter.
- h
Hue (from the HSB colour space)
Possible values: 0.0 to 360.0 (hue is specified in degrees) - s
Saturation (from the HSB colour space)
Possible values: 0.0 (greyscale) to 1.0 (full saturation - b
Brightness (from the HSB colour space)
0.0 (black) to 1.0 (white)
If a colour cannot be processed because it is in the wrong format, the function returns **-1**
as the return value.
// Hue
@Raw(Model.Utilities.ConvertColor("#C30709", 'h').ToString())
// 359,3617
// Saturation
@Raw(Model.Utilities.ConvertColor("#C30709", 's').ToString())
// 0,9306931
// Brightness
@Raw(Model.Utilities.ConvertColor("#C30709", 'b').ToString())
// 0,3960784
// Invalid color
@Raw(Model.Utilities.ConvertColor("#farbe", 'b').ToString())
// -1
Razor
The Razor Template Engine requires valid HTML code. This means that every open tag must be closed accordingly. To get around this, the @Raw(string)
function can be used to output HTML code that is excluded from validation.
@variable name
Return value:
string
Outputs the value of the variable – however, HTML tags are encoded.
@{
string VariableWithHtml = "<b>Text</b>";
}
<div>@VariableWithHtml</div>
<div><b>Text</b></div>
@Raw(string Variable)
Return value:
string
Outputs the value of the variable – HTML tags are not encoded.
@{
string VariableWithHtml = "<b>Text</b>";
}
<div>@Raw(VariableWithHtml)</div>
<div><b>Text</b></div>
Helpers
Helpers can be used to make code reusable. This is useful for buttons, for example.
Helpers are very similar to functions. Individual parameters can be passed and responded to within the helper itself.
@helper MyHelper(string link, string text = "") {
if(!string.IsNullOrEmpty(link)) {
if(text == "") {
text = "Weiterlesen";
}
<a href="@Raw(link)" style="background:red;color:white;text-decoration:none;">@Raw(text)</a>
}
}
@MyHelper("http://www.mailworx.info")
<a href="http://www.mailworx.info" style="background:red;color:white;text-decoration:none;">Weiterlesen</a>
@MyHelper("http://www.mailworx.info", "Mein <b>Lieblings</b> EMS System")
<a href="http://www.mailworx.info" style="background:red;color:white;text-decoration:none;">Mein <b>Lieblings</b> EMS System</a>
Updated about 6 hours ago