Skip to content

Instantly share code, notes, and snippets.

@calacitizen
Last active April 21, 2016 09:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calacitizen/f825e9dfb7d1d646fd32fcfb752e3b5f to your computer and use it in GitHub Desktop.
Save calacitizen/f825e9dfb7d1d646fd32fcfb752e3b5f to your computer and use it in GitHub Desktop.

###Генерация вёрстки

Генерация с помощью AST

/// <div class="oh"><p>hi</p></div>

var traversedAST = [{
   type: 'tag',
   name: 'div',
   attrs: {
      class: {
         data: {
            type: 'text',
            value: 'oh'
         }
      }
   },
   selfclosing: false,
   children: [
      {
         type: 'tag',
         name: 'p',
         attrs: {},
         selfclosing: false,
         children: [
            {
               type: 'text',
               data: {
                  type: 'text',
                  value: 'hi'
               }
               raw: 'hi'
            }
         ]
      }
   ]
}];
//Генерация вёрстки из AST шаблона
var html = tmpl.process(traversedAST, {}); // <div class="oh">123</div>

####Генерация с помощью функции AST

function ast(___helpers) {
   var modules = __helpers.modules,
       decorators = __helpers.decorators;
       
    var result = [
	    TMPL.createTag({
	    	name: 'div',
	    	selfclosing: false,
	    	attrs: {
	    	   class: {
	    	   	data: {
	    	   	   type: 'text',
	    	   	   value: 'oh'
	    	   	}
	    	   }
	    	},
	    	children: [
	    		TMPL.createTag({
	    			name: 'p',
	    			selfclosing: 'false',
	    			attrs: {},
	    			children: [
	    				TMPL.createTextNode({
	    					'hi'	
	    				})
	    			]
	    		})
	    	]
	    })
    ];  

}
//Генерация вёрстки из AST функции
var html = tmpl.process(ast(__helpers), {}); // <div class="oh"><p>hi</p></div>

####Генерация с помощью функции конкатенации

function ast(data, ___helpers, includes) {
   var modules = __helpers.modules,
       partial = __helpers.partial,
       decorators = __helpers.decorators,
       __getexpression = __helpers.getexpression;

   var result = '<div class="oh"><p>hi</p></div>';
   return result;
}
var html = ast({}, __helpers, includes);  // <div class="oh"><p>hi</p></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment