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?
I like @iamtheoneaboveall 's suggestion, it's more consistent when the display value is a more complicated expression.
Also, is the code only meant to activate on their day of birth, or also every subsequent annual celebration thereof? My experience with newborn babies is that that they tend to be a bit too out of sorts to visit websites on their first day out.