Tableau.TagExtension (tableau v0.24.0)

View Source

Creates pages for tags found in posts created by the Tableau.PostExtension.

The :tags key provided on every page in the assigns is described by tags/0.

The @page assign passed to the layout provided in the configuration is described by page/0.

Configuration

  • :enabled - boolean - Extension is active or not.
  • :layout - module - The Tableau.Layout implementation to use.
  • :permalink - string - The permalink prefix to use for the tag page, will be joined with the tag name.

Layout and Page

To take advantage of tag extension, you'll need to define a layout that will render each "tag page" and a normal Tableau.Page that lists all tags on your site.

Layout to render a tag page

defmodule MySite.TagLayout do
  use Tableau.Layout, layout: MySite.RootLayout

  def template(assigns) do
    ~H"""
    <div>
      <h1>Tag: #{@page.tag}</h1>

      <ul>
        <li :for={post <- @page.posts}>
          <a href={post.permalink}> {post.title}</a>
        </li>
      </ul>
    </div>
    """
  end
end

Page to render all tags

This example page shows listing all takes, sorting them by the number of posts for each tag.

defmodule MySite.TagPage do
  use Tableau.Page,
    layout: MySite.RootLayout,
    permalink: "/tags",
    title: "Tags"


  def template(assigns) do
    sorted_tags = Enum.sort_by(assigns.tags, fn {_, p} -> length(p) end, :desc)
    assigns = Map.put(assigns, :tags, sorted_tags)

    ~H"""
    <div>
      <h1>Tags</h1>

      <ul>
        <li :for={{tag, posts} <- @tags}>
          <a href={tag.permalink}>tag.tag</a>

          <span>- {length(posts)}</span>
        </li>
      </ul>
    </div>
    """
  end
end

Summary

Types

page()

@type page() :: %{
  title: String.t(),
  tag: String.t(),
  permalink: String.t(),
  posts: [Tableau.PostExtension.post()]
}

tag()

@type tag() :: %{title: String.t(), tag: String.t(), permalink: String.t()}

tags()

@type tags() :: %{required(tag()) => [Tableau.PostExtension.post()]}