Yii 2.0/テーマ

Yii2では、テーマを使用してアプリケーションの外観(ビュー)を簡単にカスタマイズできます。テーマは、デフォルトのビューをオーバーライドする方法を提供し、異なるレイアウトやスタイルを適用するために便利です。

概要

テーマの構成

Yii2のテーマは主に以下の3つで構成されています:

テーマのディレクトリ構造

通常、テーマは @app/themes に配置されます。例えば、以下のような構造です:

/themes
    /my-theme
        /layouts
            main.php
        /site
            index.php
            about.php
        /assets
            /css
            /js

テーマの設定

テーマを使用するには、アプリケーション構成ファイル(config/web.php)で設定します。

例: テーマの設定

'components' => [
    'view' => [
        'theme' => [
            'pathMap' => [
                '@app/views' => '@app/themes/my-theme',
            ],
            'baseUrl' => '@web/themes/my-theme',
        ],
    ],
],

テーマでビューをオーバーライドする

例えば、views/site/index.php をカスタマイズしたい場合、テーマディレクトリに同じ構造を作成します。

/themes/my-theme/site/index.php

このファイルが優先され、アプリケーションで使用されます。

アセットバンドルのカスタマイズ

テーマに合わせてCSSやJavaScriptを変更したい場合、AppAsset を拡張または置き換えます。

例: アセットバンドルの変更

namespace app\themes\mytheme\assets;

use yii\web\AssetBundle;

class ThemeAsset extends AssetBundle
{
    public $basePath = '@webroot/themes/my-theme/assets';
    public $baseUrl = '@web/themes/my-theme/assets';
    public $css = [
        'css/style.css',
    ];
    public $js = [
        'js/script.js',
    ];
    public $depends = [
        'app\assets\AppAsset',
    ];
}

アセットを登録する際に、このテーマ用アセットバンドルを指定します。

複数テーマの使用

複数テーマを切り替えたい場合は、動的に設定できます。

例: 動的テーマの切り替え

\Yii::$app->view->theme = new \yii\base\Theme([
    'pathMap' => ['@app/views' => '@app/themes/another-theme'],
    'baseUrl' => '@web/themes/another-theme',
]);

コントローラやアクションで設定すれば、特定のページのみ異なるテーマを適用できます。

具体例

現在のヘッダーを左サイドバーに変更する例を具体的に解説します。

テーマを作成する

まず、テーマのディレクトリを作成します。

ディレクトリ構造
以下のように、themes/my-theme を作成します。

/themes/my-theme
    /layouts
        main.php
    /site
        index.php
    /assets
        /css
            style.css

テーマの設定

config/web.php でテーマを設定します。
'components' => [
    'view' => [
        'theme' => [
            'pathMap' => [
                '@app/views' => '@app/themes/my-theme',
            ],
            'baseUrl' => '@web/themes/my-theme',
        ],
    ],
],

左サイドバー用のレイアウトを作成

themes/my-theme/layouts/main.php を作成し、ヘッダーを左サイドバーに移動します。

コード例: main.php

<?php
use yii\helpers\Html;
use app\assets\AppAsset;

AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
    <link rel="stylesheet" href="../themes/my-theme/assets/css/style.css">
</head>
<body>
<?php $this->beginBody() ?>
<div class="container">
    <!-- 左サイドバー -->
    <div class="sidebar">
        <h3>メニュー</h3>
        <ul>
            <li><a href="/">ホーム</a></li>
            <li><a href="/site/about">About</a></li>
            <li><a href="/site/contact">Contact</a></li>
        </ul>
    </div>

    <!-- メインコンテンツ -->
    <div class="content">
        <?= $content ?>
    </div>
</div>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

CSSを追加

themes/my-theme/assets/css/style.css を作成し、サイドバーとコンテンツのレイアウトを設定します。

CSS例: style.css

body {
    margin: 0;
    font-family: Arial, sans-serif;
}

.container {
    display: flex;
    height: 100vh;
}

.sidebar {
    width: 250px;
    background-color: #333;
    color: #fff;
    padding: 20px;
    box-sizing: border-box;
}

.sidebar h3 {
    margin-top: 0;
}

.sidebar ul {
    list-style: none;
    padding: 0;
}

.sidebar ul li {
    margin-bottom: 10px;
}

.sidebar ul li a {
    color: #fff;
    text-decoration: none;
}

.sidebar ul li a:hover {
    text-decoration: underline;
}

.content {
    flex: 1;
    padding: 20px;
    box-sizing: border-box;
}

ビューのオーバーライド

例として、themes/my-theme/site/index.php を作成します。

<?php
$this->title = 'ホーム';
?>
<h1>Welcome to My Theme</h1>
<p>このページでは、テーマでカスタマイズした左サイドバーを確認できます。</p>

アプリを確認

アプリケーションをブラウザで開き、左サイドバーが正しく表示されるか確認します。

参考情報


Today : 1 / Yesterday : 2 / Total : 33

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2025-02-16 (日) 15:52:30