ホームページ  >  記事  >  バックエンド開発  >  Yii Framework 公式ガイド シリーズ 44 - トピック: テーマ (テーマ)

Yii Framework 公式ガイド シリーズ 44 - トピック: テーマ (テーマ)

黄舟
黄舟オリジナル
2017-02-16 09:31:541178ブラウズ



テーマ設定は、Web アプリケーションの Web ページの外観をカスタマイズする体系的な方法です。新しいテーマを採用することで、Web アプリケーション全体の外観と雰囲気を即座に劇的に変えることができます。

Yii では、各テーマは、ビュー ファイル、レイアウト ファイル、および画像、CSS ファイル、JavaScript ファイルなどの関連リソース ファイルを含むディレクトリによって表されます。トピックの名前はそのディレクトリ名です。すべてのテーマは同じディレクトリ WebRoot/themes に配置されます。いつでも、アクティブにできるテーマは 1 つだけです。 WebRoot/themes下 。在任何时候,只有一个主题可以被激活。

提示:默认的主题根目录WebRoot/themes可被配置成其他的。只需要配置themeManager应用部件的属性basePath和baseUrl为你所要的值。

要激活一个主题,设置Web应用程序的属性theme为你所要的名字。可以在application configuration中配置或者在执行过程中在控制器的动作里面修改。

注:主题名称是区分大小写的。如果您尝试启动一个不存在的主题, yii: :app()->theme将返回null 。

主题目录里面内容的组织方式和application base path目录下的组织方式一样。例如,所有的view文件必须位于views下 ,布局view文件在views/layouts下 ,和系统view文件在views/system下。例如,如果我们要替换PostControllercreate view文件为classic主题下,我们将保存新的view文件为WebRoot/themes/classic/views/post/create.php

对于在module里面的控制器view文件,相应主题view文件将被放在views目录下。例如,如果上述的PostController是在一个命名为forum的模块里 ,我们应该保存create view 文件为WebRoot/themes/classic/views/forum/post/create.php 。如果 forum模块嵌套在另一个名为support模块里 ,那么view文件应为WebRoot/themes/classic/views/support/forum/post/create.php 。

注:由于views目录可能包含安全敏感数据,应当配置以防止被网络用户访问。

当我们调用render或renderPartial显示视图,相应的view文件以及布局文件将在当前激活的主题里寻找。如果发现,这些文件将被render渲染。否则,就后退到viewPath和layoutPath 所指定的预设位置寻找。

baseurl属性,我们就可以为此图像文件生成如下url,


yii">

提示:在一个主题的视图,我们经常需要链接其他主题资源文件。例如,我们可能要显示一个在主题下images目录里的图像文件。使用当前激活主题的baseurl属性,我们就可以为此图像文件生成如下url,


yii: :app()->theme->baseUrl . '/images/FileName.gif'

Below is an example of directory organization for an application with two themes basic and fancy.

WebRoot/
    assets
    protected/
        .htaccess
        components/
        controllers/
        models/
        views/
            layouts/
                main.php
            site/
                index.php
    themes/
        basic/
            views/
                .htaccess
                layouts/
                    main.php
                site/
                    index.php
        fancy/
            views/
                .htaccess
                layouts/
                    main.php
                site/
                    index.php

In the application configuration, if we configure


return array(
    'theme'=>'basic',
    ......
);

then the basic theme will be in effect, which means the application's layout will use the one under the directory themes/basic/views/layouts, and the site's index view will use the one underthemes/basic/views/site. In case a view file is not found in the theme, it will fall back to the one under theprotected/views directory.

1. 主题挂件

Starting from version 1.1.5, views used by a widget can also be themed. In particular, when we callCWidget::render() to render a widget view, Yii will attempt to search under the theme folder as well as the widget view folder for the desired view file.

To theme the view xyz for a widget whose class name is Foo, we should first create a folder named Foo(same as the widget class name) under the currently active theme's view folder. If the widget class is namespaced (available in PHP 5.3.0 or above), such as appwidgetsFoo, we should create a folder namedapp_widgets_Foo. That is, we replace the namespace separators with the underscore characters.

We then create a view file named xyz.php under the newly created folder. To this end, we should have a filethemes/basic/views/Foo/xyz.php, which will be used by the widget to replace its original view, if the currently active theme is basic

ヒント: デフォルトのテーマのルート ディレクトリ WebRoot/themes は、別のディレクトリに構成できます。 ThemeManager アプリケーション コンポーネントのプロパティ BasePath と BaseUrl を必要な値に設定するだけです。

テーマをアクティブにするには、Web アプリケーションのテーマ プロパティを希望の名前に設定します。これは、アプリケーション構成で構成することも、実行中のコントローラー アクションで変更することもできます。

注: テーマ名では大文字と小文字が区別されます。存在しないテーマを開始しようとすると、 yii:🎜 :app()->themenull を返します。 🎜
🎜テーマ ディレクトリ内のコンテンツは、アプリケーションのベース パス ディレクトリと同じ方法で編成されます。たとえば、すべてのビュー ファイルは views の下に、レイアウト ビュー ファイルは views/layouts の下に、システム ビュー ファイルは views/system の下に配置する必要があります。たとえば、PostControllercreate ビュー ファイルを classic テーマに置き換える場合は、新しいビュー ファイルを として保存します。 >WebRoot/messages/classic/views/post/create.php。 🎜🎜モジュール内のコントローラー ビュー ファイルの場合、対応するテーマ ビュー ファイルは views ディレクトリに配置されます。たとえば、上記の PostControllerforum という名前のモジュールにある場合、create ビュー ファイルを WebRoot/themes / として保存する必要があります。 classic/views/forum/post/create.phpforum モジュールが support という名前の別のモジュールにネストされている場合、ビュー ファイルは WebRoot/themes/classic/views/support/forum/post/ create である必要があります。 .php。 🎜🎜🎜注: views ディレクトリにはセキュリティ上重要なデータが含まれる可能性があるため、ネットワーク ユーザーによるアクセスを防ぐように構成する必要があります。 🎜🎜 render または renderPartial を呼び出してビューを表示すると、対応するビュー ファイルとレイアウト ファイルが現在アクティブ化されているテーマ内で見つかります。見つかった場合、これらのファイルがレンダリングされます。それ以外の場合は、viewPath およびlayoutPath で指定されたデフォルトの場所にフォールバックします。 🎜🎜🎜baseurl 属性を使用すると、この画像ファイルの次の URL を生成できます。🎜🎜🎜🎜🎜🎜
$this->widget('CLinkPager', array(
    'pages'=>$pagination,
    'maxButtonCount'=>5,
    'cssFile'=>false,
));
🎜🎜🎜🎜 ヒント: テーマのビューでは、多くの場合、他のテーマ リソース ファイルにリンクする必要があります。たとえば、テーマの下の images ディレクトリにある画像ファイルを表示したい場合があります。現在アクティブ化されているテーマのbaseurl属性を使用すると、この画像ファイルの次のURLを生成できます🎜🎜🎜🎜🎜🎜🎜
return array(
    'components'=>array(
        'widgetFactory'=>array(
            'widgets'=>array(
                'CLinkPager'=>array(
                    'maxButtonCount'=>5,
                    'cssFile'=>false,
                ),
                'CJuiDatePicker'=>array(
                    'language'=>'ru',
                ),
            ),
        ),
    ),
);
🎜🎜🎜以下は、2つのテーマを持つアプリケーションのディレクトリ構成の例です Basic と fancy です。🎜
$this->widget('CLinkPager', array(
    'pages'=>$pagination,
));
🎜アプリケーション設定で🎜🎜🎜🎜🎜🎜
$this->widget('CLinkPager', array(
    'pages'=>$pagination,
    'maxButtonCount'=>2,
));
🎜🎜🎜を設定すると、basic テーマが設定されます。これは、アプリケーションのレイアウトがディレクトリ themes/basic/views/layouts の下にあるものを使用し、サイトのインデックス ビューが themes/basic/views/ の下にあるものを使用することを意味します。サイト。テーマ内にビュー ファイルが見つからない場合は、protected/views ディレクトリにあるビュー ファイルに戻ります。🎜🎜1. バージョンから開始します。 1.1.5 では、ウィジェットで使用されるビューにもテーマを設定できます。特に、ウィジェット ビューをレンダリングするために CWidget::render() を呼び出すと、Yii はテーマ フォルダーとウィジェット ビュー フォルダーの下で目的のテーマを検索しようとします。ビューファイル。🎜🎜クラス名が Foo であるウィジェットのビュー xyz をテーマにするには、まず Foo という名前のフォルダーを作成する必要があります (同じです) appwidgetsFoo など、ウィジェット クラスに名前空間が指定されている場合 (PHP 5.3.0 以降で利用可能)、 という名前のフォルダーを作成する必要があります。 >app_widgets_Foo 。つまり、名前空間の区切り文字をアンダースコア文字に置き換えます。🎜🎜次に、新しく作成したフォルダーの下に xyz.php という名前のビュー ファイルを作成します。ファイルthemes/basic/views/Foo/xyz.php が必要です。現在アクティブなテーマが basic の場合、ウィジェットが元のビューを置き換えるためにこのファイルを使用します。 .🎜 🎜2. カスタム グローバル ウィジェット🎜🎜🎜🎜注:🎜 この機能はバージョン 1.1.3 以降で利用可能です。

When using a widget provided by third party or Yii, we often need to customize it for specific needs. For example, we may want to change the value of CLinkPager::maxButtonCount from 10 (default) to 5. We can accomplish this by passing the initial property values when calling CBaseController::widget to create a widget. However, it becomes troublesome to do so if we have to repeat the same customization in every place we useCLinkPager.


$this->widget('CLinkPager', array(
    'pages'=>$pagination,
    'maxButtonCount'=>5,
    'cssFile'=>false,
));

Using the global widget customization feature, we only need to specify these initial values in a single place, i.e., the application configuration. This makes the customization of widgets more manageable.

To use the global widget customization feature, we need to configure the widgetFactory as follows:


return array(
    'components'=>array(
        'widgetFactory'=>array(
            'widgets'=>array(
                'CLinkPager'=>array(
                    'maxButtonCount'=>5,
                    'cssFile'=>false,
                ),
                'CJuiDatePicker'=>array(
                    'language'=>'ru',
                ),
            ),
        ),
    ),
);

In the above, we specify the global widget customization for both CLinkPager and CJuiDatePicker widgets by configuring the CWidgetFactory::widgets property. Note that the global customization for each widget is represented as a key-value pair in the array, where the key refers to the wiget class name while the value specifies the initial property value array.

Now, whenever we create a CLinkPager widget in a view, the above property values will be assigned to the widget, and we only need to write the following code in the view to create the widget:


$this->widget('CLinkPager', array(
    'pages'=>$pagination,
));

We can still override the initial property values when necessary. For example, if in some view we want to setmaxButtonCount to be 2, we can do the following:


$this->widget('CLinkPager', array(
    'pages'=>$pagination,
    'maxButtonCount'=>2,
));

3. 皮肤

Note: The skin feature has been available since version 1.1.0.

While using a theme we can quickly change the outlook of views, we can use skins to systematically customize the outlook of the widgets used in the views.

A skin is an array of name-value pairs that can be used to initialize the properties of a widget. A skin belongs to a widget class, and a widget class can have multiple skins identified by their names. For example, we can have a skin for the CLinkPager widget and the skin is named as classic.

In order to use the skin feature, we first need to modify the application configuration by configuring theCWidgetFactory::enableSkin property to be true for the widgetFactory application component:


return array(
    'components'=>array(
        'widgetFactory'=>array(
            'enableSkin'=>true,
        ),
    ),
);

Please note that in versions prior to 1.1.3, you need to use the following configuration to enable widget skinning:


return array(
    'components'=>array(
        'widgetFactory'=>array(
            'class'=>'CWidgetFactory',
        ),
    ),
);

We then create the needed skins. Skins belonging to the same widget class are stored in a single PHP script file whose name is the widget class name. All these skin files are stored under protected/views/skins, by default. If you want to change this to be a different directory, you may configure the skinPath property of thewidgetFactory component. As an example, we may create under protected/views/skins a file namedCLinkPager.php whose content is as follows,


<?php
return array(
    &#39;default&#39;=>array(
        'nextPageLabel'=>'&gt;&gt;',
        'prevPageLabel'=>'&lt;&lt;',
    ),
    'classic'=>array(
        'header'=>'',
        'maxButtonCount'=>5,
    ),
);

In the above, we create two skins for the CLinkPager widget: default and classic. The former is the skin that will be applied to any CLinkPager widget that we do not explicitly specify its skin property, while the latter is the skin to be applied to a CLinkPager widget whose skin property is specified as classic. For example, in the following view code, the first pager will use the default skin while the second the classic skin:


<?php $this->widget('CLinkPager'); ?>

<?php $this->widget('CLinkPager', array('skin'=>'classic')); ?>

If we create a widget with a set of initial property values, they will take precedence and be merged with any applicable skin. For example, the following view code will create a pager whose initial values will bearray('header'=>'', 'maxButtonCount'=>6, 'cssFile'=>false), which is the result of merging the initial property values specified in the view and the classic skin.


<?php $this->widget('CLinkPager', array(
    'skin'=>'classic',
    'maxButtonCount'=>6,
    'cssFile'=>false,
)); ?>

Note that the skin feature does NOT require using themes. However, when a theme is active, Yii will also look for skins under the skins directory of the theme's view directory (e.g.WebRoot/themes/classic/views/skins). In case a skin with the same name exists in both the theme and the main application view directories, the theme skin will take precedence.

ウィジェットが存在しないスキンを使用している場合でも、Yii はエラーなしで通常どおりウィジェットを作成します。

情報: スキンを使用すると、Yii は初回にスキン ファイルを探す必要があるため、パフォーマンスが低下する可能性があります。ウィジェットが作成されています。

スキンは、グローバル ウィジェットのカスタマイズ機能と非常によく似ています。主な違いは次のとおりです。

  • スキンはプレゼンテーションのプロパティ値のカスタマイズとより関連しています;

  • ウィジェットは複数のスキンを持つことができます;

  • スキンはテーマに対応しています;

  • スキンの使用は詳細ですグローバル ウィジェットのカスタマイズを使用するよりも高価です。

以上就是Yii框架官方指南系列44——专题:テーマ(主题)の内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。