GETモジュールから表示ページがもつIDを参照する

ページ情報を取得する

URLコンテキスト(参照)で表されるページ情報のうち各種IDをGETモジュール内で取得する方法を説明します。

GETモジュール内でページ情報のうち、例えばエントリーID(以下EID)を取得することができれば、そのモジュールがどのエントリーから呼び出されたかによって参照する情報を変えることができます。基本的には、Entry_BodyやEntry_Headlineなどのモジュールもそのようにページ情報を取得することで、表示すべき情報を決定しデータベースを参照しています。

スコープの設定

下記のようにプロパティを設定することで、$this->bid, $this->cid, $this->eidとして、表示中のページが持つID情報を取得できます。

public $_scope = array(
    'bid' => 'global',
    'cid' => 'global',
    'eid' => 'global',
);

表示中ページの各種IDを参照するサンプル

<?php

namespace Acms\Custom\GET;

use ACMS_GET;

class Hoge extends ACMS_GET
{
    public $_scope = array(
        'bid' => 'global',
        'cid' => 'global',
        'eid' => 'global',
    );

    function get()
    {
        $string[] = 'Global Vars';
        $string[] .= "BID = $this->bid";
        $string[] .= "CID = $this->cid";
        $string[] .= "EID = $this->eid";

        return nl2br(implode("\r\n", $string));
    }
}

出力結果

<!-- bid = 1, cid = 2, eid = 5 の場合 -->
Global Vars<br />
BID = 1<br />
CID = 2<br />
EID = 5

取得できる情報

GETモジュールで現在表示中のページより取得できる情報には以下のものがあります。

プロパティ説明
bidブログIDhttp://sample.com/blog/
uidユーザーIDhttp://sample.com/uid/1/profile.html
cidカテゴリーIDhttp://sample.com/news/
eidエントリーIDhttp://sample.com/news/123.html
keywordキーワードhttp://sample.com/keyword/検索文字/
http://sample.com/?keyword=検索文字
tagタグhttp://sample.com/tags/aaa/
fieldフィールドhttp://sample.com/field/station/あの駅/
start検索の始まり日時http://sample.com/2017-01-01/-/2017-03-03/
end検索の終わり日時http://sample.com/2017-01-01/-/2017-03-03/
pageページ番号http://sample.com/page/2/
order並び順http://sample.com/order/datetime-desc/

テンプレートエンジン - 1(基礎)

a-blog cmsのテンプレートエンジン

a-blog cmsは独自のテンプレートエンジンを備え、標準のGETモジュールはすべてこのテンプレートエンジンを通して実装されています。ここではそのTemplateクラスの使用方法を説明します。

Templateクラス

Templateクラスはテンプレートをもとに、与えられたデータに応じて情報を加工して出力します。

$Tpl = new Template($this->tpl);

$this->tpl

$this->tpl には、テンプレート上における、BEGIN_MODULE〜END_MODULEまでの間の文字列が自動で格納されています。

<html>
<body>
    <!-- BEGIN_MODULE Sample_UseTpl -->
    <!-- ここから -->
    <p>{hoge} and {fuga}</p>
    <!-- BEGIN foo --><p>{bar}</p><!-- END foo -->
    <!-- ここまでが$this->tplには入ってくる -->
    <!-- END_MODULE Sample_UseTpl -->
</body>
</html>

addメソッド

addメソッドは、ブロックや変数をテンプレートに加えるメソッドです。

  • ブロック: ある範囲を表示するためや、繰り返し(ループ)をする テンプレート要素
  • 変数: 動的に表示を変わってくるテンプレート要素

サンプル

// get module
$Tpl = new Template($this->tpl);

// ブロックの追加
$Tpl->add('notFound');

// 変数の追加
$Tpl->add(null [
  'hoge' => 'hello world.'
]);

// html
<h1>{hoge}</h1>

<!-- ブロックの追加がされていないと以下の表示はされない -->
<!-- BEGIN notFound -->404 Not Found<!-- END notFound -->

getメソッド

getメソッドは、加えられた情報をもとにテンプレート処理を解決し、その結果の文字列を取得するメソッドです。通常はreturnするときにそのまま呼び出します。

サンプル

function get()
{
  $Tpl = new Template($this->tpl);

  $Tpl->add('notFound');
  $Tpl->add(null, [
    'hoge' => 'hello world.'
  ]);

  return $Tpl->get();
}

ACMS_Correctorクラス (校正オプションの利用)

モジュールのテンプレート内で校正オプションを利用する場合は、ACMS_Correctorクラスのインスタンスを同時に渡します。 これにより以下のように変数に対して校正オプションを利用できるようになります。

サンプル

$Tpl = new Template($this->tpl, new ACMS_Corrector());

$Tpl->add(null, [
  'hoge' => date('Y-m-d H:i:s'),
]);

// html
<p>{hoge}[datetime('Y年m月d日 H時i分)]</p>

// 表示
<p>2017年01月01日 12時59分</p>

全体のサンプル(まとめ)

テンプレート(HTML形式)

<html>
<body>
    <!-- BEGIN_MODULE Sample_UseTpl -->
    <p>{hoge} and {fuga}</p>
    <!-- BEGIN foo --><p>{bar}</p><!-- END foo -->
    <!-- END_MODULE Sample_UseTpl -->
</body>
</html>

Sample_UseTplモジュール

<?php

namespace Acms\Custom\GET\Sample;

use ACMS_GET;
use Template;
use ACMS_Corrector;

/*
 * path: extension/acms/GET/Sample/UseTpl.php
 */
class UseTpl extends ACMS_GET
{
  function get()
  {
    // Templateクラス
    $Tpl  = new Template($this->tpl, new ACMS_Corrector());

    // <!-- BEGIN foo --> ~ <!-- END foo --> の中の {bar} にテキストを代入
    $Tpl->add('foo', array('bar' => 'fooブロックの中のbar変数'));

    // ブロックを介さない、モジュール直下の変数 {hoge} と {fuga} にテキストを代入
    $Tpl->add(null, [
      'hoge' => 'ほげ', 'fuga' => 'ふが'
    ]);

    // テンプレートを処理済みの文字列としてreturn
    return $Tpl->get();
  }
}

実行結果

<html>
<body>
    <p>ほげ and ふが</p>
    <p>fooブロックの中のbar変数</p>
</body>
</html>