post.thumbnail.alt

What is Razor Templating Language?

10th March 2021

Filed Under:

  • Notes to my Junior

Photo by NASA on Unsplash

Razor is the dotnet templating language, Razor is the syntax used to create views in dotnet applications. It will normally be found in `.cshtml` files. You can think of it in a similar way to JSX, in that Razor is to C#, what JSX is to Javascript in a React application.

Templating languages don’t transform data, but arrange it for display in a view.

Example displaying a list in JSX:

const list = (items) => {
  return (
    <ul>
      {items.map((item, index) => (
        <li key={item}>{item}</li>
      ))}
    </ul>
  );
}

Displaying the same list in Razor

<ul>
@foreach(var item in items) {
  <li>@item</li>
}
</ul>

Further Reading

A great cheatsheet for copy + paste
https://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ 

A good abstract overview of templating engines as a whole:

https://thomaslevine.com/computing/templating-languages/

The Microsoft ASP.net Razor Guide

https://docs.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-razor-syntax-c

Comments

Add your comment