简体   繁体   English

如何在快速车把中添加许多动态内容部分?

[英]How can I add many sections of dynamic content in express-handlebars?

I want to use templates in express-handlebars, i've got a file named "layout.hbs" in a folder called "layouts" inside my views folder for express, i've specified in express view engine config that i want to use handlebars as view engine and i want to use a layout:我想在 express-handlebars 中使用模板,我在我的视图文件夹中名为“layouts”的文件夹中有一个名为“layout.hbs”的文件,我已经在我想要使用的 express 视图引擎配置中指定车把作为视图引擎,我想使用布局:

app.set("port", process.env.PORT || 3000);
app.set("views", path.join(__dirname, "resources/views"));
app.engine(".hbs", exphbs({
  defaultLayout : "template",
  layoutsDir : path.join(app.get("views"), "layouts"),
  partialsDir : path.join(app.get("views"), "partials"),
  extname : ".hbs",
  helpers : require("./helpers/handlebars/handlebars")
}));
app.set("view engine", ".hbs");

It works fine, i can use the layout in my handlebars files, for now i know that i can use the {{{ body }}} syntax to put the variable HTML content on the template, so, in my layout.hbs file i have this:它工作正常,我可以在我的车把文件中使用布局,现在我知道我可以使用{{{ body }}}语法将变量 HTML 内容放在模板上,所以,在我的 layout.hbs 文件中拥有这个:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://localhost:3000/css/style.min.css">
    <title>Welcome!!</title>
</head>
<body>
    {{{ body }}}
</body>
</html>

And in my main.hbs file i have this:在我的 main.hbs 文件中,我有这个:

<h1>Hello from main!!!</h1>

So the final result is:所以最终的结果是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://localhost:3000/css/style.min.css">
    <title>Welcome!!</title>
</head>
<body>
    <h1>Hello from main!!!</h1>
</body>
</html>

And that's fine, it puts the dynamic content oh the {{{ body }}} tag, but now i need to add more sections of dynamic content to my template like styles and scripts that are different for every page, for example, in my main.hbs i want to add this style:没关系,它将动态内容放在{{{ body }}}标签中,但现在我需要在我的模板中添加更多动态内容部分,例如 styles 和每个页面都不同的脚本,例如,在我的main.hbs 我想添加这种风格:

<link rel="stylesheet" href="http://localhost:3000/css/main.min.css">

But in contact.hbs i want to add:但在contact.hbs我想补充:

<link rel="stylesheet" href="http://localhost:3000/css/contact.min.css">

I don't want to add both tags in the layout 'cause when i load the main page i will be loading the contact styles that i don't need and it will make more slower the load of the page.我不想在布局中添加这两个标签,因为当我加载主页时,我将加载我不需要的联系人 styles,它会使页面加载更慢。

In Laravel Blade i can do something like:在 Laravel Blade 中,我可以执行以下操作:

Layout.blade.php Layout.blade.php

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://localhost:3000/css/style.min.css">

    @yield('css', "") 

    <title>Welcome!!</title>

</head>
<body>
    @yield('main', "") 
</body>
</html>

main.blade.php main.blade.php

@section('css')
    <link rel="stylesheet" href="{{ asset("css/main.min.css") }}">
@endsection

@section('main')
    <h1>Hello from main!!!</h1>
@endsection

And it works, how can i do the same in express-handlebars?它有效,我怎样才能在快速车把中做同样的事情?

I found the solution here .我在这里找到了解决方案。

Basically, we have to add this helper function to your helpers of express-handlebars:基本上,我们必须将此助手 function 添加到您的快速车把助手中:

section: (name, options) => { 
        if (!this._sections) this._sections = {};
         this._sections[name] = options.fn(this); 
         return null;
}

And then in your layouts file you can do something like this:然后在您的布局文件中,您可以执行以下操作:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://localhost:3000/css/style.min.css">

    {{{_sections.css}}}

    <title>Welcome!!</title>

</head>
<body>
    {{{body}}}

    {{{_sections.scripts}}}
</body>
</html>

And in your files you can do this:在您的文件中,您可以执行以下操作:

<h1>Hello from main!!!</h1>


{{#section 'css'}}
    <link rel="stylesheet" href="http://localhost:3000/css/main.min.css">
{{/section}}

{{#section 'scripts'}}
    <script src="http://localhost:3000/js/main.min.js"></script>
{{/section}}

And it will results in:这将导致:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://localhost:3000/css/style.min.css">

    <link rel="stylesheet" href="http://localhost:3000/css/main.min.css">

    <title>Welcome!!</title>

</head>
<body>
    <h1>Hello from main!!!</h1>

    <script src="http://localhost:3000/js/main.min.js"></script>
</body>
</html>

Edit:编辑:

If you want something more similar to Laravel with the default text feature you can add this helper:如果您想要更类似于 Laravel 且具有默认文本功能的内容,您可以添加此帮助程序:

yield: (name, default_value) => {

    if(typeof default_value == "object") default_value = "";
    return this._sections[name] || default_value;

}

And use in your layout:并在您的布局中使用:

{{{ yield "section_index" "default value" }}}

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

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