Skip to content

Comparison

Jacob Jensen edited this page May 24, 2016 · 3 revisions

Based on: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/

Code Block

Razor

 @{
      int x = 123; 
      string y = "because.";
 }

Diamond

@{
     int x = 123; 
     string y = "because.";
}

Expression (Html Encoded)

Razor

<span>@model.Message</span>

Diamond

@(Text to encode)

Diamond will not escape variables. However they can be escaped with the escape function.

@:escape(model.message);

In 0.2.4 it'll be possible to escape variables / expressions using the syntax @$=model.message;

Expression (Unencoded)

Razor

<span>
  @Html.Raw(model.Message)
</span>

Diamond

<span>
  @=model.message;
</span>

Combining Text and markup

Razor

@foreach(var item in items) {
  <span>@item.Prop</span>
}

Diamond

@:foreach(item; items) {
  <span>@=item.prop;</span>
}

Mixin code and Plain text

Razor

@if (foo) {
  <text>Plain Text</text>
}

Diamond

@:if (foo) {
  <text>Plain Text</text>
}

Mixin code and plain text (alternate)

Razor

@if (foo) {
  @:Plain Text is @bar
}

Diamond

@:if (foo) {
  Plain Text is @@bar;
}

Email addresses

Razor

Diamond

Explicit Expression

Razor

<span>ISBN@(isbnNumber)</span>

Diamond

<span>ISBN@=isbnNumber;</span>

Escaping the @ sign

Razor

<span>In Razor, you use the @@foo to display the value of foo</span>

Diamond

Some cases might let you write @ and not @@

<span>In Diamond, you use the @@foo to display the value of foo</span>

Server side Comment

Razor

@*
  This is a server side
  multiline comment
*@

Diamond

@*
  This is a server side
  multiline comment
*

Calling generic method

Razor

@(MyClass.MyMethod<AType>())

Diamond

@:MyClass.myMethod!AType();

Creating a Razor Delegate

Razor

@{
  Func<dynamic, object> b =
    @<strong>@item</strong>;
}
@b("Bold this");

Diamond

*In Diamond it isn't necessary to create delegates. Diamond let's you integrate any D code and thus you can create normal functions

@:void b(T)(T item) {
    <strong>@=item;</strong>
}
@:b("Bold this");

Mixing expression and text

Razor

Hello @title. @name.

Diamond

Hello @=title;. @=name;.