将JavaScript库添加到Octopress帖子?

时间:2012-06-10 00:11:08

标签: blogs octopress

对于不同的帖子(通过rake new_post[my_post]生成),我想添加一个仅包含在该帖子中的javascript函数,以及博客上的其他任何内容。

我可以通过手工编辑public/my_post/index.html文件来完成此操作,但每次执行rake generate时,我都必须再次执行此操作。

是否有内置的方法可以在Octopress中实现这一点?

干杯

2 个答案:

答案 0 :(得分:10)

在2.1中,您可以通过在yaml前端设置一些变量来将每页/后JavaScript或CSS资源注入到标题中。

目前,您只需在帖子或页面本身内插入一个链接或脚本标记,它就会被加载到位。例如:

<script type="text/javascript" src="/path/to/file.js"></script>
<link rel="stylesheet" type="text/css" href="/path/to/file.css">

答案 1 :(得分:0)

假设您在帖子中需要this.jsthat.js将它们保存在新创建的/javascripts/custom/目录下。

在默认布局中添加<head>标记内的内容,如:

{% if page.custom_javascript %}
    {% for js in page.custom_javascript %}
        <script type="text/javascript" src="/javascripts/custom/{{ js }}"></script>    
    {% endfor%}
{% endif %}

最后你可以注入每个帖子的javascript,只需添加到帖子YAML前面的字段custom_javascript

---
layout: post
title: "Insert javascript inside head with Octopress"
custom_javascript: [this.js, that.js]
---

当然,您可以使用类似的方法将其他内容注入<head>

相关问题