使用jekyll为网站标题创建变量?

时间:2016-12-04 05:20:45

标签: html ruby jekyll

基本上我想将一段代码压缩成一个可以在多个页面上重复的变量,这样当我对代码进行更改时,我不需要将整个内容复制并粘贴到每个页面上index.html页面。

我相信jekyll通过_layouts_posts选项实现了这一目标?但我不确定如何实现它。我安装了原子,红宝石和杰基尔。还有什么我需要下载以便我在本地查看和编辑吗?



<!DOCTYPE html>

<!--  BLOCK OF CODE THAT I WANT CONDENSED INTO A VARIABLE (START) -->

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="theme-color" content="#000000" />
  <script type="text/javascript" src="/./js/jquery-1.11.3.min.js"></script>
</head>

<header>

<H1>FANCY BLOG TITLE</H1>

  <IMG SRC="https://68.media.tumblr.com/6fef61f4480a1ba41346ca21b96b4643/tumblr_ohn94pLON61vaxxf8o4_1280.jpg" />EXAMPLE SITE HEADER (THIS IS WHAT I WANT TO CONDENSE INTO A VARIABLE AND DUPLICATE, ALONG WITH THE HEAD TAG)

</header>

<!--  BLOCK OF CODE THAT I WANT CONDENSED INTO A VARIABLE (END) -->

<BR>

<body>

  <H2>BLOG POST TITLE</H2>
  (THE BODY DOES NOT NEED TO BE DUPLICATED OR CONDENSED INTO A VARIABLE, AS IT'S CONTENT WILL DIFFER ACROSS SEVERAL PAGES)
  <BR>

  <P>"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
    voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi
    tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui
    in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"</P>

</body>

</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

这是_includes发挥作用的地方。 只需在_includes中创建一个文件head.html或header.html,其中包含您要压缩的内容。

在您的布局中,您可以使用

{% include head.html %}

现在。如果需要,您甚至可以传递参数(有关详细信息,请参阅Jekyll Documentation

答案 1 :(得分:0)

所以我想我想到了这一点,我对ruby完全陌生,所以我一直在学习如何使用它。我安装了使ruby与windows兼容的devkit,做了jekyll new ...,然后创建了一个repo并推送到github。

我实际上甚至不需要使用_include方法,因为我可以在default页面上保留标题的代码。但它实际上工作,并将我的default.html页面缩减为五行代码,这样很酷!我将保留这种方式,以防我将来有更多的元素添加到我的网站。

here是我的傻瓜..(免责声明:如果你是共和党人或民主党人,可能读这个。大声笑。)