在中间人中使用全局变量?例如网站标题

时间:2022-12-24 21:45:55

I'm trying to find a way to use global variables within middleman, i.e. set website name and use it as a fallback if current page doesn't have a title or when passing app title parameter inside metas like <meta name="application-name" content="Site name"> so I don't have to write this manually every time. Issue is that at present I am only able to get current page title like <%= current_page.data.title || "Site Name" %> where it is different for every layout, instead of "Site Name" I'd like to pass a global variable that is stored somewhere in one file and can be accessed by every page.

我试图找到一种方法来使用全局变量在中间人,即设置网站名称和使用它作为后备,如果当前页面传递应用程序时没有一个标题或标题内参数搜索像< meta name = =“站点名称”>“应用程序名”内容,所以我不需要每次都手动写这个。问题是目前我只能获得当前页标题,比如<%= current_page.data。标题||“站点名称”%>,其中每个布局都不相同,而不是“站点名称”,我希望传递一个全局变量,该变量存储在某个文件中,每个页面都可以访问它。

2 个解决方案

#1


4  

You could use data files. Simply create a file called site.json in the data folder at the root level of your project. You can then access it using the data path.

您可以使用数据文件。简单地创建一个名为site的文件。项目根级别的数据文件夹中的json。然后可以使用数据路径访问它。

For example:

例如:

data/site.json:

数据/ site.json:

{
  "title": "Foo Test"
}

partials/head.html.erb (or any other partial/layout/page)

泛音/ head.html。erb(或任何其他部分/布局/页)

<title><%= current_page.data.title || data.site.title %></title>

I just tested this out and it works for me using Middleman 3.4.

我刚刚测试过这个,它对我使用的是Middleman 3.4。

#2


1  

It's been awhile since I've used Middleman, but I think the way to do this is to use set in config.rb, e.g.:

我已经用过了一段时间了,但是我认为这样做的方法是在config中使用set。rb,例如:

set :site_title, "Site Name"

Then in your template you can access it with the settings object:

然后在您的模板中,您可以使用settings对象访问它:

<%= current_page.data.title || settings.site_title %>

Alternatively, you could create a helper method as described in Custom Extensions, e.g.:

或者,您可以创建自定义扩展中描述的助手方法,例如:

class MyFeature < Middleman::Extension
  SITE_TITLE = "Site Name"

  helpers do
    def page_title
      current_page.data.title || SITE_TITLE
    end
  end
end

After activating the extension in config.rb you could then do this in your template:

在配置中激活扩展之后。rb你可以在模板中这样做:

<%= page_title %>

Then in your template you could just

在你的模板中

#1


4  

You could use data files. Simply create a file called site.json in the data folder at the root level of your project. You can then access it using the data path.

您可以使用数据文件。简单地创建一个名为site的文件。项目根级别的数据文件夹中的json。然后可以使用数据路径访问它。

For example:

例如:

data/site.json:

数据/ site.json:

{
  "title": "Foo Test"
}

partials/head.html.erb (or any other partial/layout/page)

泛音/ head.html。erb(或任何其他部分/布局/页)

<title><%= current_page.data.title || data.site.title %></title>

I just tested this out and it works for me using Middleman 3.4.

我刚刚测试过这个,它对我使用的是Middleman 3.4。

#2


1  

It's been awhile since I've used Middleman, but I think the way to do this is to use set in config.rb, e.g.:

我已经用过了一段时间了,但是我认为这样做的方法是在config中使用set。rb,例如:

set :site_title, "Site Name"

Then in your template you can access it with the settings object:

然后在您的模板中,您可以使用settings对象访问它:

<%= current_page.data.title || settings.site_title %>

Alternatively, you could create a helper method as described in Custom Extensions, e.g.:

或者,您可以创建自定义扩展中描述的助手方法,例如:

class MyFeature < Middleman::Extension
  SITE_TITLE = "Site Name"

  helpers do
    def page_title
      current_page.data.title || SITE_TITLE
    end
  end
end

After activating the extension in config.rb you could then do this in your template:

在配置中激活扩展之后。rb你可以在模板中这样做:

<%= page_title %>

Then in your template you could just

在你的模板中