This is a very easy to use client side + server template. When we create some web applications, we must use ajax to get some data and use the callback function to solve this problem. Therefore, we must display this data on the client side.
The question is how to make them client side?
Now we just need the client side of jade.js.
Follow this document: https://github.com/visionmedia/jade#readme
First
git clone https:
Second
$ make jade.js ( in fact the project has already compile the file for us ) so we just need to copy this file to the path that we use.
Third
read my demo below : <script type='text/javascript' language='javascript' src="lib/jquery-1.8.2.min.js"></script> <script type='text/javascript' language='javascript' src="lib/jade/jade.js"></script> <script type='template' id='test'> ul li hello world li #{item} li #{item} li #{item} </script> <script> var compileText = $("#test").text(); console.log( typeof( compileText ) ); var fn = jade.compile( compileText , { layout : false } ); var out = fn( {item : "this is item " } ); console.log( out ); $("body").append( out ); </script>
Now you can see the output result in the body
hello world this is item this is item this is item
After reading this demo, I think you will know how to separate the server side and the client side on the jade side. If you can figure out which one to compile the jade pattern, then all the questions are easy.
Perhaps you would have another question. How to write some jade template codes in * .jade? The document will also provide us with a way to do this. This tutorial can help you.
index.jade
!!!5 html head title hello world body ul
index.js
var compileText = $('#list-template').text(); var compile = jade.compile( compileText , { layout : false } ); var data = [{ "name" : "Ben" } , {"name" : "Jack" } , {"name" : "Rose" }]; var outputText = compile( data ); $("#list").append( outputText );
source share