There is about such a pattern

{% include 'template/header.html' %} <div class="content"> ... </div> {% include 'template/footer.html' %} 

I need to render it to a file.

 var Twig=require('twig'); var file='путь к файлу'; var html = fs.readFileSync(file, 'utf8'); html=Twig.twig( //Вариант 1 {href:file} /* TwigException: Unsupported platform: Unable to do remote requests because there is no XMLHTTPRequest implementation */ //Вариант 2 {data:html} /* TwigException: Cannot extend an inline template. */ ).render({ ... }); console.log(html); fs.writeFile(dest, html, ... дальше все ок 

How to do it?

    2 answers 2

    I faced the same problem and solved the issue as follows:

    1) create index.twig

     <!DOCTYPE html> <html lang="en"> <head></head> <body> <h2>Hi {{ name }}!</h2> {% include 'body' %} </body> </html> 

    2) create body.twig

     <p>We are glad to see you here</p> 

    3) create server.js

     var fs = require('fs'); var http = require('http'); var Twig = require('twig'); Twig.twig({ id: 'body', data: fs.readFileSync('body.twig').toString() }); var html = Twig.twig({ id: 'index', ref: 'body', allowInlineIncludes: true, data: fs.readFileSync('index.twig').toString() }).render({ name: '@Darth' }); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); response.write(html); response.end(); }).listen(8888); 

    4) launch node server.js (from the command line) and go to http: // localhost: 8888 / we will see (if you look at the source code):

     <!DOCTYPE html> <html lang="en"> <head></head> <body> <h2>Hi @Darth!</h2> <p>We are glad to see you here</p></body> </html> 

    PS The main points here are that all templates need to be assigned an id and passed to the ref of another template with the allowInlineIncludes: true option allowInlineIncludes: true

      I didn't find anything in Google, they didn't answer me here .. In general, I did this:

       {{ header }} <div class="content"> ... </div> {{ footer }} 

      render:

       var Twig=require('twig'); var html = fs.readFileSync('путь к шаблону/report.twig', 'utf8'); var header = fs.readFileSync('путь/header.html'), 'utf8'); var footer = аналогично; header=Twig.twig({data:header}).render({ ... данные ... }); footer= аналогично; html=Twig.twig({data:html}).render({ header:header, footer:footer, ... данные ... }); console.log(html); fs.writeFile('сгенерированное имя файла', html, ... дальше все ок 

      Everything is working.