Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:56:39+00:00 2026-06-15T14:56:39+00:00

There are several libs for JavaScript templates (Mustache, underscore templates). What they do is

  • 0

There are several libs for JavaScript templates (Mustache, underscore templates). What they do is to create new DOM nodes.

In my app I am trying to reuse DOM nodes to decrease memory consumption. An example is a thumbnail gallery with pagination. I reuse the same 50 nodes when i load the 50 next thumbnails.

I could easily render new nodes with a template lib, but is there some template libs that instead of creating new nodes, can reuse or update existing ones?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:56:40+00:00Added an answer on June 15, 2026 at 2:56 pm

    My solution is somehow like adeneo’s but a little more generic. I use this in my projects, it uses only native DOM API, keep js and html totally separated.

    <ul class="list">
      <li class="item">
        <label><span class="name">Field:</span><input name="" type="" value="" /></label>
      </li>
    </ul>
    

    Note that, white spaces should be removed when send to client.

    First generate a template based on the delivered HTML, it takes a root element, and some binding points as parameters.

    var ul = document.querySelector('.list');
    var tpl = new Tmpl(ul.querySelector('li'), [['.name', 'input@name'], 'input@type', 'input@value']);
    

    Then use data to generate <li>s.

    var data = [['test1', 'text', 'test value'], ['test2', 'checkbox', 'true']]
    for (var i = 0; i < data.length; i++)
      ul.appendChild(tpl.generate(data[i]));
    

    When need to update one instance, you can do it like:

    tpl.apply(ul.querySelector('li'), ['test3', 'button', 'it\'s a button now']);
    

    Now the list is generated.

    The Tmpl class (minimized, removed other features, and works without any dependency) is pasted below:

    function Tmpl(node, targets, singleton) {
      this.elem = node;
      this.begins = [];
      this.targets = [];
      this.fields = [];
      this.mapping = [];
      for (var i = 0; i < targets.length; i++) {
        var tmp = targets[i] instanceof Array ? targets[i] : [targets[i]];
        this.begins.push(this.targets.length);
        for (var j = 0; j < tmp.length; j++) {
          var field = this.parse(node, tmp[j]);
          if (field) {
            this.targets.push(tmp[j]);
            this.fields.push(field);
            this.mapping.push(i);
          }
        }
      }
      this.begins.push(this.targets.length);
      node.parentNode.removeChild(node);
      return this;
    }
    Tmpl.prototype = {
      generate: function(data) {
        for (var i = 0; i < this.fields.length; i++)
          this.fields[i].nodeValue = data[this.mapping[i]];
        return this.elem.cloneNode(true);
      },
      apply: function(node, data) {
        for (var i = 0; i < this.fields.length; i++)
          this.parse(node, this.targets[i]).nodeValue = data[this.mapping[i]];
        return node;
      },
      applySingle: function(node, index, datum) {
        for (var i = this.begins[index]; i < this.begins[index+1]; i++)
          this.parse(node, this.targets[i]).nodeValue = datum;
        return node;
      },
      parse: function(that, selector) {
        selector = selector.split('@');
        var node = selector[0] == '.' ? that : that.querySelector(selector[0]);
        if (selector[1]) {
          var attr = node.getAttributeNode(selector[1]);
          if (!attr) {
            attr = document.createAttribute(selector[1]);
            node.setAttributeNode(attr);
          }
          node = attr;
        }
        if (node instanceof HTMLElement && node.nodeType != 2 && node.nodeType != 3)
          node = node.firstChild && node.firstChild.nodeType == 3 ?
            node.firstChild : node.appendChild(document.createTextNode(''));
        return node;
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are several PHP or js code formatting libs out there -- does anyone
There are several good Javascript editors for Markdown / Textile (e.g.: http://attacklab.net/showdown/ , the
There are several threads in my application that work in the background. They connect
I have encountered the NoClassDefFoundError when trying to create a new Intent to start
there are several function will have to get the dom $$('.box')[0] , I don't
There are several threads on this here at SO but I didn't find one
There are several other questions about this topic that I have gone through, but
There are several variant questions on this on SO, but I did not find
There are several places on the internet that talk about having multy key dictionary
There are several ways google throws at me for checking if a file is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.