nav*_*een 4 jekyll github-pages
我想知道是否有办法将动态命名内容从布局传递到页面.
例如,
布局
<body>
<div class="container">
{{ content }}
</div>
<footer>
<p>© Company 2015</p>
</footer>
<script src="path/to/jquery.js"></script>
{{ page.other_content }}<!-- Objective: To write scripts that varies with page -->
</body>
Run Code Online (Sandbox Code Playgroud)
页
---
layout: default
title: About Us Page
---
<p> This is about us page </p>
{% capture other_content %}
<script>
$(document).ready(function(){
console.log("About us page.");
});
</script>
{% endcapture %}
Run Code Online (Sandbox Code Playgroud)
有没有办法在布局中呈现页面中的多个内容部分?
@RenderSection("scripts", required: false)
Run Code Online (Sandbox Code Playgroud)
在内容页面中,我们可以像这样呈现该部分.
@section scripts {
Hey I'm actually on the _Main layout.
I can vary according to the page.
My presence is not mandatory in every page
}
Run Code Online (Sandbox Code Playgroud)
这可以用jekyll完成吗?
GitHub Pages禁止使用插件,所以我会坚持使用vanilla Jekyll的解决方法.您当然可以使用插件,在本地生成您的站点并在GitHub上托管结果.
这很难看,但可能.保持您的布局:
<body>
<!-- varies with page -->
{{ page.other_content }}
</body>
Run Code Online (Sandbox Code Playgroud)
然后在您的页面文件中,定义other_content前面的属性:
---
other_content: |
<script>
console.log("You can't stop me!");
</script>
---
Run Code Online (Sandbox Code Playgroud)
管道字符表示多行字符串.根据YAML的规则,您的脚本必须用空格缩进(不允许使用选项卡).
如果您的脚本变得太大,您可以将它们移动到单独的文件中,而页面的前端内容则会引用脚本文件名:
---
scripts: [test]
---
Run Code Online (Sandbox Code Playgroud)
在布局中:
{% for s in page.scripts %}
<script src="{{ s }}.js"></script>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如果test.js与使用它的页面位于同一目录中,则此方法有效.您可以使用site.baseurl获取绝对路径等.
| 归档时间: |
|
| 查看次数: |
1102 次 |
| 最近记录: |