我的Middleman-built website I have stored links and other information about all pages in a data file。
data/pages.yaml
:
pageA:
link: /some-long-url-subject-to-change.html
name: PageA name
info: Some other related info
然后,在我的HAML模板(source/test.haml
)中,我可以使用= data.pages.pageA.link
打印pageA的相对路径。
现在,我想使用markdown语法按名称(pageA)引用该页面。
示例(source/test.html.haml
):
.info
:markdown
This is some text with a [manual link](https://google.com) to somewhere.
This is another text with a [data-referenced link](pageA) to that page.
以与第一个"手动链接相同的方式"链接到谷歌,我想第二个链接使用存储在数据文件中的相对路径来创建链接。我认为解决此问题的一个解决方案是在标记下来之前用(pageA)
评估= data.pages.pageA.link
替换(pageA)
文本。
我认为这可以通过创建自定义助手来实现,但我无法确定它。
我尝试编写一个自定义帮助程序,用= data.pages.pageA.link
文本替换pageA
文本,然后通过降价显示它。
我能够用来自数据的信息替换特定文本(data.pages.pageA.link
),我还能够编写更通用的案例,用典型数据引用的显式文本替换所有数据引用。但我无法在通用案例中替换= data.pages.pageA.link
来评估# Replace specific text with information from ``data/pages.yaml``
specific = text.gsub("pageA",data.pages.pageA.link)
# Generic case: using explicit text
generic = text.gsub(/\]\((.*?)\)/,'](data.pages.\1.link)')
# Generic case: trying to use variable name, but getting explicit text
generic = text.gsub(/\]\((.*?)\)/,'](#{data.pages.\1.link})')
。
我的帮手:
test.html.haml
在= myhelper("This is another text with a [data-referenced link](pageA) to that page.")
中使用帮助器:
specific
打印/some-long-url-subject-to-change.html
变量可以提供我想要的内容(generic
)。但是打印>>> import numpy as np
>>> array = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
>>>
>>> column = 5
>>> print(array.reshape(len(array)/column, column))
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]]
>>>>>> column = 10
>>> print(array.reshape(len(array)/column, column))
[[ 1 2 3 4 5 6 7 8 9 10]
[11 12 13 14 15 16 17 18 19 20]]
变量会产生纯文本,而不是数据文件中的信息。
我可能缺乏一些基本的Ruby知识,解决方案确实非常简单。
答案 0 :(得分:1)
我不确定你到底遇到了什么。以下是我的结论:
假设您的数据文件data/pages.yaml
包含
pageA:
link: /some-long-url-subject-to-change.html
name: PageA name
info: Some other related info
您的模板test.haml.md.erb
看起来像
---
title: Test
---
This is another text with a [<%= data.pages.pageA.name %>](<%= data.pages.pageA.link %>) to that page.
输出应如下所示, PageA名称是可点击链接。
这是另一个带有 PageA名称的文字。
您似乎遇到了降价解释的问题。尝试将模板文件名从test.haml
调整为test.haml.md.erb
。
这种结构告诉Middleman首先解释ERB位,然后是Markdown位,最后是HAML(如果不使用HAML,则为纯HTML)。
答案 1 :(得分:1)
It looks as if you are trying to write your own templating system, which is probably not the best idea. First, you'll need to write a regex that properly parses Markdown and finds link notation. Then you need to eval the string you pulled to get the value from your data. And finally you'll need to substitute the placeholder with that value. I more or less got that working with this code in config.rb:
helpers do
def myhelper(text)
page=/\]\((.*?)\)/.match(text)[1]
link=eval( "data.pages.#{page}.link" )
text.gsub(page,link)
end
end
But please don't use this code! It's fragile and error-prone. Worse, there's a much simpler way to do what you are trying to do. Replace your test.haml with test.haml.erb:
.info
:markdown
This is some text with a [manual link](https://google.com) to somewhere.
This is another text with a [data-referenced link](<%= data.pages.pageA.link %>) to that page.
= myhelper("This is another text with a [data-referenced link](pageA) to that page.")
The .erb extension tells Middleman to treat the file as an ERB template. In particular, it'll evaluate whatever is between <%=
and %>
as Ruby code. That means you can skip using a messy helper method. As a bonus, you don't need to write more code to get the other bits of data:
<%= data.pages.pageA.name %>
=> 'PageA name'<%= data.pages.pageA.info %>
=> 'Some other related info'If you need to do some more complex processing, such as iterating over a list, you can do that right in the template itself. It's easier to read and maintain that way.
I also included a call to the helper method as contrast. It's a lot less clear what's going on than just using the existing templating system. Plus, you are bound to forget to wrap all the relevant strings in the method and end up with broken links down the road.
As Adam suggested, you might be even better off using Markdown directly and skipping Haml.