Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 用于ejs中的循环_Html_Node.js_Express_Ejs_Backend - Fatal编程技术网

Html 用于ejs中的循环

Html 用于ejs中的循环,html,node.js,express,ejs,backend,Html,Node.js,Express,Ejs,Backend,我有一个数组,其中包含一些类似这样的html内容 const articleArray=['<p>first text</p>\r\n','<p>second text></p>\r\n','<p>third text</p>\r\n'] 其中“viewArticles”是一个ejs文件。我已经使用中间件将视图引擎设置为ejs。 这是ejs中的代码 <% for(arr in array) {%>

我有一个数组,其中包含一些类似这样的html内容

const articleArray=['<p>first text</p>\r\n','<p>second text></p>\r\n','<p>third text</p>\r\n']
其中“viewArticles”是一个ejs文件。我已经使用中间件将视图引擎设置为ejs。 这是ejs中的代码

<% for(arr in array) {%>
    arr
<% } %>
我无法呈现任何html。如何解决这个问题

试试这个

<% array.forEach(function(item,index){ %>
        <%= item %> 
    <% }) %>

我真的不确定如何准确地执行您想要的操作,但请看一下:

首先,与其创建HTML元素数组,不如创建一个对象数组,如下所示:

const articles=[{text:“first text”}、{text:“second text”}、{text:“third text”}];
这样,您只需编写一次HTML,我对编程相当陌生,但我不认为您会得到HTML响应的场景,通常是查询数据库中的数据或JSON文件。。。假设数组实际上被传递到.ejs文件,让我们迭代它



如果数组未通过,请在代码中检查以下内容:

//如果已安装
…“依赖性”:{
“ejs”:“x.x.x”,
}//检查package.json文件
//如果导入正确
导入ejs=需要(“ejs”);
//如果指定了视图路径
app.set(“视图”,path.join(uu dirname,“视图”);
//和视图引擎
应用程序集(“查看引擎”、“ejs”);
//还有你的路线
request.render('viewArticles',{articles:articles})//检查文件名是否正确,我多次出错
''这有助于按原样打印html代码


  • 数组是从存储在html中的数据库中获取的。我使用ckeditor存储textarea表单。请解释。只有代码的答案质量很低。
    <% array.forEach(function(item,index){ %>
            <%= item %> 
        <% }) %>