@aaashur/eleventy-plugin-define-page-data 中文文档教程

发布于 3年前 浏览 16 项目主页 更新于 3年前

eleventy-plugin-define-page-data

一个 Eleventy 数据插件,使用 page 数据对象使任何数据可用于简码。

Setup

要安装此插件,请在 Eleventy 项目的根目录下运行以下命令:

npm install --save @aaashur/eleventy-plugin-define-page-data

接下来,将插件导入 .eleventy.js 配置文件:

const definePageData = require("@aaashur/eleventy-plugin-define-page-data");

最后,在导出的配置函数中的某处注册插件:

module.exports = eleventyConfig =>
{
    eleventyConfig.addPlugin(definePageData);

    // ...
};

Usage

使用 definePageData 短代码创建一个新的页面数据变量:

{% definePageData key, value %}

例如:

{% definePageData "prefersEmoji", false %}

使用 definePageData添加到 page 对象的数据被命名空间以防止踩踏现有Eleventy 创建和使用的数据属性。 默认情况下,使用 data 命名空间:

{
    date: new Date(),
    inputPath: "./current/page/myFile.md",
    fileSlug: "myFile",
    filePathStem: "/current/page/myFile",
    url: "/current/page/myFile/",
    outputPath: "./_site/current/page/myFile/index.html",

    data: {
        prefersEmoji: false
    }
}

要在短代码源中访问此数据,请使用 this.page.data

/**
 * Add random emoji to the end of a string
 * @param {string} text
 * @returns {string}
 */
eleventyConfig.addShortcode( "emojify", function(text)
{
    if( !this.page.data.prefersEmoji )
    {
        return text;
    }
    else
    {
        // ...
    }
});

??? 注意 — 您必须使用传统的 function() 而不是箭头函数来定义简码,以便 Eleventy 能够 绑定页面数据到你的简码。

definePageData 短代码还支持使用点符号定义嵌套属性:

{% definePageData "user.preferences.prefersEmoji", false %}
{
    // ...

    data: {
        user: {
            preferences: {
                prefersEmoji: false
            }
        }
    }
}

Namespace

将可选的第三个参数传递给 definePageData 以指定自定义命名空间而不是默认的 data

{% definePageData key, value, namespace %}

例如:

{% definePageData "prefersEmoji", false, "customData" %}
{
    // ...

    customData: {
        prefersEmoji: false
    }
}

eleventy-plugin-define-page-data

An Eleventy data plugin to make any data available to shortcodes using the page data object.

Setup

To install this plugin, run the following command at the root of your Eleventy project:

npm install --save @aaashur/eleventy-plugin-define-page-data

Next, import the plugin in your .eleventy.js config file:

const definePageData = require("@aaashur/eleventy-plugin-define-page-data");

Finally, register the plugin somewhere inside your exported configuration function:

module.exports = eleventyConfig =>
{
    eleventyConfig.addPlugin(definePageData);

    // ...
};

Usage

Create a new page data variable using the definePageData shortcode:

{% definePageData key, value %}

For example:

{% definePageData "prefersEmoji", false %}

Data added to the page object using definePageData is namespaced to prevent stomping over existing data properties that are created and used by Eleventy. By default, the data namespace is used:

{
    date: new Date(),
    inputPath: "./current/page/myFile.md",
    fileSlug: "myFile",
    filePathStem: "/current/page/myFile",
    url: "/current/page/myFile/",
    outputPath: "./_site/current/page/myFile/index.html",

    data: {
        prefersEmoji: false
    }
}

To access this data in your shortcode source, use this.page.data:

/**
 * Add random emoji to the end of a string
 * @param {string} text
 * @returns {string}
 */
eleventyConfig.addShortcode( "emojify", function(text)
{
    if( !this.page.data.prefersEmoji )
    {
        return text;
    }
    else
    {
        // ...
    }
});

???? Note — You must define your shortcode with a traditional function() rather than an arrow function in order for Eleventy to bind page data to your shortcode.

The definePageData shortcode also supports defining nested properties using dot notation:

{% definePageData "user.preferences.prefersEmoji", false %}
{
    // ...

    data: {
        user: {
            preferences: {
                prefersEmoji: false
            }
        }
    }
}

Namespace

Pass an optional third argument to definePageData to specify a custom namespace instead of the default data:

{% definePageData key, value, namespace %}

For example:

{% definePageData "prefersEmoji", false, "customData" %}
{
    // ...

    customData: {
        prefersEmoji: false
    }
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文