Last active
June 12, 2019 09:57
-
-
Save Mike-E-angelo/22426be9defc71000f93cb68127785c3 to your computer and use it in GitHub Desktop.
Razor C# Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") {} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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