Skip to content

Instantly share code, notes, and snippets.

@justrhysism
Last active January 17, 2019 15:32
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 justrhysism/1cbba77b23636d5280fefef786ae901d to your computer and use it in GitHub Desktop.
Save justrhysism/1cbba77b23636d5280fefef786ae901d to your computer and use it in GitHub Desktop.
C# Razor HtmlHelper Extension for Conditional Wrapping Element Tag

C# Razor HtmlHelper Extension for Conditional Wrapping Element Tag

Usage

Conditionally render wrapping tag

Razor

@using UI.Extensions

@{
	var someCondition = true;
}

@using (Html.Wrap("div", someCondition)) {
	<span>This span is wrapped with a `&lt;div&gt;` if `someCondition` is `true`.</span>
}

Output

<div>
	<span>This span is wrapped with a `&lt;div&gt;` if `someCondition` is `true`.</span>
</div>

Conditionally set wrapping element tag

Razor

@using UI.Extensions

@{
	var someCondition = true;
	var tag = someCondition ? "a" : "span";
	var attributes = someCondition ? new { href = "#" } : new {};
}

@using (Html.Wrap("div", attributes, someCondition)) {
	<span>This span is wrapped with a `&lt;a href="#"&gt;` if `someCondition` is `true`.</span>
}

Output

<a href="#">
	<span>This span is wrapped with a `&lt;a href="#"&gt;` if `someCondition` is `true`.</span>
</a>
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace UI.Extensions
{
public static class HtmlHelperExtensions
{
public static HtmlWrap Wrap(this HtmlHelper htmlHelper, string tag)
{
return htmlHelper.Wrap(tag, true);
}
public static HtmlWrap Wrap(this HtmlHelper htmlHelper, string tag, bool condition)
{
return htmlHelper.Wrap(tag, new RouteValueDictionary(), condition);
}
public static HtmlWrap Wrap(this HtmlHelper htmlHelper, string tag, object htmlAttributes)
{
return htmlHelper.Wrap(tag, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), true);
}
public static HtmlWrap Wrap(this HtmlHelper htmlHelper, string tag, IDictionary<string, object> htmlAttributes)
{
return htmlHelper.Wrap(tag, htmlAttributes, true);
}
public static HtmlWrap Wrap(this HtmlHelper htmlHelper, string tag, object htmlAttributes, bool condition)
{
return htmlHelper.Wrap(tag, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), condition);
}
public static HtmlWrap Wrap(this HtmlHelper htmlHelper, string tag, IDictionary<string, object> htmlAttributes, bool condition)
{
var htmlWrap = new HtmlWrap(htmlHelper.ViewContext, tag, htmlAttributes, condition);
htmlWrap.WriteStart();
return htmlWrap;
}
}
}
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace UI.Extensions
{
public class HtmlWrap : IDisposable
{
private readonly ViewContext _viewContext;
private readonly TagBuilder _tagBuilder;
private readonly bool _condition;
private bool _disposed;
public HtmlWrap(ViewContext viewContext, string tag, IDictionary<string, object> htmlAttributes, bool condition)
{
_viewContext = viewContext;
_tagBuilder = new TagBuilder(tag);
_tagBuilder.MergeAttributes(htmlAttributes);
_condition = condition;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
_disposed = true;
WriteEnd();
}
public void WriteStart()
{
if (!_condition) return;
_viewContext.Writer.Write(_tagBuilder.ToString(TagRenderMode.StartTag));
_viewContext.Writer.Write(Environment.NewLine);
}
public void WriteEnd()
{
if (!_condition) return;
_viewContext.Writer.Write(_tagBuilder.ToString(TagRenderMode.EndTag));
}
}
}
@larrybud2004
Copy link

Is there any way to get the content which is inside the { } from within the HtmlWrap methods?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment