杰基尔:在另一篇文章中包括一篇文章 [英] Jekyll: including a post inside another post

查看:143
本文介绍了杰基尔:在另一篇文章中包括一篇文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jekyll是否提供了在其他帖子中添加帖子的方法?我知道这听起来有点傻,但我正在将它用于烹饪/食谱网站。我的一些食谱是由组件或其他较小的食谱组成的。

Does Jekyll provide a way to include a post inside another post? I know that sounds a bit goofy, but I'm using it for a cooking/recipe site. Some of my recipes are made up of components, or other, smaller recipes.

我正在寻找一种方法来包含一些帖子 in 另一篇文章,完整的模板和所有。这可能吗?

I'm looking for a way to include a handful of posts inside another post, complete with template and all. Is this possible?

我做了大量的谷歌搜索,发现我只能包含在一个页面中,而不是发布,并且它没有使用默认的液体模板,只是简单的降价。

I did a ton of googling, and found I was only able to include into a page, not post, and it didn't use the default liquid template, just plain markdown.

想法?

推荐答案

我用过Jekyll在我的网站上收藏,以解决与您类似的问题。

I used Jekyll's Collections in my website to solve a problem similar to yours.

收藏品仍然是Jekyll的一个实验性功能,而我使用的解决方案本身就很苛刻,但如果你不太疯狂,它就足够了。

Collections are still an experimental feature in Jekyll, and the solution I used is pretty hacky by itself, but if you don't go too crazy with it it's manageable enough.

如需更详细和友好的收藏介绍,这是关于开始使用它们的好文章

For a more detailed and friendly intro to collections, here's a great post about getting started with them.

我们需要记住的一件事是我们应该得到远离这类案件的帖子。

One thing we need to have in mind is that we should get away from posts for cases like this.

正如Ben Balter在上面的链接中所提到的,如果人们使用博客文章来发布非博客文章,那么Jekyll已经失败了食谱不是帖子,如果可以,我们应该避免使用它们。

As mentioned by Ben Balter in the link above, "If people are using blog posts for a non-blog post thing, Jekyll has already failed". Recipes are not posts, and we should avoid using them as such if we can.

话虽如此,这是我的方法分三步:

With that said, here's my approach in three steps:


  1. 创建两个集合(我们的自定义帖子类型) - 让我们说这些是食谱和成分

  1. Create two collections (our custom post types) — let's say these are "Recipes" and "Ingredients"

找到一种方法将它们相互联系起来 - 确保生菜将列在沙拉下

Find a way to relate them to each other — To make sure "lettuce" will be listed under "salad"

在食谱中包含相关的成分 - 添加液体代码以实际显示来自沙拉页面某处生菜的信息。

Include the relevant "Ingredients" in your "Recipes" — Adding the liquid code to actually display information from "lettuce" somewhere in the "salad" page.






第1步

创建 _ingredients _recipes 每个集合的文件夹(确保将它们命名为复数形式)并将它们添加到 _config.yml 文件中:

Create _ingredients and _recipes folders for each of your collections (make sure to name them in plural) and add them to your _config.yml file:

    collections:
      ingredients:
        output: true 
      recipes: 
        output: true  
        permalink: /recipes/:path/  

输出:true 参数为集合中的每个项目创建单独的HTML页面永久链接(可选)可让您控制URL。

The output: true parameter creates an individual HTML page for each item in the collection and permalink (optional) lets you control the URL.

第2步

将这样的元数据添加到 lettuce.md 集合项的YAML前端:

Add a piece of metadata like this to the YAML front-matter of your lettuce.md collection item:

    ---        
    parent-collection: salad
    ---

如果 lettuce.md 属于多个配方,则可以添加多个配方。如果您需要说明,请务必在元数据下方添加内容:

If lettuce.md belongs to more than one recipe, you can add more than one. Make sure to add content below the metadata if you need a description:

    ---
    parent-collection: 
     - salad
     - hamburguer
     - taco
    ---

    Lettuce is really good for your health, but it kinda sucks as a food.

请注意 salad hamburguer taco 的名称应该与食谱URL slug完全相同,否则这将无效(即greatrecipes.com/recipes/<$c $ C>沙拉)。如果你的食谱名称是一个句子,你用引号括起来。

Note that salad, hamburguer and taco should named exactly like the recipe URL slug or this won't work (i.e. greatrecipes.com/recipes/salad). If you have a recipe name that's a sentence, you wrap it in quotes.

这个变量稍后会被淘汰,所以这个名字就像 parent -collection:AmazingSouflèFrançais将匹配 the-amazing-soufle-francais 。然而,奇怪的事情发生了:如果有疑问,只需写下小钱包沙拉

This variable will be slugified later on, so a name like parent-collection: The Amazing Souflè Français will match the-amazing-soufle-francais. Still, weird stuff happens: when in doubt, just write smallcaps salad.

这是 hacky 部分。

第3步

为食谱创建特定的布局(例如 _layout / recipe-page.html )并添加以下代码 - 这是我们将配料包含在配方页面中的地方。

Create a specific layout for your recipes (like _layout/recipe-page.html) and add the code below — this is where we'll include the ingredients into the recipe page.

记住你的食谱( salad.md 和朋友们应该指向这个布局。

Remember your recipes (salad.md and friends) should be pointing to this layout.

    {% capture current_collection %}{{ page.url | remove: "recipes" | remove: "/" | remove: " " }}{% endcapture %}

这里我们捕获的名称来自URL的配方并将其分配给 current_collection 变量。确保将其放在一行中,因为将其设置为多行会在捕获的字符串中添加一堆空格,并将代码分解为10/10次。 疯狂。

Here we capture the name of the recipe from the URL and assign it to the current_collection variable. Make sure this in a single line because making it multi-line will add a bunch of whitespace to the captured string and break your code 10/10 times. Insane.

    <div class="recipe-content">
    <p>{{ content }}</p> -- This is the content of the current recipe in the collection, your "post text".
    </div>


    {% for ingredient in site.ingredients %}

    {% capture captured_parent_collection %}{{ ingredient.parent-collection | slugify }}{% endcapture %} -- This capture is just to pass the slugify filter and sanitize parent.collection to make sure it matches the URL slug   

      {% if captured_parent_collection == current_collection %}
        <div class="recipe-ingredient">
          <h3>{{ ingredient.name }}</h3> -- "<h3>Lettuce</h3>"
          <p>{{ ingredient.content }}</p> -- "<p>Lettuce is really good for your health, but it kinda sucks as a food.</p>"
        </div>
      {% endif %}
    {% endfor %}

匹配此配方的成分,并将其中的每一个都包含在布局中。 YASS!

您的每种食材都应该出现在您的食谱文本下面。

Each of your ingredients should be appearing right under your recipe text.

请注意,您没有像在问题中描述的那样在食谱文本中拉出内的成分,但是在它之后的布局中包含它们。据我所知,你不能在你的帖子/收藏文本的中间中添加 - 除非你通过Jekyll的自定义插件破解你的方式。

Note that you're not pulling the ingredients inside the recipe text like you described in your question, but including them in the layout after it. As far as I know you can't really add things in the middle of your post/collection text — unless you hack your way through a custom plugin for Jekyll.

这样做的方法肯定不那么简单,但这就是我的工作方式。如果您发现自己尝试此操作有任何问题,请告诉我 - 其他任何人都可以随意纠正我。

There are certainly less hacky ways to do this, but this is how I made it work. Let me know if you find any trouble trying this for yourself — and anyone else, feel free to correct me.

我在我的网站上进行了此设置 - 看看我拉入的布局我的孩子收集物品子集合项目上的元数据。

I have this setup going on in my site — take a look at the layout where I pull in my child collection items and the metadata on the child collection items themselves.

这篇关于杰基尔:在另一篇文章中包括一篇文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆