Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Last active November 1, 2017 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfowl/dcf89afe8d3dd40399e28979c77a5329 to your computer and use it in GitHub Desktop.
Save davidfowl/dcf89afe8d3dd40399e28979c77a5329 to your computer and use it in GitHub Desktop.
Also @template and @taghelper for declarative templates and tag helpers in razor files. (inside viewimports)
@model MyViewModel
<grid data-source="Customers">
<column model="FirstName" />
<column model="LastName" />
<column model='@FirstName + " " + LastName' />
<item-template>
</item-template>
</grid>
@model MyViewModel
<table>
<thead>
<th asp-display-name-for="@Customers.FirstName"></th>
<th asp-display-name-for="@Customers.LastName"></th>
<th>Full Name</th>
</thead>
<tbody>
<tr asp-repeat="Customers">
<td>@item.FirstName</td>
<td>@item.LastName</td>
<td>@(item.FirstName + " " + item.LastName)</td>
</tr>
</tbody>
</table>
@Html.RepeatFor(m.Customers,
@<tr>
<td>@item.FirstName</td>
<td>@item.LastName</td>
<td>@(item.FirstName + " " + item.LastName)</td>
</tr>
)
public static IHtmlContent RepeatFor<TItem>(IEnumerable<TItem> items, Func<TItem, IHtmlContent> renderItem)
{
var builder = new HtmlContentBuilder();
foreach (var item in items)
{
builder.AppendHtml(renderItem(item));
}
return builder;
}
<foo blah="SomeBlah">
<tr asp-repeat="Model.Customers" my-prop="Something">
<td>@item.FirstName</td>
<td>@item.LastName</td>
<td>@(item.FirstName + " " + item.LastName)</td>
</tr>
[HtmlTargetElement("*", Attributes = "asp-repeat")]
[ContentContextParameterName("item")]
public class RepeatTagHelper<TContext> : TagHelper<TContext>
{
public IEnumerable<TItem> Items { get; set; }
public override async Task ProcessAsync(TagHelperContext<TContext> context, TagHelperOutput output)
{
foreach (var item in Items)
{
output.AppendHtml(await context.GetChildContentAsync(item));
}
output.TagName = null;
}
}
<table>
<thead>
<th>@Html.DisplayNameFor(m => m.FirstName)</th>
<th>@Html.DisplayNameFor(m => m.LastName)</th>
<th>Full Name</th>
</thead>
<tbody>
foreach (var customer in Model.Customers)
{
<tr>
<td>@Model.FirstName</td>
<td>@Model.LastName</td>
<td>@(FirstName + " " + LastName)</td>
</tr>
}
</tbody>
</table>
<ul data-source="Categories">
<li>@Model.Name</li>
</ul>
public class CustomerViewModel
{
public IList<Customer> Customers { get; set; }
}
[RestrictChildren("column"]
public class GridTagHelper : TagHelper
{
public IEnumerable<GridColumnTagHelper> Columns { get; set; }
public ModelExpression DataSource { get; set; }
[Unbound]
public GridContext GridContext { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
// Force processing of children now
await output.GetChildContentAsync();
// Build the grid using the declartions from the children
}
}
[HtmlTargetElement("column")]
public class GridColumnTagHelper : ChildTagHelper<GridTagHelper>
{
public ModelExpression Field { get; set; }
public string Format { get; set; }
public bool Enabled { get; set; }
[NotBound]
public HtmlTemplate Template { get; set; }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
Template = HtmlTemplate.Create(await output.GetChildContentAsync());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment