Skip to content

DomCR/HtmlCSBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

May 29, 2022
f0f0461 · May 29, 2022

History

8 Commits
May 29, 2022
May 29, 2022
May 28, 2022
May 29, 2022
May 28, 2022
May 29, 2022

Repository files navigation

HtmlCSBuilder

Library to write HTML documents using .NET

Syntax Example

Example of basic html

<html>
    <body class="main_body">
        <h1>Heading</h1>
        <p>This is a paragraph</p>
    </body>
</html>

HtmlCSBuilder syntact to write the html sample:

HtmlBuilder builder = new HtmlBuilder();

using (builder.AddTag(HtmlTag.Html))
{
	using (builder.AddTag(HtmlTag.Body, (HtmlAttribute.Class, "main_body")))
	{
		using (builder.AddTag(HtmlTag.H1))
		{
			builder.WriteLine("Heading");
		}

		using (builder.AddTag(HtmlTag.P))
		{
			builder.WriteLine("This is a paragraph");
		}
	}
}

Write once use it everywhere, you can have an html splited in different HtmlBuilders and merge them in the different documents, for example you can setup a default header and footer and use the same bulider for all your pages:

<html>

    <!-- Head to use in all the pages -->
    <head>
        <title>
            This is the default title for all pages
        </title>
    </head>

    <!-- Dynamic body that changes between pages -->
    <body>
        Dynamic main content in this html page
    </body>

    <!-- Footer to use in all the pages -->
    <footer>
        <p>
            Author: this is the author of the pages
        </p>
    </footer>
</html>

C# example for how to create the static elements in the html:

HtmlBuilder page1 = new HtmlBuilder();

using (page1.AddTag(HtmlTag.Html))
{
	page1.AddChunk(header());

	using (page1.AddTag(HtmlTag.Body))
	{
		page1.WriteLine("Dynamic main content in this html page 001");
	}

	page1.AddChunk(footer());
}

Where header() and footer() implementation looks like:

private static HtmlBuilder header()
{
	HtmlBuilder head = new HtmlBuilder();

	using (head.AddTag(HtmlTag.Head))
	{
		using (head.AddTag(HtmlTag.Title))
		{
			head.WriteLine("This is the default title for all pages");
		}
	}

	return head;
}

private static HtmlBuilder footer()
{
	HtmlBuilder foot = new HtmlBuilder();

	using (foot.AddTag(HtmlTag.Footer))
	{
		using (foot.AddTag(HtmlTag.P))
		{
			foot.WriteLine("Author: this is the author of the pages");
		}
	}

	return foot;
}