Мне очень нужна помощь с Джекиллом. Я отчаянно пытаюсь создать страницы, где сообщения группируются по дате.Jekyll - групповые сообщения по дате
Я хочу иметь домашнюю страницу со всеми сообщениями текущего месяца, а затем возможность просматривать каждый месяц. Поэтому мне нужно создать индексную страницу и создать все другие ежемесячные страницы.
Я новичок в рубине и Джекилле, поэтому я действительно изучаю. Моя домашняя страница работает с каким-то уродливым кодом, но мои месячные страницы не работают: содержимое сообщения не «компилируется» и не отображается в сыром текстиле.
Мой класс генератор выглядит следующим образом:
class MonthlyGenerator < Generator
safe true
def group_by_month(posts)
months = []
posts_by_month = {}
posts.reverse.each do |post|
key = Time.utc(post.date.year, post.date.month)
if posts_by_month.has_key?(key)
posts_by_month[key] << post
else
posts_by_month[key] = [post]
months << key
end
end
return [months,posts_by_month]
end
def generate(site)
data = group_by_month(site.posts)
months = data[0]
posts_by_month = data[1]
months.each_with_index do |month, index|
if index >= 0 && index < months.length
nextMonth = months[index+1]
else
nextMonth = false
end
if index > 0 && index <= months.length
previousMonth = months[index-1]
else
previousMonth = false
end
posts = posts_by_month[month]
dir = "/"+month.year.to_s+"-"+month.mon.to_s
write_monthly_posts(site, dir, month, posts, nextMonth, previousMonth)
end
end
def write_monthly_posts(site, dir, month, posts, nextMonth, previousMonth)
monthlypage = MonthlyPage.new(site, dir, month, posts, nextMonth, previousMonth)
monthlypage.render(site.layouts, site.site_payload)
monthlypage.write(site.dest)
site.pages << monthlypage
end
end
И мой шаблон monthly.html:
<div class="span3">
<h1>{{ page.month }}</h1>
</div>
<div class="span9">
{% for post in page.posts %}
<a href="{{ post.url }}">{{ post.title | truncate:65 }}</a>
{{ post.content }}
{% endfor %}
</div>
Выход такой:
<div class="span3">
<h1>Sat Nov 01 00:00:00 UTC 2008</h1>
</div>
<div class="span9">
<a href="/cat1/2008/11/19/test2.html">Test2</a>
h1. test
p(meta). 1nounmjlkjlktest2
2008 is a leap year. That means that three hundred and sixty six days
<iframe class="video" src="http://player.vimeo.com/video/?title=0&byline=0&portrait=0" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>
</div>
содержание по-прежнему текстиль. Любая идея? Спасибо большое! Александр
обновление:
решение:
{{ post.content | textilize }}
не работает, потому что она не учитывает значений учетных записей, установленных в моем файле post.textile.
Решение:
Я нашел кое-что, если я заставляю пост, чтобы сделать в цикле, это делает работу:
def group_by_month(posts, site)
months = [] posts_by_month = {}
posts.reverse.each do |post|
post.render(site.layouts, site.site_payload)
key = Time.utc(post.date.year, post.date.month)
if posts_by_month.has_key?(key)
posts_by_month[key] << post
else
posts_by_month[key] = [post]
months << key
end
end
return [months,posts_by_month]
end
Здравствуйте , Большое спасибо за ваш ответ, я попытался это сделать, но он не учитывает переменные в моем текстильном файле (в папке _post). Это моя проблема ! –
Просто нашел решение. Если я выполняю промежуточную визуализацию сообщения, он работает: 'def group_by_month (сообщения, сайт) months = [] posts_by_month = {} posts.reverse.each do | post |' * * post.render (site.layouts, сайт.site_payload) ** 'ключ = Time.utc (post.date.year, post.date.month) если posts_by_month.has_key? (ключ) posts_by_month [ключ] << опубликовать еще posts_by_month [ключ] = [ сообщение] месяцев << ключ конца конца возвращения [месяцы, posts_by_month] end' –
@AlexandreAssouad, что код будет понятнее, если вы редактируете в свой вопрос (под заголовком «** решения **»). – huon