Skip to content

Instantly share code, notes, and snippets.

@Mike-E-angelo

Mike-E-angelo/CSharpish.cs

Last active Jun 12, 2019
Embed
What would you like to do?
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