JavaScript Is it possible to insert new HTML with pure.js

I am searching for something which renders an HTML template starting from a JSON file of data.

The matter is that the plugin/framework/library I'm searching for must create itself the <html> template structure, starting from something very simple.

For example I have an simple html like this:

<ul>
  <li><li>
</ul>

and a json like this:

{
   "mylist":{
      "listone": 
          {"img" : "/img/pippo.gif" ,
           "text1" : "pluto",
           "text2" : "topolino",
           "link" : "http://www.sito.it"        
           },
      "listtwo":
          {"img" : "/img/pippo.gif" ,
           "text1" : "pluto",
           "text2" : "topolino",
           "link" : "http://www.sito.it"        
          }
   }
}

and I want the data to render in my document like this:

<ul>
  <li>
    <img src="/img/pippo.gif" />
    <h1>pluto</h1>
    <p><a target='_blank' href="http:://www.sito.it>topolino</a></p>
  </li>
</ul>

If I head already the entire structure I could use pure.js as usual, but, since I don't have the inner tags in the li, can I inject the HTML code with the pure.js directives? Or is it possible only with JsRender or similar?

Answer:1

Pure JS allows you to use JavaScript function with directives. Whatever is returned from that function, will be used as a value for a directive.

The argument of the function is an object with the following properties:

  • context : This is the full JSON that was passed for transformation
  • item* : the current loop item
  • items* : all the items of the loop
  • pos* : the current position in the loop. An integer when iterating an array, a property name iterating on a collection

The following example shows how to do it.

var data = {
  "img": "/img/pippo.gif",
    "text1": "pluto",
    "text2": "topolino",
    "link": "http://www.sito.it"
}

var directive = {
  'li': function (arg) {
    return "<img src=\""+arg.context.img+"\" /><h1>"
      +arg.context.text1+"</h1><p><a target='_blank' href=\""
      +arg.context.link+"\">"+arg.context.text2+"</a></p>"
  }
}

$('ul').render(data, directive);

The given HTML:

<ul><li></li></ul>

Will become as following one (after rendering):

<ul>
  <li>
    <img src="/img/pippo.gif">
    <h1>pluto</h1>
    <p>
      <a target='_blank' href="http://www.sito.it">topolino</a>
    </p>
  </li>
</ul>

I hope that will help.

Answer:2

This is more advise than answer. I would recommend against what you are trying to accomplish. The powerful concept used for templating is to separate the HTML from the code. When you keep this separation and you can figure out how to write code and understand code that follows this separation. Then code will be easier to write and to understand, not only by you but by others that follow that same principle.

In your example your HTML template should be like this:

<ul>
  <li>
    <img src="/img/pappo.gif" />
    <h1>marte</h1>
    <p><a target='_blank' href="http:://www.sito.it>guille</a></p>
  </li>
</ul>

and a json like this:

{
   "mylist":{
      "listone": 
          {"img" : "/img/pippo1.gif" ,
           "text1" : "pluto1",
           "text2" : "topolino1",
           "link" : "http://www.sito1.it"       
           },
      "listtwo":
          {"img" : "/img/pippo2.gif" ,
           "text1" : "pluto2",
           "text2" : "topolino2",
           "link" : "http://www.sito2.it"       
          }
   }
}

and the final should look like this:

<ul>
  <li>
    <img src="/img/pippo1.gif" />
    <h1>pluto1</h1>
    <p><a target='_blank' href="http:://www.sito1.it>topolino1</a></p>
  </li>
  <li>
    <img src="/img/pippo2.gif" />
    <h1>pluto2</h1>
    <p><a target='_blank' href="http:://www.sito2.it>topolino2</a></p>
  </li>
</ul>

The easiest way I can explain how to accomplish this is to make your JSON data structure follow your HTML almost one to one. The huge difference is that once that is done the template will be easy to write, and programming the JSON transformation will eventually be easier than writing the HTML transformation. Plus your HTML templates will be 100% render-able by a browser and a 100% modifiable by non programmers. The key here is that although your final product is HTML, taking the JSON object route will produce better code. It won't remove the actual problem of creating the JSON, and you will need to adjust, learn and find tools to do that.

Answer:3

i am replacing the img src attribute using javascript my code is <div class="reader" id="reader1"></div> <img src="images/img_001.jpg" alt="image" id="image1"/> </div&...

i am replacing the img src attribute using javascript my code is <div class="reader" id="reader1"></div> <img src="images/img_001.jpg" alt="image" id="image1"/> </div&...

I have been working on making a form that uploads an image through ajax with php doing the upload. For some background I am having an issue where the xhr.readyState and .Status seem to run and upload....

I have been working on making a form that uploads an image through ajax with php doing the upload. For some background I am having an issue where the xhr.readyState and .Status seem to run and upload....

  1. ajax javascript with php
  2. ajax with javascript
  3. ajax with javascript example
  4. javascript ajax with parameters
  5. javascript ajax with headers
  6. ajax with javascript tutorial
  7. javascript ajax with promise
  8. javascript ajax with callback
  9. javascript ajax with json
  10. ajax with javascript or jquery
  11. javascript ajax with data
  12. javascript ajax with post
  13. javascript ajax with cookies
  14. javascript ajax with timeout
  15. ajax with javascript function
  16. javascript ajax with laravel
  17. javascript ajax with jsonp
  18. ajax with javascript html
  19. ajax javascript to php variable
  20. ajax javascript to python

This code works perfect for crome and firefox.. but error in IE8 if($('#soundcheck').is(':checked')){ //alert('checked'); var audio = document.createElement('...

This code works perfect for crome and firefox.. but error in IE8 if($('#soundcheck').is(':checked')){ //alert('checked'); var audio = document.createElement('...

  1. jquery play notification sound

Possible Duplicate: Javascript outer scope variable access I have a javascript module that looks something like below. The main issue I'm having is how to access variables in the "this" scope ...

Possible Duplicate: Javascript outer scope variable access I have a javascript module that looks something like below. The main issue I'm having is how to access variables in the "this" scope ...

  1. javascript private inner class