使用html模板的正确方法?
我已经开始将一个简单的网站迁移到 Jekyll。我有一组所有内容都继承的模板,因此我所要做的就是填写实际内容。然而,在将内容移过来的过程中,我发现其中一些内容需要修改模板之前指定的标签。
例如,我有一个“主”模板,它指定了直到并包括开始 标记的所有内容。我网站的一个页面使用了一些 JavaScript 来修改 html5 画布,因此其开始标记必须类似于
。现在,拥有一个完全独立的模板,其唯一的更改就是这个单个标记,这似乎很愚蠢,但从模板中删除此标记并让每个页面手动指定开始
标记也同样愚蠢。解决这个问题的正确方法是什么?
I've started migrating a simple site to Jekyll. I have a set of templates that everything inherits from, so that all I have to do is fill in the actual content. However, in moving the content over, I find that some of it needs to modify the tags previously specified by the template.
For example, I have a 'main' template, which specifies everything up to and including the opening <body>
tag. One of my site's pages uses a bit of JavaScript to modify an html5 canvas, and so its opening tag has to be something like <body onload="startDrawing();">
. Now, it seems silly to have an entirely separate template whose only change is this single tag, but equally silly to remove this tag from the template and have every page manually specify the opening <body>
tag. What is the right way to go about solving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在需要特殊 onload 命令的单个页面中,在 YAML frontmatter 中添加一行:
然后,在默认模板中,将
更改为
在具有该 YAML frontmatter 的页面上,它将呈现内容。在其 frontmatter 中没有“special_command”行的页面上,它不会呈现任何内容。您将在 中留下额外的空间。标签在这些页面上,但仍然将 W3 规范作为有效代码传递(或者,至少,当我刚刚运行该代码时它通过了验证器。)
In the individual page that needs the special onload command, add a line to the YAML frontmatter:
Then, in the default template, change
<body>
to
<body {{ page.special_command }}>
On pages that have that YAML frontmatter, it'll render the content. On pages that don't have that "special_command" line in their frontmatter, it'll render nothing. You'll be left with an extra space in the <body> tag on those pages, but that still passes the W3 specs as valid code (or, at least, it passed the Validator when I just ran that code through it.)
我对 Jekyll 一无所知,但我倾向于为包含画布的一页提供一个单独的模板。这是一个特殊情况,除非 Jekyll 可以用一些特殊的语法来解释它,否则对我来说,为这种情况使用不同的模板会更干净。
I don't know anything about Jekyll, but I lean towards having a separate template for the one page that contains the canvas. It's a special case, and unless Jekyll can account for it with some special syntax, it feels cleaner to me to have a different template for this case.