Coder Social home page Coder Social logo

mevrael / html-template Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 9.0 7 KB

The WIP implementation of the HTML5 <template> v2 with parse() support and part of WHATWG/HTML proposal #2254 https://github.com/whatwg/html/issues/2254

License: MIT License

HTML 23.31% JavaScript 76.69%

html-template's Introduction

html-template

The WIP implementation of the HTML5 <template> v2 with parse() support and part of WHATWG/HTML proposal #2254

For tests open index.html and use the test() function in DevTools. Tests, data and templates defined at the bottom of index.html.

template.js contains the implementation of the .parse(data) method on the Template prototype.

Use-cases:

Global:

  1. parse() should return a single Node. outerHTML can be used to check the result string.
  2. Only first child element in DocumentFragment (template) is used as a template (root node). If no root node found, exception thrown.
  3. Current algorithm transforms first child element to an HTML string (outerHTML) and parses template as a single string
  4. It finds all the expressions
  5. If data was not found, expression should be replaced with an empty string
  6. It should trim() spaces, i.e. both {{var}} and {{ var }} should be valid

A:

<template id="1">
  <div class="box">
    <h1>{{ title }}</h1>
    <p>{{ message }}</p>
  </div>
</template>
document.getElementById('1').parse({title: 'Hello, World', message: 'Lorem Ipsum'});
  1. It should replace {{ title }} with Hello, World and {{ message }} with Lorem Ipsum.
  2. If there are no any argument specified in data (key is undefined) then according to Global 5 it should be replaced with an empty string

B:

<template id="2">
  <box>
    <h1>{{ title }}</h1>
    <p>{{ message }}</p>
    <p>{{ title }}</p>
  </box>
</template>

Same data set.

It should replace all occurrences of {{ title }}.

C:

It should replace nested vars

<template id="3">
  <div class="box">
    <h1>{{ title }}</h1>
    <p>{{ message }}</p>
    <div>{{ author.name }}</div>
  </div>
</template>

D:

Currently block statements (if/endif) in progress:

<template id="4">
  <div class="box">
    <h1>{{ title }}</h1>
    <p>{{ message }}</p>
    {{ if (author) }}
    <div>{{ author.name }}</div>
    {{ endif }}
  </div>
</template>

It should parse if block and do not display it if expression is false

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.