Jekyll /液体模板如果在<h2>标签中,则获取文本

时间:2017-11-07 13:14:11

标签: html jekyll liquid html-heading

我试图从我的帖子中获得一些头条新闻。我目前有这个代码

let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath)

但是extract_element不是一个函数,我怎么能够实现这个呢?

谢谢

2 个答案:

答案 0 :(得分:1)

考虑到您的帖子包含以下内容:

out of heading
<h2>I'm a heading!</h2>
<p>This is a paragraph</p>

然后我们将其内容分开,直到我们得到所需的标题:

{% assign h2_open_start = content|split: '<h2'%}
 # ["out of heading\n", ">I'm a heading!</h2>\n<p>This is a paragraph</p>\n"]

{% assign h2_open_end = h2_open_start[1]| split: '>'%}
# ["", "I'm a heading!</h2", "\n<p", "This is a paragraph</p", "\n"]

{% assign h2_content_array = h2_open_end[1]| split: '</h2'%}
# ["I'm a heading!"]

{% assign h2_content = h2_content_array[0]%}
#"I'm a heading!" 

然后{{ h2_content }}产生:

"I'm a heading!"

答案 1 :(得分:0)

如果您不介意依赖关系,如果您将 jekyll-extract-element gem 添加到您的 gemfile,原始帖子中的代码将起作用。这是我在 CloudCannon 网站上看到的一个常见片段,这个 gem 也是由他们编写的。