42 lines
1.1 kB
1
defmodule BlogWeb.Layouts do
2
@moduledoc """
3
This module holds different layouts used by your application.
4
5
See the `layouts` directory for all templates available.
6
The "root" layout is a skeleton rendered as part of the
7
application router. The "app" layout is set as the default
8
layout on both `use BlogWeb, :controller` and
9
`use BlogWeb, :live_view`.
10
"""
11
use BlogWeb, :html
12
alias BlogWeb.Presence
13
14
embed_templates "layouts/*"
15
16
def recent_posts do
17
Blog.Content.Post.all()
18
|> Enum.map(fn post ->
19
{post.title, format_date(post.written_on), post.slug}
20
end)
21
end
22
23
def posts_by_tag do
24
Blog.Content.Post.all()
25
|> Enum.flat_map(fn post ->
26
Enum.map(post.tags, fn tag -> {tag.name, {post.title, post.slug}} end)
27
end)
28
|> Enum.group_by(
29
fn {tag_name, _} -> tag_name end,
30
fn {_, post_info} -> post_info end
31
)
32
|> Enum.map(fn {tag_name, posts} -> {tag_name, Enum.take(posts, 2)} end)
33
end
34
35
def total_readers do
36
Presence.list("blog_presence") |> map_size()
37
end
38
39
defp format_date(datetime) do
40
Calendar.strftime(datetime, "%B %d, %Y")
41
end
42
end
43