取り敢えず、使えそうな感じにはなった。他にいい方法無いのか?
現在、category, tag, layout についてはテストできていない。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
{# body class を生成 #} {% set body_plink = "" %} {% if page is defined %} {% set body_plink = page.permalink %} {% elif section is defined %} {% set body_plink = section.permalink %} {% endif %} {% set body_classes = [] %} {# permalink を分解して path[0] を取得 #} {% set plink_parts = body_plink | split(pat="/") %} {% set path0 = plink_parts | nth(n=3) | default(value="") %} {% if path0 != "" %} {% set body_classes = body_classes | concat(with=["path0-" ~ path0]) %} {% else %} {% set body_classes = body_classes | concat(with=["home"]) %} {% endif %} {# slug によるクラス #} {% if section is defined and section.slug is defined %} {% set body_classes = body_classes | concat(with=["section-" ~ section.slug]) %} {% elif page is defined and page.slug is defined %} {% set body_classes = body_classes | concat(with=["page-" ~ page.slug]) %} {% endif %} {# カテゴリクラスを追加(例: category-news) #} {% if page is defined and page.taxonomies is defined %} {% for cat in page.taxonomies.categories | default(value=[]) %} {% set body_classes = body_classes | concat(with=["category-" ~ cat]) %} {% endfor %} {% endif %} {# タグクラスを追加(例: tag-web, tag-dev) #} {% if page is defined and page.taxonomies is defined %} {% for tag in page.taxonomies.tags | default(value=[]) %} {% set body_classes = body_classes | concat(with=["tag-" ~ tag]) %} {% endfor %} {% endif %} {# レイアウト(テンプレート)クラスを追加(例: layout-page) #} {% if page is defined and page.template is defined %} {% set body_classes = body_classes | concat(with=["layout-" ~ page.template]) %} {% elif section is defined and section.template is defined %} {% set body_classes = body_classes | concat(with=["layout-" ~ section.template]) %} {% endif %} {# HTML 出力 #} <body class="{{ body_classes | join(sep=' ') }}"> |
—