GithubHelp home page GithubHelp logo

sendit.htmlbuilder's Introduction

SendIt.HtmlBuilder

Build HTML code with C# using fluent methods.

Build Status Coverage

Download from NuGet.

NuGet NuGet

How to use

Define HTML tags as C# objects

var html = new Html(new Head(), new Body());

var h4 = new H4("This is a level 4 heading.");
var p = new P("This is a paragraph.");
var img = new Img("https://picsum.photos/400/300");

var table = new Table();
var tr1 = new TR();
var td1 = new TD("Cell 1");
var td2 = new TD("Cell 2");
var tr2 = new TR();
var td3 = new TD("Cell 3");
var td4 = new TD("Cell 4");

Create HTML DOM

tr1.AppendChild(td1);
tr1.AppendChild(td2);
tr2.AppendChild(td3);
tr2.AppendChild(td4);

table.AppendChild(tr1);
table.AppendChild(tr2);

body.AppendChild(h4);
body.AppendChild(p);
body.AppendChild(img);
body.AppendChild(table);

var html = new Html(new Head(), body);

Generate HTML string

var htmlString = html.ToHtml();
Console.WriteLine(htmlString);

Output (formatted manually for readability):

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <h4>This is a level 4 heading.</h4>
        <p>This is a paragraph.</p>
        <img alt="" src="https://picsum.photos/400/300">
        <table>
            <tr>
                <td>Cell 1</td>
                <td>Cell 2</td>
            </tr>
            <tr>
                <td>Cell 3</td>
                <td>Cell 4</td>
            </tr>
        </table>
    </body>
</html>

Use fluent methods

var htmlFluent = new Html(
    new Head(),
    new Body()
        .AppendChild(new H4("This is a level 4 heading."))
        .AppendChild(new P("This is a paragraph."))
        .AppendChild(new Img("https://picsum.photos/400/300"))
        .AppendChild(new Table()
            .AppendChild(new TR()
                .AppendChild(new TD("Cell 1"))
                .AppendChild(new TD("Cell 2")))
            .AppendChild(new TR()
                .AppendChild(new TD("Cell 3"))
                .AppendChild(new TD("Cell 4")))) as Body);

Define attributes and styling

var h3 = new H3("This is a level 3 heading and it is blue")
{
    Id = "Heading3",
    Style = new Style()
        .Add(StyleProperty.Color, "blue")
        .Add(StyleProperty.FontFamily, "Arial")
        .Add(StyleProperty.FontSize, "0.9rem")
};

h3.Style[StyleProperty.FontWeight] = "600";

var h3String = h3.ToHtml();
Console.WriteLine(h3String);

Output:

<h3 id="Heading3" style="color: blue; font-family: Arial; font-size: 0.9rem; font-weight: 600;">This is a level 3 heading and it is blue</h3>

Parse HTML

var htmlStringToParse = "<div><h3>Hello World!</h3><p>This HTML code can be parsed by HtmlParser.</p><div>";

var htmlParsed = HtmlParser.Parse(htmlStringToParse);

sendit.htmlbuilder's People

Contributors

filipliwinski avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.