Code Monkey home page Code Monkey logo

conditional-tagged-templates's Introduction

Experimental conditional tagged templates for generating HTML strings

I had the idea that something like this:

function renderLocalHTML(text: string, secondary: string) {
  return html`<html>
    <head>
      <title></title>
      <meta charset="UTF-8" />
    </head>
    <body>
      <p>Test text</p>
      ${IF(secondary)}
      <p>${trusted(secondary)}</p>
      ${ELSEIF(text)}
      <p>${text}</p>
      ${ELSE()}
      <p>Otherwise</p>
      ${END()}
      <svg xmlns="http://www.w3.org/2000/svg">
        <rect
          stroke="black"
          fill="blue"
          x="45px"
          y="45px"
          width="200px"
          height="100px"
          stroke-width="2"
        />
      </svg>
    </body>
  </html>`;
}

Would be more readable than:

function renderLocalHTML(text: string, secondary: string) {
  return html`<html>
    <head>
      <title></title>
      <meta charset="UTF-8" />
    </head>
    <body>
      <p>Test text</p>
      ${secondary
        ? html`<p>${trusted(secondary)}</p>`
        : text
        ? html`<p>${text}</p>`
        : html`<p>Otherwise</p>`}
      <svg xmlns="http://www.w3.org/2000/svg">
        <rect
          stroke="black"
          fill="blue"
          x="45px"
          y="45px"
          width="200px"
          height="100px"
          stroke-width="2"
        />
      </svg>
    </body>
  </html>`;
}
  • IF, ELSEIF, ELSE, and END are used to set up conditional blocks
  • trusted is used to inject safe HTML strings that don't need escaping
  • html otherwise auto-escapes strings using the excellent html-sloppy-escaper
  • deno test runs the tests
  • deno task bench runs the benchmark

It's an experiment, not a project, so don't go expecting this to be published and maintained in any sort of way. I did include tests with full coverage and a naive benchmarking suite comparing it with other tagged templates and JSX approaches. It's roughly as fast as other tagged template and string template approaches and 4-40x faster than server-side JSX libraries.

Conditional tagged templates:

  • 1.13x slower than Eta template
  • 1.07x slower than Tagged templates uhtml sync
  • 1.09x faster than Tagged templates basic html sync
  • 4.94x faster than JSX direct to string sync
  • 17.71x faster than Preact async
  • 42.98x faster than JSX indirect async

If you like this, feel free to copy it into your own projects (remember to include the tests) and reuse.

conditional-tagged-templates's People

Contributors

baldurbjarnason avatar

Stargazers

 avatar  avatar  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.