Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Razor templates
HOW TO USE RAZOR BASED TEMPLATES IN DYNAMICWEB
Version: 1.0
2013.06.24
English
LEGAL INFORMATION
Copyright 2013 Dynamicweb Software A/S (Ltd).
All rights reserved. Alteration or reproduction of this document or parts hereof is strictly
prohibited, regardless of form or means, unless explicit permission has been acquired
from Dynamicweb Software.
Dynamicweb is a registered trademark of Dynamicweb Software.
Company and product names mentioned in this document may be registered trademarks
or trademarks of third parties.
ii
CONTENTS
Introduction _________________________________ v
What this document is about..................................................... v
Who this document is for .......................................................... v
Related documents ................................................................... v
Revision history ........................................................................ v
Introduction.................................................................... 1
1.1.1 Read more ......................................................................... 1
1.2
2.4
2.5
2.6
Loops ............................................................................ 6
Conditionals................................................................... 8
Convert HTML templates to Razor ................................ 8
2.6.1 Editing Razor templates ................................................... 11
2.7
Snippets ...................................................................... 17
Global tags .................................................................. 18
Date extension tags ..................................................... 19
iii
Contents
iv
INTRODUCTION
What this document is about
This document explains how to use Razor as the template language in Dynamicweb.
Traditionally templates in Dynamicweb have been based on HTML and XSLT. This
document explains how to convert to and use Razor templates.
Related documents
Revision history
Documentation updates:
First version (12th June, 2013): Released
Bug fixes/improvements:
8.2.3.2: Introduces a GlobalTags property to the PageView object to get hold of the
global tags known from the HTML based templates.
WHAT IS RAZOR
1.1
Introduction
Razor is an ASP.NET programming syntax released by Microsoft in January 2011 as one
of the view engines for ASP.NET MVC. It is a template markup syntax and is a mix of
HTML and .NET code in the same file, like classic ASP and PHP.
This means that templates have the simplicity of HTML and the power of .NET. Where the
HTML based templates in Dynamicweb have very limited scripting capabilities and
requires .NET based add-ins to do more advanced rendering logic, the Razor based
templates have the possibility to utilize all of the Dynamicweb and .NET APIs to perform
the rendering.
Razor is maybe a new technology for some developers, but is fast to learn since it is
HTML and .NET (C# or VB) so not a new language, but a new combination of existing
implementation methods.
The benefits is that implementation and rendering logic can be developed faster and gives
the developer full freedom to achieve a well working website.
Do install ASP.NET MVC 4 when developing Razor templates in your Visual Studio for
better syntax support: http://www.asp.net/mvc/mvc4
Version 1.0
1.2
What is Razor
ASP.NET MVC extends the capabilities in the Razor view engine and these capabilities
are not available in Dynamicweb Razor templates. Examples could be the @Html and
@Url which are ASP.NET MVC specific helper classes.
Version 1.0
2.1
Upload your razor template to any template folder and select the template where you
want to apply it.
You do not have to change all templates to Razor. You can change just one or more
templates as needed.
Version 1.0
2.3
Tags
To get the value of a tag, use a method called GetValue:
@GetValue("ParagraphText")
Version 1.0
These Get methods returns safe values meaning i.e. GetInteger will ALWAYS return
a valid integer. If the passed tag name does not exist or is not an integer, it will return 0. If
the original value is required, including null references etc, use i.e.
(int)GetValue(TagName)
Version 1.0
2.4
Loops
To loop elements in a Dynamicweb template loop, use a method called GetLoop:
GetLoop("Images")
The GetLoop method will return a Generic.List(Of LoopItem) of the loop name
specified (Images) that can be iterated.
The entire syntax could look like this:
@foreach (LoopItem i in GetLoop("Images"))
{
//Loop items
}
Inside the foreach statement, the items of the loop are available in the current
LoopItem (i) in the above example.
To get a value of a property of a loopitem, use the GetValue method of the LoopItem
object:
@foreach (LoopItem i in GetLoop("Images"))
{
<div>
@i.GetValue("Gallery.Image.Thumb.Medium")
</div>
}
Version 1.0
2.5
Conditionals
In Dynamciweb HTML templates conditionals are a Dynamicweb specific syntax. In Razor
conditionals are regular .NET syntax (C# or VB) and gives full programming capabilities
against the template values.
2.6
Read
more on
condition
als in C#
Dynamicweb has the possibility of converting an existing HTML based template to Razor
http://ms
(C#).
dn.micro
soft.com/
enThere are two ways of converting an existing HTML template to Razor.
us/library
/5011f09
h.aspx
1. From any template selector where a HTML template is selected (See Figure 2),
Version 1.0
In the Ribbon, click the button Convert to Razor. This will convert the contents of the
editor to Razor.
Version 1.0
After the conversion, click the Save as... button, and rename the file to .cshtml
Version 1.0
10
The conversion will handle the most common scenarios in the HTML templates it will
convert these things
1. Template tags (<!--@SomeTag-->)
2. Loops (<!--@LoopStart(nameOfLoop)-->)
3. If Defined (<!--@If Defined(SomeTag)-->)
More advanced template implementations are not converted (conditionals). The converter
may not always convert all your code 100% correctly and you therefore need to manually
validate the conversion to see if it is ok and make the needed changes to have it work
fully.
The conversion cannot be guaranteed to be 100% correct the converted file can have
some syntax errors. These have to be handled manually.
The best editor for Razor templates is Visual Studio 2012 with ASP.NET MVC 4 installed.
You can connect to the filemanager using webdav: http://manual.dynamicwebcms.com/Dynamicweb-On-line-Manual/Management-Center/Web-andHTTP/WebDAV.aspx. Any other text based editor can be used as well.
Version 1.0
11
2.7
The @model
In MVC and Razor, you normally access data properties on the Model dynamic object.
In Dynamicweb, you can access Model but in this first implementation of Razor in
Dynamicweb Model is not the type being rendered, but the Template instance in context.
In Dynamicweb Razor templates you can access the model like this:
@Model.PropertyName
Do not use @Model yet. @Model.Template can be used, but is not guaranteed to stay
on the Model.
Version 1.0
12
LAYOUTS IN RAZOR
3.1
Content placeholders
Layouts in Razor
Content placeholders in Razor based Layout files are defined in the same way as in the
HTML based templates. See layout documentation (en-US) Designs and Layouts.pdf for
full examples.
The placeholder is defined by having a class (dwcontent), and id and a title attribute
specified. None of these can contain Razor code.
It is possible though, to add additional classes to the placeholder element using Razor.
Just make sure the class name is added using a variable. The additional class name is
then added runtime. The dwcontent class name has to be static.
Version 1.0
13
Layouts in Razor
It is possible to test if a placeholder returns a value by checking if the tag containing the
rendering of the content of the placeholder has a value.
3.2
Navigation
Navigations in Razor based Layout files are defined in the same way as in the HTML
based templates. See layout documentation (en-US) Designs and Layouts.pdf for full
examples.
The template applied to the UL is a XSLT template and cannot be converted to a Razor
template.
The navigation is defined by having a class (dwnavigation), and id and a title attribute
specified. None of these can contain Razor code.
Do not use Razor code in the attributes of a navigation. Unlike placeholders, the class
attribute cannot contain a Razor specified class. Wrap the entire UL in a div instead.
Version 1.0
14
3.3
Layouts in Razor
The master template can be either html or cshtml. Dynamicweb will load and merge the
master and layout template into one Razor template that will be parsed by the Razor
engine as one.
The content placeholder in the master template is defined using the
ContentPlaceHolder tag. Even though it looks like a regular Dynamicweb template
tag, it is not, so it has to follow this syntax.
Since the master and layout template are merged into one, they share context and the
same Razor code can be used in both.
Razor sections (@section) are defined in Razor views and rendered in the Razor layout
file (Master page). Since Dynamicweb does not currently have a Razor layout file,
sections are not supported.
Snippets can be used instead of sections.
Version 1.0
15
3.4
Layouts in Razor
Attribute values
The attributes of HTML elements in a template can sometimes have the need for getting a
Dynamic value defined in the code of the Razor code or have the value from a
Dynamicweb template tag.
Attributes in HTML are defined as attribute=value. In Razor it is possible to let the
value be a code block.
This will work just fine in all templates except Layout templates
(/Templates/Designs/nameOfDesign/Layoutfile.html).
Layout files are loaded as a DOM in Dynamicwebs layout engine and having code blocks
with inside an attribute value will make the DOM parser fail.
To use Razor in attribute values in Dynamicweb layout and master templates, define a
variable with the value and use that variable as the attribute value.
Do not use Razor code in the attributes values in layout files.Do define a variable in a
code block and use that variable as the attribute value.
Version 1.0
16
4.1
Snippets
Dynamicweb 8.2 introduced snippets. They define a piece of markup that is moved from
one place in any template (source) to any other place in any template (destination).
Snippets are defined using a HTML template syntax <!--@Snippet--> and in Razor
templates this syntax is the same. This is because the snippets are not handled by the
template engine itself, but by the output replacer.
Define a snippet source with the SnippetStart and SnippetEnd tags.
Version 1.0
17
4.2
Global tags
Global tags in HTML templates gives the possibility to get hold of information from the
execution context in any template, like Pageview, cart etc.
Razor has the possibility to get access to everything because the Dynamicweb API can
be utilized directly in the template.
Therefore, global template tags are not available as regular tags when using a Razor
template. It coulde be the <!--@Global:Area.ID--> tag. In Razor that would translate
into @GetValue(Global:Area.ID) but this is not available in Razor templates. No
Global.* tags are available from Razor using the GetValue, GetString etc.
Instead of using the global tags, consider using the API instead.
Figure 20. Using the API to get values normally available in Global tags.
Version 1.0
18
4.3
Date extension tags are not available in Razor templates. Use @GetDate() and .NET
date formatting instead.
Version 1.0
19