|
1 | 1 | --- |
2 | 2 | layout: page |
3 | | -title: Developing template engines for Express |
| 3 | +title: Desenvolvendo template engines para Express |
4 | 4 | menu: advanced |
5 | | -lang: en |
| 5 | +lang: pt-br |
6 | 6 | --- |
7 | 7 |
|
8 | | -# Developing template engines for Express |
| 8 | +# Desenvolvendo template engines para Express |
9 | 9 |
|
10 | | -Use the `app.engine(ext, callback)` method to create your own template engine. `ext` refers to the file extension, `callback` is the template engine function which accepts the location of the file, the options object, and the callback function, as its parameters. |
| 10 | +Utilize o método `app.engine(ext, callback)` sua própria template entgine. `ext` se refere à extensão dos arquivos de template, `callback` é a função do template engine que recebe como parâmetros a localização do arquivo, o objeto `options`, e a fução callback. |
11 | 11 |
|
12 | | -The following is an example of implementing a very simple template engine for rendering ".ntl" files. |
| 12 | +A seguir temos um exemplo bem simples de uma template engine que renderiza arquivos com extensão ".ntl". |
13 | 13 |
|
14 | 14 | ~~~js |
15 | | -var fs = require('fs'); // this engine requires the fs module |
16 | | -app.engine('ntl', function (filePath, options, callback) { // define the template engine |
| 15 | +var fs = require('fs'); // esta engine requer o módulo fs. |
| 16 | +app.engine('ntl', function (filePath, options, callback) { // define a template engine |
17 | 17 | fs.readFile(filePath, function (err, content) { |
18 | 18 | if (err) return callback(new Error(err)); |
19 | | - // this is an extremely simple template engine |
| 19 | + // esta é uma template engine extremamente simples |
20 | 20 | var rendered = content.toString().replace('#title#', '<title>'+ options.title +'</title>') |
21 | 21 | .replace('#message#', '<h1>'+ options.message +'</h1>'); |
22 | 22 | return callback(null, rendered); |
23 | 23 | }) |
24 | 24 | }); |
25 | | -app.set('views', './views'); // specify the views directory |
26 | | -app.set('view engine', 'ntl'); // register the template engine |
| 25 | +app.set('views', './views'); // especifica o dirétórios onde estão as views |
| 26 | +app.set('view engine', 'ntl'); // registra a template engine |
27 | 27 | ~~~ |
28 | 28 |
|
29 | | -Your app will now be able to render ".ntl" files. Create a file named "index.ntl" in the views directory with the following content. |
| 29 | +Você poderá renderizar arquivos ".ntl". Crie uma arquivo chamado "index.ntl" no diretório views com o seguinte conteúdo. |
30 | 30 |
|
31 | 31 | ~~~js |
32 | 32 | #title# |
33 | 33 | #message# |
34 | 34 | ~~~ |
35 | | -Then, create the following route in your app. |
| 35 | +Agora, crie a seguinte rota no seu app. |
36 | 36 |
|
37 | 37 | ~~~js |
38 | 38 | app.get('/', function (req, res) { |
39 | 39 | res.render('index', { title: 'Hey', message: 'Hello there!'}); |
40 | 40 | }) |
41 | 41 | ~~~ |
42 | | -On making a request to the home page, "index.ntl" will be rendered as HTML. |
| 42 | + |
| 43 | +Fazendo-se uma requisição para a home page, "index.ntl" será renderizado como HTML. |
0 commit comments