LoginSignup
3
2

More than 5 years have passed since last update.

【Memo】MTタグでMTAppjQueryのJSONTableに孫オブジェクトがある場合の取得方法

Last updated at Posted at 2016-11-20

以下のようなJSONデータをmt:loopタグを使って出力するのですが、itemsの子オブジェクトは簡単に取得できるが孫はどうやって取る??という素朴な疑問にぶち当たったのでメモとして残しておきます。

MTAppjQueryのJSONTable実装で得た知見になります。

  • MTAppjQueryバージョン:v1.9.0
{
  "items": [
    {
      "number": "01",
      "imagePath": {
        "id": "1",
        "url" "/hoge/images/dummy.jpg"
      },
      "description": "テキストテキスト"
    },
    {
      "number": "02",
      "imagePath": {
        "id": "2",
        "url" "/hoge/images/dummy.jpg"
      },
      "description": "テキストテキスト"
    },
    {
      "number": "03",
      "imagePath": {
        "id": "3",
        "url" "/hoge/images/dummy.jpg"
      },
      "description": "テキストテキスト"
    }
  ]
}

やりたいこと

  • imagePathのurlを取得し出力させたい
  • カスタムフィールド名:entry_gallery(textarea)

実装方法

  • 子オブジェクトに対してjson_decodeを定義して、孫のキーを取得して変数に格納
  • 孫のキーの変数をHTML部分で出力させる
<mt:entry_gallery json_decode="1" setvar="json">  // カスタムフィールドをjson_decodeを定義(MTAppjQueryのモディファイア:配列・ハッシュ変数に変換)し変数に格納
<mt:Var name="json" key="items" setvar="items">  // itemsのキーを変数itemsに格納
<mt:SetVar name="items_index" value="0">  // items_indexに初期値を格納

// ループ処理
<mt:Loop name="items">
<mt:Var name="items" index="$items_index" setvar="item">
<mt:Var name="item" key="imagePath" setvar="ImagePathHash">  // itemのimagePathを変数に格納
<mt:If name="ImagePathHash">
  <mt:Var name="ImagePathHash" json_decode="1" setvar="jsonImagePath">  // imagePathに対してjson_decodeを定義し変数に格納
  <mt:Var name="jsonImagePath" key="url" setvar="ImagePath_url">  // キーのurlを取得し変数格納
</mt:If>

// メインのHTMLループ部分
<mt:If name="__first__">
<ul>
</mt:If>
  <li>
    <img src="<mt:Var name="ImagePath_url" />" alt="<mt:Var name="item" key="description">"/>
  </li>
<mt:If name="__last__">
</ul>
</mt:If>


<mt:SetVar name="items_index" op="++">  // items_indexの値をインクリメント
</mt:Loop>
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2