如何动态生成 Jekyll 配置文件
Jekyll

> 
> 使用 Python 或 Bash 将动态数据插入 Jekyll 静态网站中，并且避免创建一个 API 后端。
> 
> 
> 


![](/data/attachment/album/202111/06/172709dqcv65spvl363fav.jpg "Digital creative of a browser on the internet")


静态网站生成器 [Jekyll](https://opensource.com/article/21/9/build-website-jekyll) 使用 `_config.yml` 进行配置。这些配置都是 Jekyll 特有的。但你也可以在这些文件中 [用我们自己的内容定义变量](https://ayushsharma.in/2021/08/using-variables-in-jekyll-to-define-custom-content)，并在整个网站中使用它们。在本文中，我将重点介绍动态创建 Jekyll 配置文件的一些优势。


在我的本地笔记本电脑上，我使用以下命令来服务我的 Jekyll 网站进行测试：



```
bundle exec jekyll serve --incremental --config _config.yml

```

### 结合多个配置文件


在本地测试中，有时需要覆盖配置选项。我的网站的 [当前 \_config.yml](https://gitlab.com/ayush-sharma/ayushsharma-in/-/blob/2.0/_config.yml) 有以下设置：



```
# Jekyll Configuration

# Site Settings
url: "https://notes.ayushsharma.in"
website_url: "https://notes.ayushsharma.in/"
title: ayush sharma's notes ☕ + ? + ?️
email: ayush@ayushsharma.in
images-path: /static/images/
videos-path: /static/videos/
js-path: /static/js/
baseurl: "" # the subpath of your site, e.g. /blog

```

由于本地的 `jekyll serve` URL 是 `http://localhost:4000`，上面定义的 URL 就不能用了。我可以创建一个 `_config.yml` 的副本 `_config-local.yml` 并替换所有的值。但还有一个更简单的选择。


Jekyll 允许[指定多个配置文件](https://jekyllrb.com/docs/configuration/options/#build-command-options)，后面的声明覆盖前面的声明。这意味着我可以用以下代码定义一个新的 `_config-local.yml`：



```
url:""

```

然后我可以把上述文件和我的主 `_config.yml` 结合起来，像这样：



```
bundle exec jekyll serve --incremental --config _config.yml,_config-local.yml

```

通过合并这两个文件，这个 `jekyll serve` 的 `url` 的最终值将是空白。这就把我网站中定义的所有 URL 变成了相对的 URL，并使它们在我的本地笔记本电脑上工作。


### C结合动态配置文件


一个简单的例子，假设你想在你的网站上显示当前日期。它的 bash 命令是：



```
&gt; date '+%A, %d %B %Y'
Saturday, 16 October 2021

```

我知道我也可以 [使用 Jekyll 的 \_config.yml 的自定义内容](https://ayushsharma.in/2021/08/using-variables-in-jekyll-to-define-custom-content)。我将上述日期输出到一个新的 Jekyll 配置文件中。



```
my_date=`date '+%A, %d %B %Y'`; echo 'my_date: "'$my_date'"' > _config-data.yml

```

现在 `_config-data.yml` 包含：



```
my_date: "Saturday, 16 October 2021"

```

我可以把我的新配置文件和其他文件结合起来，在我的网站上使用 `my_date` 变量。



```
bundle exec jekyll serve --incremental --config _config.yml,_config-local.yml,_config-data.yml

```

在运行上述命令时，`{{ site.my_date }}` 输出其配置的值。


### 结论


上面的例子很简单，但可能性是无穷的。Bash、Python 和其他编程语言可以动态地生成 Jekyll 配置文件。然后我可以在 `build` 或 `serve` 过程中结合这些文件。


对于 [findmymastodon.com](https://findmymastodon.com/)，[我使用 Python 来获取 Mastodon 的用户统计数据](https://gitlab.com/ayush-sharma/find-my-mastodon/-/blob/1.0/src/fetch-instance-data/fetch_data.py#L252)。然后我把这些写进一个新的 [\_config-data.yml 文件](https://gitlab.com/ayush-sharma/find-my-mastodon/-/blob/1.0/_config-data.yml)（目前是手动）。最后，[主页和其他页面从配置文件中显示这些](https://gitlab.com/ayush-sharma/find-my-mastodon/-/blob/1.0/index.md#L16)。这样，我就可以利用一个动态的后台，并且仍然保持我所喜欢的所有静态网站的优点。


我希望这能为你自己的静态网站激发一些想法。Jamstack 对于静态网站是很好的，但你可以避免为动态内容创建整个 API 后台。为什么不使用一个构建作业来创建带有更新内容的配置文件呢？这可能不适合每一种情况，但少一个 API 意味着更少的基础设施移动部件。


我希望这能在你的下一个静态网站项目中给你一些帮助。继续阅读，并祝你编码愉快。


这篇文章最初发布在 [作者的网站](https://ayushsharma.in/2021/10/inserting-dynamic-data-into-jekyll-static-sites-using-python-or-bash) 上，并经授权转载。




---


via: <https://opensource.com/article/21/11/jekyll-config-files>


作者：[Ayush Sharma](https://opensource.com/users/ayushsharma) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[Jekyll](https://opensource.com/article/21/9/build-website-jekyll), the static site generator, uses the `_config.yml`

for configuration. The configurations are all Jekyll-specific. But you can also [define variables with our own content](https://ayushsharma.in/2021/08/using-variables-in-jekyll-to-define-custom-content) in these files and use them throughout your website. In this article, I'll highlight some advantages of dynamically creating Jekyll config files.

On my local laptop, I use the following command to serve my Jekyll website for testing:

`bundle exec jekyll serve --incremental --config _config.yml`


## Combining many configuration files

During local testing, it's sometimes necessary to override configuration options. My website's [current _config.yml](https://gitlab.com/ayush-sharma/ayushsharma-in/-/blob/2.0/_config.yml) has the following settings:

```
# Jekyll Configuration
# Site Settings
url: "https://notes.ayushsharma.in"
website_url: "https://notes.ayushsharma.in/"
title: ayush sharma's notes ☕ + ? + ?️
email: ayush@ayushsharma.in
images-path: /static/images/
videos-path: /static/videos/
js-path: /static/js/
baseurl: "" # the subpath of your site, e.g. /blog
```


Since the local `jekyll serve`

URL is http : //localhost:4000, the URL defined above won’t work. I can always create a copy of `_config.yml`

as `_config-local.yml`

and replace all the values. But there is an easier option.

Jekyll allows [specifying many configuration files](https://jekyllrb.com/docs/configuration/options/#build-command-options) with later declarations overriding previous ones. This means I can define a new `_config-local.yml`

with the following code:

`url:""`


Then I can combine the above file with my main `_config.yml`

like this:

`bundle exec jekyll serve --incremental --config _config.yml,_config-local.yml`


By combining both files, the final value of `url`

for this `jekyll serve`

will be blank. This turns all URLs defined in my website into relative URLs and makes them work on my local laptop.

## Combining dynamic config files

As a simple example, suppose you want to display the current date on your website. The bash command for this is:

```
> date '+%A, %d %B %Y'
Saturday, 16 October 2021
```


I know I can [use Jekyll's _config.yml's for custom content](https://ayushsharma.in/2021/08/using-variables-in-jekyll-to-define-custom-content) as well. I'll output the above date into a new Jekyll config file.

`my_date=`date '+%A, %d %B %Y'`; echo 'my_date: "'$my_date'"' > _config-data.yml`


Now `_config-data.yml`

contains:

`my_date: "Saturday, 16 October 2021"`


I can combine my new config file with the others and use the `my_date`

variable on my website.

`bundle exec jekyll serve --incremental --config _config.yml,_config-local.yml,_config-data.yml`


On running the above command, `{{ site.my_date }}`

outputs its configured value.


## Conclusion

The example above is quite simple, but the possibilities are endless. Bash, Python, and other programming languages can dynamically generate Jekyll config files. I can then combine these during the `build`

or `serve`

process.

For [findmymastodon.com](https://findmymastodon.com/), [I'm using Python to fetch Mastodon user statistics](https://gitlab.com/ayush-sharma/find-my-mastodon/-/blob/1.0/src/fetch-instance-data/fetch_data.py#L252). I'm then writing these into a new [_config-data.yml file](https://gitlab.com/ayush-sharma/find-my-mastodon/-/blob/1.0/_config-data.yml) (currently manually). Finally, the [home page and others display these from the configuration file](https://gitlab.com/ayush-sharma/find-my-mastodon/-/blob/1.0/index.md#L16). This way, I can leverage a dynamic backend and still keep all the static website goodness I'm so fond of.

I hope this has sparked some ideas for your own static websites. Jamstack is great for static websites, but you can avoid creating an entire API backend for dynamic content. Why not instead use a build job to create config files with updated content? It might not suit every use case, but one less API means fewer infrastructure moving parts.

I hope this helps you in some way during your next static website project. Keep reading, and happy coding.

*This article originally appeared on the author's website and is republished with permission.*

## Comments are closed.