Add javascript library to Octopress posts?

For different posts (generated via rake new_post[my_post] ), I want to add a javascript function that should only be included in this post and nowhere else on the blog.

I can do this by manually editing the file public/my_post/index.html , but then every time I do rake generate , I have to do it again.

Is there a built-in way to do this in Octopress?

Greetings

+6
source share
2 answers

In 2.1, you can enter JavaScript or CSS attributes on the page / post in the header by setting some vars in the yaml front element.

Currently, you can simply insert a link or script tag inside the message itself or the page itself, and it will be loaded into place. Example:

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

Suppose you need this.js and that.js in a message to save them in the newly created directory /javascripts/custom/ .

Than in your default layout, add something like inside the <head> tag:

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

Finally, you can add javascript for each post by simply adding custom_javascript to the YAML front-matter field:

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

Of course, you can use a similar approach for other things that you need to enter in <head> .

0
source

Source: https://habr.com/ru/post/917703/


All Articles