サイトマップ用テンプレートの作成 [Plone3.3]

サイトマップ用に指定したフォルダ内の index.html ページとフォルダタイプ一覧(キーワードの指定があればそのキーワードが指定されたフォルダのみ)を表示するマクロをテンプレートとして作成する。実際に利用するときはこれをテンプレートから複数呼び出し、並べて表示してサイトマップとする。

テンプレートの作成

サイトマップ用マクロ

テンプレート eng_sitemap を次のように作成し、テンプレート内にマクロ eng_sitemap を定義する。

<html xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      i18n:domain="plone">
<body>

<dl metal:define-macro="eng_sitemap"
    tal:define="query path;
                subject subject | nothing;
                subject python:test(subject, subject, '');
                root python:context.portal_catalog.searchResults(
                                   portal_type='Folder',
                                   path = { 'query' : query, 'depth' : 0,});
                results1 python:context.portal_catalog.searchResults(
                                   id = 'index.html',
                                   portal_type=['Document','Topic'],
                                   path = { 'query' : query, 'depth' : 1,},);
                results2 python:context.portal_catalog.searchResults(
                                   portal_type='Folder',
                                   path = { 'query' : query, 'depth' : 1,},
                                   Subject = subject,
                                   sort_on = 'getObjPositionInParent');
                results python:results2 and results1+results2 or results2;"
    tal:condition="python:root and results" tal:omit-tag="">
    <div class="s-menu"
         tal:define="title python:root[0].Title;">
    <div class="s-menu-head">
    <h2 tal:content="title">title</h2>
    </div>

    <ul class="s-menu">
    <tal:items tal:repeat="item results">
        <li>
            <a tal:attributes="href string:${item/getURL};
                               class python:'sitemap-' + item.id;
                               title string:${item/Description}">
            <span tal:replace="item/pretty_title_or_id">Title</span>
            </a>
        </li>
    </tal:items>
    </ul>
    </div>
</dl>

</body>
</html>

Topicタイプのビューとして ENG SITEMAP VIEW を定義する

工学研究科のサイトマップ表示用に、Topicタイプのビューを定義する。スキンフォルダ内にテンプレートを eng_sitemap_view として作成する。タイトルは「ENG SITEMAP VIEW」とする。なお「ナビゲーションに表示しない」と設定したフォルダは表示されないようにする。また、コレクションのサブフォルダで検索された項目についてもサイトマップの項目として表示する。

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      lang="en"
      metal:use-macro="here/main_template/macros/master"
      i18n:domain="plone">

<body>
<div metal:fill-slot="main">
<metal:main_macro define-macro="main">
    <metal:body define-macro="body_macro"
                tal:define="kssClassesView context/@@kss_field_decorator_view;
                            getKssClasses nocall:kssClassesView/getKssClassesInlineEditable;
                            templateId template/getId">

        <div tal:replace="structure provider:plone.abovecontenttitle" />

        <h1 class="documentFirstHeading"> 
            <metal:field use-macro="python:here.widget('title', mode='view')">
            Title
            </metal:field>
        </h1>

        <div tal:replace="structure provider:plone.belowcontenttitle" />

        <p class="documentDescription">
            <metal:field use-macro="python:here.widget('description', mode='view')">
            Description
            </metal:field>
        </p>

        <div tal:replace="structure provider:plone.abovecontentbody" />

        <div metal:define-macro="text-field-view"
             id="parent-fieldname-text" class="stx"
             tal:define="kss_class python:getKssClasses('text',
                         templateId=templateId, macro='text-field-view');
                         text here/getText|nothing"
             tal:condition="text"
             tal:attributes="class python:test(here.Format() in ('text/structured',
                                                   'text/x-rst', ), 'stx' + kss_class, 'plain' + kss_class)">
            <div metal:define-slot="inside" tal:replace="structure text">The body</div>
        </div>

        <div tal:attributes="class string:sitemap sitemap-${context/aq_parent/id}"
             tal:define="sitemapContents python:here.queryCatalog(sort_on = 'getObjPositionInParent',
                                                                  portal_type = 'Folder',);">
          <div tal:repeat="sitemapContent sitemapContents"
               tal:attributes="class python:test(repeat['sitemapContent'].odd(), 'sitemap-odd', 'sitemap-even');
                               id string:sid-${sitemapContent/id};">
            <div tal:define="path sitemapContent/getPath" tal:omit-tag=""
                 tal:condition="not: sitemapContent/exclude_from_nav">
              <div metal:use-macro="here/eng_sitemap/macros/eng_sitemap" />
            </div>
          </div>
          <div class="visualClear"><!-- --></div>
        </div>

        <div tal:define="results python:context.portal_catalog.searchResults( path = {'query' : '/'.join(context.getPhysicalPath()), 'depth' : 1,} );"
             tal:omit-tag="">
        <div tal:repeat="result results">
          <div tal:attributes="class string:sub-sitemap-${result/id}"
               tal:define="sitemapContents python:result.getObject().queryCatalog( sort_on = 'getObjPositionInParent', portal_type = 'Folder',);">
            <div tal:content="result/Title" tal:attributes="class string:title-${result/id}" />
            <div tal:repeat="sitemapContent sitemapContents"
                 tal:attributes="class python:test(repeat['sitemapContent'].odd(), 'sitemap-odd', 'sitemap-even');
                                 id string:sid-${sitemapContent/id};">
              <div tal:define="path sitemapContent/getPath" tal:omit-tag=""
                   tal:condition="not: sitemapContent/exclude_from_nav">
                <div metal:use-macro="here/eng_sitemap/macros/eng_sitemap" />
              </div>
            </div>
            <div class="visualClear"><!-- --></div>
          </div>
        </div>
        </div>

        <div class="visualClear"><!-- --></div>

    </metal:body>

    <div tal:replace="structure provider:plone.belowcontentbody" />

</metal:main_macro>
</div>
</body>
</html>

ENG SITEMAP VIEWのキーワード対応

サイトマップに表示される各項目をキーワードで絞れるようにする。eng_sitemap_view を次のように変更する。

  tal:define="sitemapContents python:here.queryCatalog(sort_on = 'getObjPositionInParent',
-                                                      portal_type = 'Folder',);">
+                                                      portal_type = 'Folder',);
+             subject context/eng_sitemap_keyword | nothing">

この機能を使うにはZMIでコレクションのプロパティに次のように指定すれば良い。

Name : eng_sitemap_keyword
Type : String
Value : 指定するキーワード

Topicのビューで選択できるものを指定する

フォルダのビューで選択できるものを指定する。上で作成した、eng_sitemap_view も設定できるようにする。portal_types/Topic を開き Available view methods に次の行を追加する。

eng_sitemap_view

 

テンプレートの利用方法

ENG SITEMAP VIEW を利用せず、表示する項目を指定することもできる。利用方法はフォルダへの path と、キーワード(指定しなければすべて)を定義して、次のように呼び出せば良い。

<div tal:define="path python:'/ja/information';
                 python:context.portal_url.getPortalPath() + path;
                 subject python:['受験生の方',]">
  <div metal:use-macro="here/eng_sitemap/macros/eng_sitemap" />
</div>

または、

<div tal:define="path python:'/ja/information';
                 python:context.portal_url.getPortalPath() + path;">
  <div metal:use-macro="here/eng_sitemap/macros/eng_sitemap" />
</div>