【问题标题】:How to get page title in config file in Eleventy JS?如何在 Eleventy JS 的配置文件中获取页面标题?
【发布时间】:2022-01-14 11:38:17
【问题描述】:

我需要在 ALT 属性中插入页面的标题。如何获得称号?下面的代码位于 .eleventy.js 文件中

现在,如果我传入道具“标题”,我就会得到标题。但是如何在不传递props的情况下获取header呢?

eleventyConfig.addShortcode('image', (src, className, title) => {
let classNameValue = (className) ? className : ' '
let titleValue = (title) ? title : ' '

    let cloudinary =
      'https://res.cloudinary.com/dr24pbwrs/image/upload/q_auto,f_auto/'
    let pathimg = src.replace(
      new RegExp('https://res.cloudinary.com/.*/image/upload/', 'g'),
      ''
    )

    return `<picture class="${classNameValue}">
                            <source
                                srcset="${cloudinary}w_768/${pathimg} 768w,
                                ${cloudinary}w_1024/${pathimg} 1024w,
                                ${cloudinary}w_1024/${pathimg} 1240w"
                                sizes="(min-width: 320px) 768px,
                                (min-width: 768px) 1024px,
                                (min-width: 1024px) 1240px,
                                100vw">
                            <img src="${cloudinary}${pathimg}" alt="${titleValue}" loading="lazy" />
                        </picture>`
  })

【问题讨论】:

    标签: javascript eleventy


    【解决方案1】:

    好的,所以您想使用常规函数,而不是粗箭头,这样您就可以访问页面对象,但是,您无法访问前面的内容。但是,您知道文件现在在哪里,并且可以使用gray-matter 将其读入。answer 解释了如何执行此操作。

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-30
      • 2021-05-26
      • 2015-01-04
      • 1970-01-01
      • 2017-10-19
      相关资源
      最近更新 更多