EC-CUBEにて、URL単位でサイドバー表示ブロックを切り替える
EC-CUBEでは管理画面上でページ単位にサイドバーに表示するブロックを切り替えれるのが便利だけど表示して欲しいページでサイドバーが表示されない場合がある。
なので今回は前回と同様にサイドバーブロックを用意してブロック内でSmartyの条件式をごりごりしてサイドバーの表示条件と内容を変更。
まずはサイドバー(今回は左限定)の出力をしてるテンプレートを修正。
ファイル:data/Smarty/templates/default/site_main.tpl
<!--{if $arrPageLayout.LeftNavi|@count > 0}--> <div id="side"> <h1><a href="<!--{$smarty.const.SITE_URL}-->"><img src="<!--{$TPL_DIR}-->img/logo.jpg" alt="<!--{$arrSiteInfo.shop_name|escape}-->/<!--{$tpl_title|escape}-->" width="200" height="280" /></a></h1> <div class="side_contents"> <!--{* ▼左ナビ *}--> <!--{foreach key=LeftNaviKey item=LeftNaviItem from=$arrPageLayout.LeftNavi}--> <!-- ▼<!--{$LeftNaviItem.bloc_name}--> ここから--> <!--{if $LeftNaviItem.php_path != ""}--> <!--{include_php file=$LeftNaviItem.php_path}--> <!--{else}--> <!--{include file=$LeftNaviItem.tpl_path}--> <!--{/if}--> <!-- ▲<!--{$LeftNaviItem.bloc_name}--> ここまで--> <!--{/foreach}--> <!--{* ▲左ナビ *}--> </div> </div> <!--{/if}-->
を
<!--{include file="`$smarty.const.USER_TEMPLATE_PATH``$smarty.const.TEMPLATE_NAME`/`$smarty.const.BLOC_DIR`side.tpl"}-->
に変更。
で、管理画面上から「新規ブロック作成」でサイドバー用のブロックを作成。さっきテンプレートファイル変更時にインクルードファイルの名称を「site.tpl」としたのでここで指定するファイル名も「site.tpl」。
ファイルの内容は以下のような感じ。条件式や出力内容はご自由に。
<!--{if $smarty.server.REQUEST_URI|regex_replace:"/^\/mypage\/login.php.*/":"match" == "match" || $smarty.server.REQUEST_URI|regex_replace:"/^\/cart\/index.php.*/":"match" == "match" || $smarty.server.REQUEST_URI|regex_replace:"/^\/shopping\/index.php.*/":"match" == "match" || $smarty.server.REQUEST_URI|regex_replace:"/^\/shopping\/payment.php.*/":"match" == "match" || $smarty.server.REQUEST_URI|regex_replace:"/^\/shopping\/confirm.php.*/":"match" == "match" || $smarty.server.REQUEST_URI|regex_replace:"/^\/shopping\/complete.php.*/":"match" == "match" || $smarty.server.REQUEST_URI|regex_replace:"/^\/shopping\/deliv.php.*/":"match" == "match" }--> <div id="side"> <h1><a href="<!--{$smarty.const.SITE_URL}-->"><img src="<!--{$TPL_DIR}-->img/logo.jpg" alt="<!--{$arrSiteInfo.shop_name|escape}-->/<!--{$tpl_title|escape}-->" width="200" height="280" /></a></h1> <div class="side_contents"> <!--{include file="`$smarty.const.USER_TEMPLATE_PATH``$smarty.const.TEMPLATE_NAME`/`$smarty.const.BLOC_DIR`mailmagazine.tpl"}--> <!--{include file="`$smarty.const.USER_TEMPLATE_PATH``$smarty.const.TEMPLATE_NAME`/`$smarty.const.BLOC_DIR`kodawari.tpl"}--> <!--{include file="`$smarty.const.USER_TEMPLATE_PATH``$smarty.const.TEMPLATE_NAME`/`$smarty.const.BLOC_DIR`mobileqr.tpl"}--> <!--{include file="`$smarty.const.USER_TEMPLATE_PATH``$smarty.const.TEMPLATE_NAME`/`$smarty.const.BLOC_DIR`banner.tpl"}--> <!--{include_php file="`$smarty.const.HTML_PATH`frontparts/bloc/calendar.php"}--> </div> </div> <!--{else}--> <!--{if $arrPageLayout.LeftNavi|@count > 0}--> <div id="side"> <h1><a href="<!--{$smarty.const.SITE_URL}-->"><img src="<!--{$TPL_DIR}-->img/logo.jpg" alt="<!--{$arrSiteInfo.shop_name|escape}-->/<!--{$tpl_title|escape}-->" width="200" height="280" /></a></h1> <div class="side_contents"> <!--{* ▼左ナビ *}--> <!--{foreach key=LeftNaviKey item=LeftNaviItem from=$arrPageLayout.LeftNavi}--> <!-- ▼<!--{$LeftNaviItem.bloc_name}--> ここから--> <!--{if $LeftNaviItem.php_path != ""}--> <!--{include_php file=$LeftNaviItem.php_path}--> <!--{else}--> <!--{include file=$LeftNaviItem.tpl_path}--> <!--{/if}--> <!-- ▲<!--{$LeftNaviItem.bloc_name}--> ここまで--> <!--{/foreach}--> <!--{* ▲左ナビ *}--> </div> </div> <!--{/if}--> <!--{/if}-->
上記例では任意のURLの場合に指定したブロックを表示するようにし、その他の場合は通常通りに管理画面でページ単位に設定したブロックを表示するようしています。条件式が正規表現なのはURL末尾にGETパラメタが不可する場合がある為。
「<div class="side_contents">」中にて読み込み指定しているテンプレートブロックですが、システムデフォルトはテンプレートファイルパス、管理者追加ブロックはPHPファイルパスを指定しています。
で、前回同様に追加したサイドバー用ブロックは通常のブロックと同様に扱われます。
以上!