* This source code was highlighted with Source Code Highlighter.
- /*
- * jQuery Template plugin 1.3
- *
- * Copyright (c) 2009 Anton Kolodii
- *
- * Dual licensed under the MIT and GPL licenses:
- * http://www.opensource.org/licenses/mit-license.php
- * http://www.gnu.org/licenses/gpl.html
- */
- ;(function($) {
- function Property() {
- this.Name = "";
- this.pos = -1;
- };
- function PatternEntities() {
- this.str = "";
- };
- jQuery.extend( {
- prepare : function(str) {
- var NameList = new Array();
- var i = 0;
- while (true) {
- var sear = str.search(/{(.)*}/gi);
- if (sear == -1)
- break;
- var name = str.substring(sear + 1, str.indexOf("}"));
- if (name.indexOf("foreach:") == -1) {
- var property = new Property();
- property.pos = sear;
- property.Name = name;
- property.type = 'property';
- str = str.replace("{" + name + "}", "");
- NameList[i] = property;
- } else {
- //name = name.substring(8); //why .substring(8), because "foreach:" has 8 symbols and now name has such form "foreach:name"
- var nestedstr = str.substring(str.indexOf("}") + 1, str.indexOf("{/foreach:" + name.substring(8) + "}"));
- var strEntity = $.prepare(nestedstr);
- strEntity.pos = sear;
- strEntity.Name = name.substring(8);
- strEntity.type = 'entity';
- NameList[i] = strEntity;
- str = str.replace(new RegExp("{foreach:" + strEntity.Name + "}.*{\/foreach:" + strEntity.Name + "}", "i"), "");
- }
- i++;
- }
- var entity = new PatternEntities();
- entity.Name = "";
- entity.str = str;
- entity.type = 'root';
- entity.Parameters = NameList;
- return entity;
- },
- filltemplate : function(Entities, data) {
- function insert(str, ins, pos) {
- return str.substring(0, pos) + ins + str.substring(pos);
- }
- ;
- function fillTemplate(template, data, NameList) {
- var fultmpl = template;
- for (i = NameList.length - 1; i >= 0; i--) {
- fultmpl = insert(fultmpl, data[NameList[i].Name],
- NameList[i].pos);
- }
- return fultmpl;
- }
- ;
- var NameList = Entities.Parameters;
- var paternStr = Entities.str;
- if (NameList && NameList.length > 0) {
- for ( var i = NameList.length - 1; i >= 0; i--) {
- if (NameList[i].type == "entity") {
- if (NameList[i].Name) {
- var eachstr = "";
- $.each(data[NameList[i].Name], function(i, item) {
- eachstr += $.filltemplate(NameList[i], item);
- });
- paternStr = insert(paternStr, eachstr, NameList[i].pos);
- } else {
- var eachstr = "";
- $.each(data, function(j, item) {
- eachstr += $.filltemplate(NameList[i], item);
- });
- paternStr = insert(paternStr, eachstr, NameList[i].pos);
- }
- } else {
- paternStr = insert(paternStr, data[NameList[i].Name], NameList[i].pos);
- }
- }
- } else {
- return paternStr;
- }
- return paternStr;
- },
- stringformat : function(str, data) {
- var entities = $.prepare(str);
- return $.filltemplate(entities, data);
- }
- });
- })(jQuery);
So, for instance, your template string is:
And your JSON data is:
So after executing function from this plug-in we receive new string:
Return this result:
Download js file from link
P.S. This is my first JQuery plug-in. Please write in comment if you have questions.
template =
<div style="color: red;" >
Thank you.
{foreach:} <span style="color: green; margin: 10px;"> Wine - {wine} </span>
<span>Sugar - {sugar} Lemon is {lemon} </span> {/foreach:}
</div>
And your JSON data is:
data = [{"wine":3,"sugar":4,"lemon":22},{"wine":2,"sugar":3,"lemon":12}]
So after executing function from this plug-in we receive new string:
var resultstr = $.stringformat(template, data);
Return this result:
<div style="color: red;" >
Thank you.
<span style="color: green; margin: 10px;" > Wine - 3 </span>
<span> Sugar - 4 Lemon is 22
<span style="color: green; margin: 10px;"> Wine - 2 </span>
<span> Sugar - 3 Lemon is 12 </span>
</div>
Download js file from link
P.S. This is my first JQuery plug-in. Please write in comment if you have questions.
Комментариев нет:
Отправить комментарий