http://code.google.com/p/jsmarty/ - use it

On Oct 25, 12:21 pm, "七星海" <[EMAIL PROTECTED]> wrote:
> var Smarty=Class.create();
> Smarty.pattern=/(^|.|\r|\n)(#\{(if|for|else|end)?(.*?)\})/;
> Smarty.pattern_key=/\$(\w+)/;
> Smarty.prototype={
> initialize: function(template){
>         this._var={};
>         this.source=template.toString();
>         this.template='';
>         this._parse();},
>
> assign  : function(key, value){
>         this._var[key]=value;},
>
> clear_assign: function(key){
>         delete this._var[key];},
>
> evaluate:       function(){
>         try{
>                 return eval(this._template);
>         }catch(err){
>                 message.alert( err.message );
>                 throw err.message+" in smarty.js line 19";
>         }},
>
> _parse: function(){
>     var result = ['var __tpl__=[];\n'], source = this.source,level= 0,
> match, _var;
>     while (source.length > 0) {
>       if (match = source.match(Smarty.pattern)) {
>                 _var=source.slice(0, match.index);
>                 if(match[1] == '\\'){
>                         result.push( '\t'.times(level)+'__tpl__.push("'+(_var
> +match[2]).replace(/"/g,'\\"')+'");\n' );
>                 }else{
>                         result.push( '\t'.times(level)+'__tpl__.push("'+(_var
> +match[1]).replace(/"/g,'\\"')+'");\n' );
>                         _var=match[4].replace(Smarty.pattern_key, 
> 'this._var["$1"]');
>                         switch(match[3]){
>                         case 'if':
>                         case 'for':
>                                 result.push( 
> '\t'.times(level)+match[3]+'('+_var+'){\n' );
>                                 level++;
>                                 break;
>                         case 'else':
>                                 result.push( '\t'.times(level-1)+'}else{\n' );
>                                 break;
>                         case 'end':
>                                 level--;
>                                 result.push( '\t'.times(level)+'}\n' );
>                                 break;
>                         default:
>                                 result.push( 
> '\t'.times(level)+'__tpl__.push('+_var+');\n' );
>                                 break;
>                         }
>                 }
>         source  = source.slice(match.index + match[0].length);
>       } else {
>         result.push( '\t'.times(level)
> +'__tpl__.push("'+source.replace(/"/g,'\\"')+'");\n' ); source = '';
>       }
>     }
>         result.push('__tpl__.join("");');
>         this._template=result.join('');
>
> }
> };
>
> Support some logic: if and for
>
> Using:
> var template='aa#{$module.test1}bb#{if
> $module.test2=="test2"}ccc#{else}ddd#{end}ddd#{for var i=0;i<5;i+
> +}#{$module.test3}#{end}';
>  var temp=new Smarty(template);
> temp.assign("module", { test1: 'test1', test2: 'test2', test3:
> 'test3'});
> temp.evaluate();---
>
> >string(   aatest1bbcccdddtest3test3test3test3test3   )


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to