简体   繁体   English

使用Webpack 2将数据传递到ejs局部

[英]Pass data to ejs partials using Webpack 2

I am using Webpack 2 and ejs-template-loader I can get data for my page however I need to pass some section of data to one of partials. 我正在使用Webpack 2和ejs-template-loader,我可以为页面获取数据,但是我需要将部分数据传递给partials之一。

<% 
var data = htmlWebpackPlugin.options.data;
var carouselElement = data.carouselElement;
if (data) {  %>
<!-- Header -->
<%- include ../../components/avalon/header/header.ejs %>
    <div class="page-mask">
        <div class="main-content <%= carouselElement%>">
            <%- include ../../components/hero.image/hero.image.ejs %>
            <%- include ../../components/content.carousel/content.carousel.ejs {carouselElement} %>
            <%- include ../../components/experiences/experiences.ejs %>
        </div>
    </div>
<%}%>

above code gave me error like this 上面的代码给了我这样的错误

ENOENT: no such file or directory ENOENT:没有这样的文件或目录

Do you know how to pass data to partials? 您知道如何将数据传递到局部吗?

to pass data in a partial in EJS with include : 使用include在EJS的部分中传递数据:

<%- include( '../mypage', { data: "mydata" }); %>

To show it in mypage.ejs mypage.ejs显示

<%= data %> // will show mydata

with EJS loader, it seems to be like this : 使用EJS加载程序,似乎是这样的:

var mainPage = require("ejs!./mainPage.ejs");
var subPage = require("ejs!./subPage.ejs");

var renderedPage = _.mainPage('<%= mainPage %>', { title: 'data', subPage:subPage  });

<%= renderedPage %>

In mainPage.ejs : mainPage.ejs

<main>
    <h1><%= title %></h1>
    <%= subPage({ title:'sub page', content: 'This is the subPage' }) %>
</main>

In subPage.ejs : subPage.ejs

<div>
    <h2><%= title %></h2>
    <p><%= content %></p>
</div>

Don't forget to add lodash plugin in your webpack conf file : 不要忘记在您的webpack conf文件中添加lodash插件:

plugins: [
    new webpack.ProvidePlugin({
        _: "underscore"
    })
]

Enjoy ! 请享用 !

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

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