How to use the setDefaults function from swig
Find comprehensive JavaScript swig.setDefaults code examples handpicked from public code repositorys.
swig.setDefaults is a function in the Swig templating engine that sets default options for rendering templates.
GitHub: Vertafore/docular
![Vertafore profile picture](/Vertafore.png?size=40)
157 158 159 160 161 162 163 164 165 166
})); fse.copySync(path.resolve(__dirname, '../../', './resources/css'), baseFolder + '/resources/css'); lessResolver.resolve(); }.bind(this)); swig.setDefaults({ locals: { config: structure }, varControls: ['{=', '=}'] //Angular uses the same set ( {{ and }} ), and we don't want swig interpreting angular syntax.
GitHub: zeonjs/zeon-ms-05
104 105 106 107 108 109 110 111 112 113
temp = '{% extends "' + layout_code + '" %}\n' + temp; var template = {}; template[layout_code] = data.layout.template; swig.invalidateCache(); swig.setDefaults({ loader: swig.loaders.memory(template) }); var page_relative_path = path.relative(path_parse.dir, config.dir._module); page_relative_path == '' ? page_relative_path = '.' : null; page_relative_path = page_relative_path.replace(/\\/ig, '/');
How does swig.setDefaults work?
Swig is a popular templating engine for Node.js that allows developers to generate HTML or other text-based formats using a combination of markup and programming logic. The swig.setDefaults function is used to set default options for rendering templates with Swig. When a template is rendered with Swig, these default options are used unless they are overridden by explicit options passed to the render function. Here's how swig.setDefaults works: javascript Copy code {{{{{{{ const swig = require('swig'); swig.setDefaults({ cache: false, locals: { title: 'My Website' } }); In this example, we use the require function to import the swig module, which gives us access to the setDefaults function. We then call setDefaults and pass in an object that specifies our desired default options. The cache option controls whether Swig should cache compiled templates for faster rendering on subsequent requests. In this case, we set it to false, which disables caching. The locals option is an object that contains default values for variables that can be used in templates. In this case, we set a title variable to the string 'My Website', which can be used in any template rendered with Swig. Once these default options are set, any subsequent templates rendered with Swig will use them unless overridden by explicit options passed to the render function. For example: javascript Copy code {{{{{{{ class="!whitespace-pre hljs language-javascript">const html = swig.renderFile('my-template.html', { title: 'My Page' }); In this example, we render a template file named 'my-template.html' using Swig's renderFile function. We pass in an object containing a title property with the value 'My Page'. This title property overrides the default title value we set with swig.setDefaults, so the resulting HTML will use 'My Page' as the page title instead of 'My Website'.
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
/** * Loads templates from the file system. * @alias swig.loaders.fs * @example * swig.setDefaults({ loader: swig.loaders.fs() }); * @example * // Load Templates from a specific directory (does not require using relative paths in your templates) * swig.setDefaults({ loader: swig.loaders.fs(__dirname + '/templates' )}); * @param {string} [basepath=''] Path to the templates as string. Assigning this value allows you to use semi-absolute paths to templates instead of relative paths.
+ 23 other calls in file
320 321 322 323 324 325 326 327 328 329
template_index: templateIndex, render_version: '0.1.0', cms_edit: req.query.env === 'dev' }; var model = {}; swig.setDefaults({locals: defaults}); // Add routes to the context on first load if (firstLoad) { swig.renderFile(templateIndex, model);
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12
const swig = require("swig"); swig.setDefaults({ cache: false, locals: { appName: "My App", author: { name: "John Smith", email: "john@example.com", }, }, });
In this example, we import the swig module using the require function and call the swig.setDefaults function with an object that specifies our desired default options. We set the cache option to false to disable template caching for faster development iterations. We also set the locals option to an object that contains default values for variables that can be used in templates. This object includes two properties: appName, which is set to the string 'My App', and author, which is an object that contains a name property set to 'John Smith' and an email property set to 'john@example.com'. Once these default options are set, any subsequent templates rendered with Swig will use them unless overridden by explicit options passed to the render function. For example: javascript Copy code
32 33 34 35 36 37 38 39 40 41 42 43
} }; var setupSwig = function () { highlight.apply(swig); swig.setDefaults(opts.defaults); swig.setFilter('contains', function (input, contains) { return input.indexOf(contains) > - 1; });
56 57 58 59 60 61 62 63 64 65
globalVars = {}; var async = this.async(); if (options.swig !== 'undefined') { swig.setDefaults(options.swig); } try { globalVars = grunt.util._.extend(data, grunt.file.readJSON(path.join(process.cwd(), 'src', 'swig.json')));
swig.compileFile is the most popular function in swig (1638 examples)