> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-cms/13.latest/fundamentals/design/templates/basic-razor-syntax.md).

# Basic Razor Syntax

How to perform common logical tasks in Razor like if/else, foreach loops, switch statements and using the @ character to separate code and markup

*Shows how to perform common logical tasks in Razor like if/else, foreach loops, switch statements and using the @ character to separate code and markup.*

## The @ symbol

The @ symbol is used in Razor to initiate code, and tell the compiler where to start interpreting code, instead of returning the content of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.

```csharp
@* Writing a value inside a html element *@

<p>@Model.Name</p>

@* Inside an attribute *@
<a href="@Model.Url()">@Model.Name</a>

@* Using it to start logical structures *@
@if (selection?.Length > 0)
{
    <ul>
        @foreach (var item in selection)
        {
            <li>
                <a href="@item.Url(PublishedUrlProvider)">@item.Name</a>
            </li>
        }
    </ul>
}
```

## Embedding comments in razor

Commenting your code is important, use comments to explain what the code does. `@* *@` indicates a comment, which will not be visible in the rendered output.

```csharp
@* Here we check if the name is equal to foobar *@
@if (Model.Name == "foobar")
{
    @foreach (var child in Model.Children)
    {
        @* here we write stuff for each child page *@
        <p>write stuff</p>
    }
}
```

## If/else

If/else statements perform one task if a condition is true, and another if the condition is not true

```csharp
@if (Model.Name == "home")
{
    <p>This is the homepage!</p>
}

@if (Model.NodeTypeAlias == "TextPage")
{
    <p>This is a textpage</p>
}
else
{
    <p>This is NOT a textpage</p>
}
```

## Foreach loops

A foreach loop goes through a collection of items, typically a collection of pages and performs an action for each item

```csharp
@foreach (var item in Model.Children)
{
    <p>The item name is: @Item.Name</p>
}
```

## Switch block

A Switch block is used when testing a large number of conditions

```csharp
@switch (Model.WeekDay)
{
    case "Monday":
        "<p>It is Monday</p>";
        break;
    case "Tuesday":
        "<p>It is Tuesday</p>";
        break;
    case "Wednesday":
        "<p>It is Wednesday</p>";
        break;
    default:
        "<p>It's some day of the week</p>";
        break;
}
```

### More information

* [More examples](/umbraco-cms/13.latest/reference/templating/mvc/examples.md)
* [Querying](/umbraco-cms/13.latest/reference/querying.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/umbraco-cms/13.latest/fundamentals/design/templates/basic-razor-syntax.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
