Last active
December 21, 2015 12:08
-
-
Save devuxer/6303712 to your computer and use it in GitHub Desktop.
I was curious if I could build a C# "tag builder" that comes close to the succinctness of the Parrot view engine (https://github.com/ParrotFx/Parrot). This is purely hypothetical at the moment (i.e., HtmlWizard does not really exist), but I believe something very close to this would be possible. Do you like the syntax? Do you think it would be e…
This file contains hidden or 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
public HtmlWizard GetWelcomeView | |
{ | |
var model = new { Features = new[] { "Easy to type", "Easy to read", "Easy to maintain" } }; | |
return new HtmlWizard((e /* element */, a /* attribute */, ca /* custom attribute */) => | |
{ | |
e.Html( | |
e.Head( | |
e.Title("Check out ", e.Strong("HTML Wizard"), "!")), | |
e.Body( | |
e.H1(a.Id("header"), ca("custom1", "value1"), ca("custom2", "value2"), "HTML Wizard"), | |
e.P(a.Id("tagname"), a.Class("small"), "All new HTML Tag Generator"), | |
e.P("Just go to ", e.A(a.Href("http://github.com"), "GitHub"), " and search for Wizard."), | |
e.UL( | |
model.Features.Select(f => e.LI(f))))) | |
}); | |
} |
This file contains hidden or 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
<html> | |
<head> | |
<title>Check out <strong>HTML Wizard"</strong>!</title> | |
</head> | |
<body> | |
<h1 id="header" custom1="value1" custom2="value2">HTML Wizard</h1> | |
<p id="tagname" class="small")>All new HTML Tag Generator</p> | |
<p>Just go to <a href="http://github.com">GitHub</a> and search for Wizard.</p> | |
<ul> | |
<li>Easy to type</li> | |
<li>Easy to read</li> | |
<li>Easy to maintain</li> | |
</ul> | |
</body> | |
</html> |
That's a good point, but if I use dynamics, that would mean no intellisense, which is something I was hoping to keep. Instead, how about a different method for adding custom attributes?
h.H1(h.Set(id: "header"), h.Let("custom1", "value1", "custom2", "value2"), "HTML Wizard")
(I also revised the code in the Gist with this example.)
Or perhaps a separate h.Let() for each attribute (because I'm not sure sure about the alternating name, value, name, value thing).
And here's another idea, sort of a hybrid:
h.H1(h.Set(id: "header", other: new { custom1 = "value1", custom2 = "value2"}), "HTML Wizard")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
h.H1(h.Set(new { id = "header" }), "HTML Wizard"),
not so pretty right there but still simple enough...one of the things you'd probably want is to use dynamics since you can define elements outside of the specs.