Grav-从模板中的页面检索特定项目 [英] Grav - Retrieving specific items from page in template

查看:61
本文介绍了Grav-从模板中的页面检索特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Grav中具有以下页面结构:

I have the following page structure in Grav:

# Title
## Subtitle
### Subsubtitle

页面结构始终相同,只有这三个项目.

The page structure is always the same and it has just those three items.

如何在Twig模板中分别检索每个项目(标题/字幕/字幕)?

How can I retrieve each item (Title/Subtitle/Subsubtitle) separately in Twig template?

然后,树枝模板将执行以下操作:

The twig template will then do things like:

<p> You typed {{ page.whatever_retrieves_the_title_# }} as title, then {{ page.whatever_retrieves_the_subtitle_## }} as subtitle and finally {{ page.whatever_retrieves_the_subsubtitle_### }} as subsubtitle</p>

如果我不是上面的结构,该怎么办?

What if instead of the above structure I have:

- Title
- Subtitle
- Subsubtitle

目标是用户仅添加三个项目的结构,而树枝模板使用每个项目来显示更复杂的布局.

The objective is that the user adds just that structure of three items and the twig template use each item to display a more complex layout.

推荐答案

这是Markdown,对吧?

This is Markdown, right?

# Title
## Subtitle
### Subsubtitle

您可以使用{{ page.content }}在Twig中获取页面Markdown的HTML版本,如

You can get the HTML version of the page's Markdown in Twig with {{ page.content }}, as described in Grav's documentation. So you should get something like this:

<h1>Title</h1>
<h2>Subtitle</h2>
<h3>Subsubtitle</h3>

您可以使用splitraw过滤器来提取这些标签的内容.我还使用了default过滤器,因此,如果标记内容的提取失败,则不会出现错误:

You can use the split and raw filters to extract the contents of those tags. I'm also using the default filter so that there won't be an error if the extraction of the tag contents fails:

Title is:
{{ page.content|split('<h1>')[1]|default|raw|split('</h1>')[0] }}

Subtitle is:
{{ page.content|split('<h2>')[1]|default|raw|split('</h2>')[0] }}

Subsubtitle is:
{{ page.content|split('<h3>')[1]|default|raw|split('</h3>')[0] }}

或者因为Grav似乎提供了 regex_replace过滤器,您也可以使用它:

Or because Grav seems to provide a regex_replace filter, you could also use it:

Title is:
{{ page.content|regex_replace('~.*<h1>(.*)</h1>.*~s', '$1') }}

Subtitle is:
{{ page.content|regex_replace('~.*<h2>(.*)</h2>.*~s', '$1') }}

Subsubtitle is:
{{ page.content|regex_replace('~.*<h3>(.*)</h3>.*~s', '$1') }}


如果相反,您有以下内容:


If instead you have this:

- Title
- Subtitle
- Subsubtitle

您可以再次使用splitdefaultraw过滤器:

You can again use the split, default and raw filters:

Title is:
{{ page.content|split('<li>')[1]|default|raw|split('</li>')[0] }}

Subtitle is:
{{ page.content|split('<li>')[2]|default|raw|split('</li>')[0] }}

Subsubtitle is:
{{ page.content|split('<li>')[3]|default|raw|split('</li>')[0] }}


不是很漂亮. :-)如果标题可以包含HTML(例如## Hello **world**!<h2>Hello <strong>world</strong>!</h2>)或特殊字符,则可能需要在已经很长的魔术符后附加|raw.


Not very beautiful. :-) If the titles can contain HTML (e.g. ## Hello **world**!<h2>Hello <strong>world</strong>!</h2>) or special characters, you probably need to append |raw to the already long magic spells.

这篇关于Grav-从模板中的页面检索特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆