Coder Social home page Coder Social logo

fhtml's Introduction

fhtml

github crates.io docs.rs build status

fhtml provides convenient macros to write formatted HTML in Rust with embedded expressions.

Components

Components can be written in a number of ways, but the common ways to create reusable components include:

  • Function components — A function component can be a simple function that accepts some arguments and returns the computed HTML. These are the most common types of components across web frameworks. For fhtml, function components are not always the best way to create a component, since unlike most JSX frameworks, function components do not get special treatment when used inside an fhtml macro.

  • Struct components — A struct that implements Display is arguably the most JSX-like way to use components, since you can specify fields or "props" in an arbitrary order, and use convenient traits like Default.

  • Macros — In Rust 1.71, flattening of nested format_args! was introduced, but this only works if macros are invoked, not functions nor methods, even if they are inlined. So for smaller components, using a macro that returns fhtml::format_args! is the most efficient kind of component, since they are usually zero-cost.

Nested formatting

You often need to do additional formatting inside your HTML, and you might be tempted to use the standard format! for that. However, this is not the most efficient way of doing additional formatting. Instead, format_args! should be used in most cases. The overhead of format_args! is typically zero-cost since nested format_args! calls are flattened by the compiler.

let puppy_kind = "Golden Retriever";
fhtml::format! {
    <img
        alt={format_args!("A happy {} playing", puppy_kind)}
        src="puppy.jpg"
    />
}
// Is equivalent to writing:
std::format!("<img alt=\"A happy {} playing\" src=\"puppy.jpg\">", puppy_kind)

Const formatting

There are often situations when you want to write some HTML without using any runtime values or variables. For this, you can use fhtml::concat!.

const MY_PAGE: &str = fhtml::concat! {
    <!DOCTYPE html>
    <head>
        <title>"My HTML Page"</title>
    </head>
    <body>
        <h1>"Welcome to my HTML page!"</h1>
        {include_str!("../my-page.html")}
    </body>
};

Escaping

Values are not escaped automatically. fhtml exports a simple escape function. For more complex escaping, html-escape may be sufficient.

License

Licensed under MIT license.

fhtml's People

Contributors

jonahlund avatar

Stargazers

swlkr avatar James Benner avatar  avatar Lê Tấn Trọng avatar Kyle L. Davis avatar Cr0a3 avatar

Watchers

 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.