If you want to print the current article's title just use <%= current_article.title %>
.
Middleman: How can I get the current article title?
题
When generating a simple blog in Middleman using the "middleman-blog" extension I get a the layout file which simply yields the content of the post.
What I want is to get the title of the current post and display that.
What I have right now:
<% blog.articles.each do |article| %>
<%= link_to article.title, article %>
<% end %>
This loops through every post title even though only the single post content is displayed. So it outputs something like this for the url /post-title-one
Post Title One Post Title Two Post Title Three
"only the content of post title one"
I want to try something like
<% blog.articles.each do |article| %>
<%= link_to current_article.title, article %>
<% end %>
But it just randomly spits out two page titles.
解决方案
其他提示
I'm not quite sure what you're asking. The first block of code you gave is intended to iterate through each article in the blog and generate a link where the link text is the article's title and the target is the article. I would assume that you want to simply link to the current article, since you're on its content page.
<%= link_to current_article.title, current_article %>
That should get the job done.