Good Javascript template engine to work with JSON

前端 未结 8 1137
青春惊慌失措
青春惊慌失措 2020-12-06 08:54

I have looked at jTemplates and it\'s worth a try. Are there any other template engines other than jTemplates?

相关标签:
8条回答
  • 2020-12-06 09:27

    You can use this one: https://jocapc.github.io/jquery-view-engine/

    It binds properties of JSON object into empty HTML template and match properties with elements by name, id, or class.

    First, you would need to have plain HTML template in your page:

    <div id="template">
        <h1 id="Name"></h1>
        <label>Description:</label>
        <textarea name="Desc"></textarea>
        <ul>
            <li class="bind-Tags"></li>
        </ul>
    </div>
    

    Then you need JS object that will be placed in template:

    var data = {   Name: "JOVN",
        Desc: "The simplest view engine",
        Tags: ["View engine", "JavaScript", "SPA"]
    }
    

    Finally just fill the view with the data object:

    $("div#template").view(data);
    

    Result is:

    <div id="template">
        <h1 id="Name">JOVN</h1>
        <label>Description:</label>
        <textarea name="Desc">The simplest view engine</textarea>
        <ul>
            <li class="bind-Tags">View engine</li>
            <li class="bind-Tags">JavaScript</li>
            <li class="bind-Tags">SPA</li>
        </ul>
    </div>
    

    View engine will populate single fields or replicate array elements in template.

    0 讨论(0)
  • 2020-12-06 09:32

    I liked the approach the JavaScriptMVC Frameworks Views take, especially because it uses JavaScript itself as the templating language. The framework is now based on jQuery and you can render your Model right into the views (Model supports JSON, JSONP, XML etc.).

    0 讨论(0)
提交回复
热议问题