CKAN Simple Template Extends

在 Add a new tab, controller, page on CKAN 有提到如何在CKAN header新增一個分頁
此分頁繼承於CKAN原有的 page.html 的頁面,叫做 helloCheif.html
今天要做的事情很簡單
如何在子tab間繼承 helloCheif.html
只對修改 mycontent block,結果如下
程式碼如下
cooking/helloCheif.html
{% extends "page.html" %}

{% block styles %}
  {{ super() }}
  <link rel="stylesheet" href="/helloChief.css" />
{% endblock %}

{% block content_primary_nav %}
  {{ h.build_nav_main(
    ('browseData', _('Browse Data')),
    ('addData', _('Add Data')),
    ('cleanData', _('Data Clean')),
    ('mergeData', _('Data Merge'))
  ) }}
{% endblock %}

{% block primary_content %}
  {{ super() }}
  {% block my_content %}
    <h2>{{ _('Choose action you want.') }}</h2>
    <br><br><br><br><br><br><br><br><br><br>
  {% endblock %}
{% endblock %}

{% block secondary %}
  {{ super() }}
  {% block my_sidebar_module %}
    {% snippet "snippets/display-ingredient-module.html" %}
  {% endblock %}
{% endblock %}

{% block breadcrumb %}
  <h2>{{ _('Data Kitchen') }}</h2>
{% endblock %}
content_primary_nav block 裡面新增了四個tab: browseData, addData, cleanData, mergeData
子分頁主要要取代 my_content block
helloChief.css 裡面考量排版,只是把一個還用不到的element移除就不特別提
plugin.py
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit


class YtdataservicePlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.IConfigurer)
    plugins.implements(plugins.IRoutes)

    # IConfigurer

    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'ytdataservice')

    # IRoutes
    def before_map(self, map):
        map.connect('cookData','/cookingData', controller='ckanext.ytdataservice.controllers:cookDataController', action='firstPage')
        map.connect('browseData','/cookingData/browseData', controller='ckanext.ytdataservice.controllers:cookDataController', action='browseData')
        map.connect('addData','/cookingData/addData', controller='ckanext.ytdataservice.controllers:cookDataController', action='addData')
        map.connect('cleanData','/cookingData/cleanData', controller='ckanext.ytdataservice.controllers:cookDataController', action='cleanData')
        map.connect('mergeData','/cookingData/mergeData', controller='ckanext.ytdataservice.controllers:cookDataController', action='mergeData')
        return map

    def after_map(self, map):
        return map

唯一與上篇文章不同的是 before_map 新增了四個 Routing rule,並且設定了不同的action
這邊提醒一下,第二個參數 /cookingData/browseData
可以為任意url,看需求來設計
就算是完全不同的url也可以
放在母分類底下表示他們有階層式的關聯
至於如何在這當中帶參數之後再談
controllers.py
import ckan.lib.base as base

class cookDataController(base.BaseController):
    def firstPage(self):
        return base.render('cooking/helloChief.html')

    def browseData(self):
        return base.render('cooking/browseData.html')

    def addData(self):
        return base.render('cooking/addData.html')

    def cleanData(self):
        return base.render('cooking/cleanData.html')

    def mergeData(self):
        return base.render('cooking/mergeData.html')

這邊也是很單純的新增四個對應的 request action
cooking/addData.html (其他三個 browseData.html,mergeData.html, cleanData.html同理)
{% extends "cooking/helloChief.html" %}

{% block my_content %}
    <h2>{{ _('Add data page') }}</h2>
    <br><br><br><br><br><br><br><br><br><br>
{% endblock %}
完成之後,重啟Server就完成子分頁繼承母分頁後的行為
接著就可以在各自的分頁裡,處理各自的功能
使用自己的 javascript, css操作等等
共用的就拉到 helloChief.html 來做

留言