简体   繁体   English

如何使用车把模板?

[英]How to use a handlebar template?

I bought a theme expecting it to have HTML and Angular versions but all I see are HBS files.我买了一个主题,希望它有 HTML 和 Angular 版本,但我看到的只是 HBS 文件。 I am newbie to the grunt/express/npm the whole scenario and I'm lost on how to extract a possible html version from these files.我是整个场景中 grunt/express/npm 的新手,我迷失了如何从这些文件中提取可能的 html 版本。

There is a Gruntfile.js and I tried running "grunt" on CLI but I get an error saying "unable to find local grunt file".有一个 Gruntfile.js,我尝试在 CLI 上运行“grunt”,但出现“无法找到本地 grunt 文件”的错误消息。 Feels like it is some sort of Handlebar template.感觉就像是某种 Handlebar 模板。

Below is the file structure.下面是文件结构。 在此处输入图片说明

在此处输入图片说明

Handlebar is nothing rather than a template engine on top of Mustache, which means it's possible to see HTML as well as interpolation variables inside. Handlebar 只不过是 Mustache 之上的模板引擎,这意味着可以看到 HTML 以及内部的插值变量。 that.那。

As an example举个例子

Handlebars templates look like regular HTML, with embedded handlebars expressions.把手模板看起来像普通的 HTML,带有嵌入的把手表达式。

<div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
</div>

A handlebars expression is a {{, some contents, followed by a }}车把表达式是一个 {{,一些内容,后跟一个 }}

You probably see more helpers such as {{#if}} or {{#each}} and etc, so make that easy to iterate or have other necessary logic in the template.您可能会看到更多的助手,例如 {{#if}} 或 {{#each}} 等,因此请使其易于迭代或在模板中包含其他必要的逻辑。

So, as you mentioned Angular, I assume angular is binding data with Handlebar as its template engine, Or alternatively, you may have Express app which uses Handlebar.因此,正如您提到的 Angular,我假设 angular 正在使用 Handlebar 作为其模板引擎绑定数据,或者,您可能拥有使用 Handlebar 的 Express 应用程序。

What you have to do is to extract HTML tags from handlebars template and just ignore {{...}} then replace your content appropriately with {{..}} instead.您需要做的是从把手模板中提取 HTML 标签,然后忽略 {{...}},然后用 {{..}} 适当地替换您的内容。

It's possible to extract even with Grunt or other automation task runners like Gulp or Webpack.甚至可以使用 Grunt 或其他自动化任务运行器(如 Gulp 或 Webpack)进行提取。 However, it may need more efforts and different plugins or specific code.然而,它可能需要更多的努力和不同的插件或特定的代码。

Remember, you need to also copy your CSS in order to see the same style for your HTML stylesheet.请记住,您还需要复制您的 CSS,以便为您的 HTML 样式表看到相同的样式。

Last but not least, There are other ways to extract or to get your template run, however, the simplest is to what I explained.最后但并非最不重要的是,还有其他方法可以提取或运行您的模板,但是,最简单的是我所解释的。

For more information read here有关更多信息,请阅读此处

Regarding:关于:

"unable to find local grunt file". “无法找到本地 grunt 文件”。

it's not as easy as one solution, there may be different problems.它不像一种解决方案那么容易,可能会有不同的问题。 However, you will need to install (preferably) the latest grunt version:但是,您需要安装(最好)最新的 grunt 版本:

npm install grunt --save-dev

that should work for yo as --save-dev will add grunt as a dev-dependency to your package.json.这应该对你有用,因为--save-dev会将 grunt 作为dev-dependency 添加到你的 package.json 中。 This makes it easy to reinstall dependencies.这使得重新安装依赖项变得容易。

Hope it works for you.希望对你有效。

You can use this to convert those .hbs files to .js which you can include directly to your html I believe - https://github.com/yaymukund/grunt-ember-handlebars您可以使用它来将那些 .hbs 文件转换为 .js,您可以将其直接包含到您的 html 我相信 - https://github.com/yaymukund/grunt-ember-handlebars

grunt.initConfig({
  ember_handlebars: {
    all: {
        src: 'src/templates/**/*.hbs',
        dest: 'output/javascripts/templates.js'
    }
  }
});

Handlebars is semantic template engine which means your html is created as string. Handlebars 是语义模板引擎,这意味着您的 html 是作为字符串创建的。 Handlebars is basically used to represent your data. Handlebars 基本上用于表示您的数据。 for example,例如,

JS JS

var context = {site : jsgyanblogspot.com };

Handlebars车把

<h3>{{site}}</h3>

At above, the part with h3 is handlebar, the expression {{site}} looks up the value in the current context and fetches the value jsgyanblogspot.com .在上面,带有h3的部分是车把,表达式{{site}}在当前上下文中查找值并获取值jsgyanblogspot.com

The handlebar part is converted to HTML through the compilation, In your case, you need to precompile the templates.车把部分通过编译转换为HTML,在您的情况下,您需要预编译模板。

After precompiling, all template files would be converted to single js file(.hbs files => single.js).预编译后,所有模板文件将转换为单个 js 文件(.hbs 文件=> single.js)。

Your single.js file contains the HTML.您的single.js文件包含 HTML。 You need the respective npm modules for precompiling, just type npm install from your project directory agile , that installs the required npm modules.您需要相应的 npm 模块进行预编译,只需从项目目录Agile 中键入npm install安装所需的 npm 模块。

If this still does not solve your problem, try to install grunt locally executing command sudo npm install grunt --save-dev from your project directory如果这仍然不能解决您的问题,请尝试从您的项目目录安装grunt本地执行命令sudo npm install grunt --save-dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM