如何在模板文件中包含元数据?
我有一个通过 erb 过滤模板文件的系统。 使用约定优于配置,输出文件在镜像输入文件的文件层次结构中创建。 许多文件具有相同的名称,我可以使用目录来区分它们。
该计划一直有效,直到我需要将附加信息与每个文件关联起来。 因此,我在每个目录中使用元数据创建了一个 YAML 文件。 现在我有约定和配置。 恶心。
然后我了解了 Webby,以及它在每个模板文件顶部包含 YAML 元数据部分的方式。 它们看起来像这样:
---
title: Baxter the Dog
filter: textile
---
All the best little blogs use Webby.
如果我可以实现这样的标头,我就可以放弃我的层次结构和单独的 YAML 文件。 Webby 实现非常通用,实现了一个新的 MetaFile 类,将标题与“真实文本”分开,但它似乎比我需要的更复杂。
将元数据放在 erb 注释中似乎不错——它将被 erb 自动忽略,但我不确定如何访问注释数据。
<%#
title: Baxter the Dog
%>
有没有办法访问erb评论? 或者也许有不同的方法? 我的很多模板都会执行大量 erb 操作,但如果可以使其余部分变得更容易,我可以在单独的步骤中运行 erb。
I have a system that filters template files through erb. Using convention over configuration, the output files get created in a file hierarchy that mirrors the input files. Many of the files have the same names, and I was able to use the directories to differentiate them.
That plan worked until I needed to associate additional info with each file. So I created a YAML file in each directory with the metadata. Now I have both convention and configuration. Yuck.
Then I learned Webby, and the way it includes a YAML metadata section at the top of each template file. They look like this:
---
title: Baxter the Dog
filter: textile
---
All the best little blogs use Webby.
If I could implement a header like that, I could ditch my hierarchy and the separate YAML files. The Webby implementation is very generic, implementing a new MetaFile class that separates the header from the "real text", but it seems more complicated than I need.
Putting the metadata in an erb comment seems good -- it will be automatically ignored by erb, but I'm not sure how to access the comment data.
<%#
title: Baxter the Dog
%>
Is there a way to access the erb comments? Or maybe a different approach? A lot of my templates do a bunch of erb stuff, but I could run erb in a separate step if it makes the rest easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您也将内容转储为 YAML 怎么样? 据推测,元数据只是转储到 YAML 的哈希值。 您可以将内容作为字符串附加到同一文件中的第二个 YAML 文档中:-
转储就像这样简单:-
加载就像这样简单:-
请注意,管道字符出现在 YAML 以便保留内容字符串中的换行符。
How about if you dump your content as YAML too. Presumably the metadata is simply a Hash dumped to YAML. You could just append the content as string in a second YAML document in the same file :-
Dumping is as simple as :-
Loading is as simple as :-
Note that the pipe character appears in the YAML so that newlines in the content string are preserved.