Allows you to write methods that contain mixed HTML and C# in Razor Views (.cshtml) / Razor Components (.razor) files.
This is a primitive for composing Razor code that is very low overhead / low concept. Your code calls your code.
In Views (not components): Can be async, can use tag helpers. In Components (not views): Usage and semantics still need figuring out, but we want to build something like this.
<h1>Here is some info about people on the team</h1>
@foreach(var person in people)
{
DisplayPerson(person);
}
@functions {
void DisplayPerson(Person person)
{
@<div>
<h3>
@person.Name is @person.Age
</h3>
@if (DateTime.Now.DayOfYear == person.Birthday.DayOfYear)
{
<h4>Happy Birthday! @person.Name</h4>
}
</div>
}
Person[] people = new Person[]
{
new Person()
{
Name = "Ryan",
Age = 33,
Birthday = new DateTime(1985, 9, 9),
},
new Person()
{
Name = "Dan", // Not Dan Roth, some other guy.
Age = 53,
Birthday = new DateTime(1965, 2, 3),
},
};
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime BirthDay { get; set; }
}
}
Is this W A V Y ? SPICY? Have you had cases where you wanted this? What would you add/change/remove?
Heh! Yeah, this isn't an amazing example. I wanted to include some control flow.
The purpose is that you can refactor your logic into smaller methods if you think it needs it. It's possible to decompose Razor today, but the tools to use it are weakly typed and have high overhead relative to something like a method call.