go - 雨果相当于PHP的strpos()

标签 go hugo

因此,我想将.Content分解为适合不同的导航标签。如果有更好的模式可以完成,请告诉我。

因此,在我的/content/shop/product-name/index.md前面,问题包含:

# summary
summary: "This is the **product's** summary which will render markdown"
---
This is the first line of the full description of the product. This section of the ./index.md
page is referenced in the `single.html` file as `.Content`.|^^|This is the next part of the
.Content that I want to throw into a different nav-tab.

然后在/layouts/shop/single.html中:
   <div class="row">
     <div class="col-lg-12">
       {{ .Params.summary }}
     </div>
   </div> 
   <div class="row">
      <div class="col-lg-12">
        <nav class="product-info-tabs wc-tabs mt-5 mb-5">
          <div class="nav nav-tabs nav-fill" id="nav-tab" role="tablist">
            <a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab"
              aria-controls="nav-home" aria-selected="true">Description</a>
            <a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab"
              aria-controls="nav-profile" aria-selected="false">Additional Information</a>
            <a class="nav-item nav-link" id="nav-contact-tab" data-toggle="tab" href="#nav-contact" role="tab"
              aria-controls="nav-contact" aria-selected="false">Reviews</a>
          </div>
        </nav>

        <div class="tab-content" id="nav-tabContent">
          <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
            {{ .Content }}
          </div>
...

在过去的日子里,在PHP中,我可以使用strpos(.Content, '|^^|')然后使用substr(.Content, 0, (strpos(.Content, '|^^|'))来获取一段文本。您还可以使用用户配置的分隔符split('|^^|', .Content)将字符串放入数组中。

因此,回到Hugo,在.Content中,我可能会遇到类似以下情况:
This is the content. This is the last line before being split.|^^|This is the next line, that would be in array[1] or the next indexed substr.

我正在尝试将.Content的这两部分放入single.html页的不同选项卡中。每个产品的.Content显然都将有所不同,因此使用Hugo的substr()的数量我确实无法保持一致。

我看到的使用前件的问题是,尽管它是markdown呈现的,但它不能跨越多行。我知道我可以将\n用于换行符,但是这抵消了markdown的好处。

谢谢。

最佳答案

听起来您可能可以这样做:

{{ replace .Content "|^^|" "</div><div class='tab-pane fade show'>" | safeHTML}}

哪个会变成
      <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
        This is the content. This is the last line before being split.|^^|This is the next line, that would be in array[1] or the next indexed substr.
      </div>

进入
      <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
        This is the content. This is the last line before being split.
      </div>
      <div class='tab-pane fade show'>
        This is the next line, that would be in array[1] or the next indexed substr.
      </div>

关于go - 雨果相当于PHP的strpos(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59779326/

相关文章:

mongodb - 在 n 天后删除 MongoDB 文档

templates - 如何在 hugo go 模板中使用 asciidocify 而不是 markdownify?

hugo - 如何添加新的hugo静态页面?

go - Martini 渲染在页面上显示 {{ yield }}

arrays - GOLANG 从 byte slice 中修剪前导字符,直到遇到某个特定字符

r - 将 'Working Papers' 部分(在出版物中)添加到 Hugo-academic 站点

gitlab - Hugo 无法建立网站,称无法找到主题

Jekyll 相当于 Hugo 的 "with"

performance - 如何使用 Go 限制下载速度?

go - 从数组/slice 中获取元素的索引或从 Go 中的映射中获取值的键?