当前位置: 首页 > 面试题库 >

Express.js HBS模块-从.HBS文件注册部分文件

满子实
2023-03-14
问题内容

我在express.js中使用handlebars.js
hbs包装器。我的模板可以正常工作,但是我需要添加局部视图以与视图一起呈现。

我想做这样的事情:

hbs.registerPartial('headPartial', 'header'); 
// where "header" is an .hbs file in my views folder

但是,它抛出“找不到标头部分”。

如果我将html字符串传递给第二个参数,则可以使registerPartial起作用,但是我想对局部对象使用单独的视图文件。

我还没有找到任何文档,但是希望我可能缺少一些简单的东西。

有谁知道如何在registerPartial方法中使用视图文件?如果是这样,我该如何实施?

更新

为了提供更多的上下文,让我添加更多的代码。这是我的“服务器”文件-app.js

var express = require('express')
, routes = require('./routes')
, hbs = require('hbs');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'hbs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// this is the line that generates the error
hbs.registerPartial('headPartial', 'header');

// What I'm expecting is for "headPartial" to be a compiled template partial 
// of the template within views/header.hbs, but it is not loading this way.
// If I do something like hbs.registerPartial('headPartial', '<p>test</p>');
// then it does work. I need to know how to pass an .hbs file to the
// registerPartial method.

// Routes
app.get('/', routes.index);

app.listen(3000);

这是我的routes.index文件:

exports.index = function(req, res){
  res.render('index', { title: 'Express' })
};

在我的views文件夹中,我有三个模板:

views/
  header.hbs (this is my partial)
  index.hbs
  layout.hbs

在我的index.hbs文件中,我使用以下命令调用“ headPartial”部分:

{{> headPartial}}

任何帮助是极大的赞赏。


问题答案:

此代码将所有部分模板加载到目录中,并使它们按文件名可用:

var hbs = require('hbs');
var fs = require('fs');

var partialsDir = __dirname + '/../views/partials';

var filenames = fs.readdirSync(partialsDir);

filenames.forEach(function (filename) {
  var matches = /^([^.]+).hbs$/.exec(filename);
  if (!matches) {
    return;
  }
  var name = matches[1];
  var template = fs.readFileSync(partialsDir + '/' + filename, 'utf8');
  hbs.registerPartial(name, template);
});


 类似资料:
  • 模块可以分配到文件/目录的层次结构中。让我们将可见性小节例子 的代码拆开分到多个文件中: $ tree . . |-- my | |-- inaccessible.rs | |-- mod.rs | `-- nested.rs `-- split.rs 在 split.rs 文件: // 此声明将会查找名为 `my.rs` 或 `my/mod.rs` 的文件,并将该文件的内容插入到 /

  • 创建文件分类 接口 POST https://cloud.minapp.com/userve/v1/file-category/ 参数说明 Content-Type: application/json 参数 类型 必填 说明 name String Y 文件分类的名称 代码示例 var axios = require('axios').create({ withCredentials: tru

  • 创建文件分类 接口 POST https://cloud.minapp.com/oserve/v1/file-category/ 参数说明 Content-Type: application/json 参数 类型 必填 说明 name String Y 文件分类的名称 代码示例 {% tabs createCategoryCurl=”Curl”, createCategoryNode=”Node”

  • 问题内容: 到目前为止,直到未模块化的Java,您只需将文件放入以确保它位于类路径中,然后使用 从classpath的几乎任何地方开始。 现在有了模块,地块变厚了。 我的项目设置如下: 配置文件放在里面。 项目运行于 由于主类不是位于我自己的项目中,而是位于外部框架模块中,因此看不到。现在的问题是,是否有办法以某种方式将我的配置文件放入模块或打开它?我是否必须更改框架上游加载文件的方式? 我尝试在

  • 我的项目设置如下: 配置文件放在中。 运行项目时使用 如何以最佳实用的方式实现这一点,使它能够像在Java8中一样工作,并且尽可能少的改动?

  • 很多程序需要向系统里面注册dll文件才能正确的运行,例如各种类型的IE工具条、网络银行的密码保护等等。在QQ里面,需要注册Dll才可以使用大部分功能,这其中就包括TIM平台。 TIM平台要注册2个DLL:TIMProxy.dll和Timwp.dll 如何让安装程序在安装过程中注册DLL文件呢? regdll “$instdir\TIMProxy.dll” regdll “$instdir\Timw