How to use the compile function from pug

Find comprehensive JavaScript pug.compile code examples handpicked from public code repositorys.

pug.compile compiles a Pug template into a JavaScript function that can be executed to generate HTML markup.

58
59
60
61
62
63
64
65
66
67
~ The name of the template function. Only applies to `compileClient` functions. Defaults to `'template'`.
```

## Methods

### pug.compile(source, ?options)

Compile a Pug template to a function, which can be rendered multiple times with different locals.

```parameter-list
fork icon31
star icon12
watch icon7

+ 3 other calls in file

128
129
130
131
132
133
134
135
136
137
  options.pretty  = 'staticPretty' in config ? config.staticPretty : config.pretty
}

return new Promise((resolve, reject) => {
  try {
    const fn = pug.compile(data, options)
    let html = fn(locals)

    if (!asset) {
      html = this._export(null, JSON.stringify(html))
fork icon3
star icon8
watch icon2

How does pug.compile work?

pug.compile is a function provided by the Pug templating engine that compiles a Pug template into a JavaScript function that can be used to render HTML. The function takes a Pug template string as input and returns a compiled function that can be called with data to generate an HTML output. During the compilation process, the Pug template string is transformed into a JavaScript function that returns a string of HTML. This function can then be called with data to produce the final rendered output.

4
5
6
7
8
9
10
11
12
13
exports.name = 'pug'
exports.inputFormats = ['pug', 'jade']
exports.outputFormat = 'html'

exports.compile = function (source, options) {
  const fn = pug.compile(source, options)
  return {
    fn,
    dependencies: fn.dependencies
  }
fork icon1
star icon6
watch icon4

315
316
317
318
319
320
321
322
323
324
const compile = (data) => {
    if(process.config.templateEngine === 'ejs'){
        return ejs.compile(data, process.config.templateConfig);
    }
    if(process.config.templateEngine === 'pug'){
        return pug.compile(data, process.config.templateConfig);
    }
    return handlebars.compile(data);
};

fork icon7
star icon48
watch icon3

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const pug = require("pug");

const template = `div
h1 Hello #{name}!`;

const compiledFunction = pug.compile(template);
const result = compiledFunction({ name: "John" });

console.log(result); // Outputs: ' Hello John! '

In this example, a Pug template is defined as a string with an interpolated variable name. The pug.compile function is called with this template, and the resulting compiled function is stored in the compiledFunction variable. This function can then be called with an object containing the variable to be interpolated, resulting in the compiled template with the variable replaced with its value. Finally, the result is logged to the console.

3
4
5
6
7
8
9
10
11
12

exports.name = 'pug';
exports.outputFormat = 'html';

exports.compile = function (source, options) {
  var fn = pug.compile(source, options);
  return {fn: fn, dependencies: fn.dependencies}
};
exports.compileClient = function (source, options) {
  return pug.compileClientWithDependenciesTracked(source, options);
fork icon1
star icon0
watch icon2

25
26
27
28
29
30
31
32
33
  filename: path.join(basePath, 'templates/static', filename),
  basedir: basePath
}
);

const outFn = pug.compile(contents, {
  filename: path.join(basePath, 'templates/static', filename),
  basedir: basePath
});
fork icon0
star icon1
watch icon2

200
201
202
203
204
205
206
207
208
209
if (exampleArray.length >= 1) {
    return exampleMarkupArray;
} else if (language === 'html_example') {
    return exampleMarkup;
} else if (language === 'pug_example') {
    let pugFn = pug.compile(code, {pretty: true});
    let pugCompiled = pugFn().trim();

    const re = new RegExp('{lorem', 'i');
    while (re.test(pugCompiled)) {
fork icon0
star icon0
watch icon2

+ 9 other calls in file