如何在脚本标签中包含 ejs

发布于 2025-01-14 01:30:06 字数 2503 浏览 2 评论 0原文

我想使用脚本标记内的 forEach 循环来渲染 ejs,但出现产品未定义错误。我无法正确地将变量传递给 ejs.render() 函数。

这是我的产品卡 ejs 模板:

<div class="card mb-3" style="max-width: 540px">
    <div class="row no-gutters">
      <div class="col-md-4">
        <img
          src="/images/<%= product.image_groups[0].images[0].link%>"
          class="card-img"
          alt="<%= product.image_groups[0].images[0].alt%>"
        />
      </div>
      <div class="col-md-8">
        <div class="card-body d-flex flex-column">
          <h5 class="card-title"><%= product.name %></h5>
          <p class="card-text">
            <%- product.short_description %>
          </p>

          <div class="card-footer bg-transparent border-dark mt-auto">
            <div class="row">
              <div class="col sm-6">
                <p>Price:<%= product.currency %> <%= product.price %></p>
              </div>
              <div class="col sm-6">
                <a href="/product/<%= product.primary_category_id %>/<%= product.id %>" class="btn btn-primary">More Info</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

这是我的脚本:

<script>
  const form = document.querySelector("form");

  form.addEventListener("submit", async (e) => {
    e.preventDefault();
    // get values
    const productName = form.productname.value
    const searchResultsEl = document.getElementById('searchResults')
    
    try {
      const res = await fetch("/search", {
        method: "POST",
        body: JSON.stringify({ productName }),
        headers: { "Content-Type": "application/json" },
      })
      const foundProducts = await res.json()
      foundProducts.data.forEach(product => {
        let html = ejs.render("<%- include('../product/productCard.ejs') %>",{product:product})
        searchResultsEl.innerHTML += html
        
      })

    } catch (err) {

      console.log(err)
    }
  });
</script>

错误: 产品未定义

我可以使用 console.log 打印产品(产品)所以有产品。我不知道出了什么问题。有什么帮助吗?

错误图片:

错误图像

I want to render ejs using the forEach loop inside the script tag but I get product is not defined error. I cant pass variable into ejs.render() function correctly.

Here is my ejs template for product card:

<div class="card mb-3" style="max-width: 540px">
    <div class="row no-gutters">
      <div class="col-md-4">
        <img
          src="/images/<%= product.image_groups[0].images[0].link%>"
          class="card-img"
          alt="<%= product.image_groups[0].images[0].alt%>"
        />
      </div>
      <div class="col-md-8">
        <div class="card-body d-flex flex-column">
          <h5 class="card-title"><%= product.name %></h5>
          <p class="card-text">
            <%- product.short_description %>
          </p>

          <div class="card-footer bg-transparent border-dark mt-auto">
            <div class="row">
              <div class="col sm-6">
                <p>Price:<%= product.currency %> <%= product.price %></p>
              </div>
              <div class="col sm-6">
                <a href="/product/<%= product.primary_category_id %>/<%= product.id %>" class="btn btn-primary">More Info</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

Here is my script:

<script>
  const form = document.querySelector("form");

  form.addEventListener("submit", async (e) => {
    e.preventDefault();
    // get values
    const productName = form.productname.value
    const searchResultsEl = document.getElementById('searchResults')
    
    try {
      const res = await fetch("/search", {
        method: "POST",
        body: JSON.stringify({ productName }),
        headers: { "Content-Type": "application/json" },
      })
      const foundProducts = await res.json()
      foundProducts.data.forEach(product => {
        let html = ejs.render("<%- include('../product/productCard.ejs') %>",{product:product})
        searchResultsEl.innerHTML += html
        
      })

    } catch (err) {

      console.log(err)
    }
  });
</script>

Error: product is not defined

I can print products by using console.log(product) so there are products. I cant figure out what is the problem. Any help?

Image of Error:

Error Image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2025-01-21 01:30:06

尝试

let html = ejs.render('<%- include("../product/productCard.ejs") %>',{product:product})

Try

let html = ejs.render('<%- include("../product/productCard.ejs") %>',{product:product})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文