在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:dominicbarnes/koa-handlebars开源软件地址:https://github.com/dominicbarnes/koa-handlebars开源编程语言:JavaScript 99.4%开源软件介绍:koa-handlebars
NOTE: the UsageThis middleware adds 2 methods to the koa context object. The primary one is
app.js var koa = require("koa");
var handlebars = require("koa-handlebars");
var app = koa();
app.use(handlebars({
defaultLayout: "main"
}));
app.use(function *() {
yield this.render("index", {
title: "Test Page",
name: "World"
});
});
app.listen(3000); layouts/main.hbs <!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
{{{@body}}}
</body>
</html> views/index.hbs Hello, {{name}}! <!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
Hello, World!
</body>
</html> ViewsThe entry point for rendering is known as a view. The view usually contains the
content specific to a single page. It has access to all loaded partials, helpers
and is injected into the The simplest usage is to have a flat In "development mode", views are not cached. LayoutsLayouts are the content that is shared between many views. Like views, it has
access to all loaded partials and helpers. The The simplest usage is to have a flat In "development mode", layouts are not cached. PartialsPartials are sub-templates that you can use to render smaller bits within layouts/views. There are 2 main types of partials. First, global partials are registered
during init. (see Secondly, partials residing in the directory specified by HelpersHelpers are functions that any of your templates can call upon. Currently, helpers can only be defined globally and must be declared during
initialization. (see DevelopmentTo enable "development mode" in your server, simply set In addition, this library uses
visionmedia/debug, so you can enable
debug output via Special VariablesWhen rendering templates, koa-handlebars will add 3 special private variables to your templates:
You can add more variables of your own via the Generally speaking, avoid injecting data directly into Configuration OptionsrootThe base directory to use when resolving paths. Default: cacheEnables or disables the view cache. This is basically the flag for "development mode". Default:: app.use(handlebars({
cache: app.env !== "development"
})); dataAdds additional private data (alongside extensionThe extension(s) that your template files can use. Any files not using the given extensions will be ignored. Default: If you have multiple extensions, you simply use an array. For example: viewsDirThe location of your view templates (relative to Default: "views" viewPath(id)Translates the given By default, this simply returns the given This function is run with the renderer as it's context (ie: defaultLayoutIf you are using layouts, then this can be used to bypass requiring each call
to layoutsDirThe location of your layout templates (relative to Default: "layouts" layoutPath(id)Translates the given By default, this simply returns the given This function is run with the renderer as it's context (ie: partialsDirThe location of your non-global partial templates (relative to Default: "partials" partialId(file)This function is a little backwards compared to layouts and views, but it takes
a path for a partial template file. (relative to For example: By default, it will strip the extension and camel-case the remaining string. For example: helpersAllows you to define global helpers during initialization, this should be a shallow object where each key is a helper name and the value is a function. partialsAllows you to define global partials during initialization, this should be a shallow object where each key is a partial name and the value is a function. handlebarsAllows you to pass a custom handlebars instance, which you may want to do in some edge cases. For example, if you plan on using Handlebars.SafeString in your block helpers, you'll need a handle on the same instance of handlebars that the middleware is using. For example:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论