Home >Web Front-end >JS Tutorial >JS template implementation method_javascript skills
When we use JS to render DOM, we usually use strings to create DOM and then attach it to the parent element. If the attached DOM is dynamic and volatile, a lot of logic needs to be written in the function. If it is during the control implementation process, the problems this brings are more serious.
A common solution to this problem is to use templates and pass them into controls as configuration items to separate data and rendering. The specific implementation methods are as follows:
String replacement is the simplest way to implement templates. Take a look at the specific implementation:
1. Define replacement function
* Replace fields in string.
* @param {String} str template string
* @param {Object} o json data
* @param {RegExp} [regexp] Regular expression matching string
*/
function substitute(str,o,regexp){
return str.replace(regexp || /\?{([^{}] )}/g, function (match, name) {
return (o[name] === undefined) ? '' : o[name];
});
}
2. Use configuration items:
data : {value : '123',text:'abc'},
template : ''
};
3. In the process of creating DOM, we call it like this:
$(str).appendTo('body');
Through the above example, we have completed the decoupling of data and strings, which can be flexibly used in controls. Most current JS frameworks provide templates in this way.
On this basis, the following extensions can be made. If you are interested, you can implement them yourself:
1. Use numbers instead of parameter names:
Such as ''
2. Nested use of object attributes:
Such as ''
Advantages: Simple to implement and easy to understand.
Disadvantages: It can only perform simple data structures and cannot handle loops and conditional statements.
We can handle very complex logic in the rendering function and pass the rendering function as a parameter into the configuration item.
Configuration items:
data : [{value : '0',text:'abc'},{value : '1',text:'bcd'}],
renderer : function(obj){
if(obj.value === '0'){
return obj.text;
}else{
return '';
}
}
};
When in use:
var obj = data[i],
str = config.renderer(obj);
$(str).appendTo('body');
}
This is a great solution when dealing with loops, conditional statements.
Advantages: Relatively simple implementation, flexible implementation, able to meet complex data structures, easy to debug
Disadvantages:
Every JS UI library will have a powerful template engine. A template engine needs to implement the following functions:
1. String replacement
2. Process complex statements, conditions, loops
3. Use built-in functions
4. Allow users to pass in custom functions
There are currently two common implementation methods of template engines:
1. Use regular expressions to analyze the string, execute the special statement logic, and replace the corresponding data
Let’s take a look at an example of the KISSY template:
'Hello, {{#each users}}{{#if _ks_value.show}}{{_ks_value.name}}{{/if}}{{/each}}.'
The above is a template that can handle loops and conditional statements.
2. Perform syntax analysis on the string, generate a syntax tree, and replace the corresponding tags or data.
The following is how to use Ext’s xtemplate:
{name}'s favorite beverages:
',Advantages: powerful functions and high flexibility
Disadvantages: It is complicated to use and even harder to understand. Not easy to debug.
1. Using templates in controls can separate data and DOM. However, if a control contains a large number of templates, it will increase the user's workload and make it difficult to debug, so a balance needs to be weighed.
2. If a large number of controls use the same template and the same data structure, it is inconvenient to configure each control individually. A better solution is to allow the parent control to configure the template.