Skip to content

Instantly share code, notes, and snippets.

@Mike-E-angelo
Last active June 12, 2019 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mike-E-angelo/22426be9defc71000f93cb68127785c3 to your computer and use it in GitHub Desktop.
Save Mike-E-angelo/22426be9defc71000f93cb68127785c3 to your computer and use it in GitHub Desktop.
Razor C# Example
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.RenderTree;
namespace Presentation.Elements
{
public abstract class ElementBase : ComponentBase
{
readonly string _name;
protected ElementBase(string name) => _name = name;
[Parameter]
public string StyleType { get; set; }
[Parameter]
public string InlineStyle { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, _name);
builder.AddAttribute(1, "class", StyleType);
builder.AddAttribute(2, "style", InlineStyle);
builder.AddContent(3, ChildContent);
builder.CloseElement();
}
}
public sealed class Paragraph : ElementBase
{
public Paragraph() : base("p") {}
}
public sealed class Container : ElementBase
{
public Container() : base("div") {}
}
}
@using Presentation.Elements
<Paragraph StyleType="myClass">Hello World!</Paragraph>
<!--
The above will emit:
<p class="myClass">Hello World!</p>
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment