Yii Framework 공식 가이드 시리즈 44 - 주제: 테마(테마)

黄舟
풀어 주다: 2023-03-05 18:58:01
원래의
1195명이 탐색했습니다.



테마는 웹 애플리케이션에서 웹 페이지의 모양을 사용자 정의하는 체계적인 방법입니다. 새로운 테마를 채택하면 웹 애플리케이션의 전체적인 모양과 느낌이 즉각적이고 극적으로 바뀔 수 있습니다.

Yii에서는 뷰 파일, 레이아웃 파일, 관련 리소스 파일(예: 이미지, CSS 파일, JavaScript 파일 등)을 포함하여 각 테마가 디렉터리로 표시됩니다. 주제의 이름은 해당 디렉터리 이름입니다. 모든 테마는 동일한 디렉토리WebRoot/themes에 배치됩니다. 언제든지 하나의 테마만 활성화할 수 있습니다.

팁: 기본 테마 루트 디렉토리WebRoot/themes를 다른 디렉토리로 구성할 수 있습니다. themeManager 애플리케이션 컴포넌트의 basePath 및 baseUrl 속성을 원하는 값으로 구성하기만 하면 됩니다.

테마를 활성화하려면 웹 애플리케이션의 테마 속성을 원하는 이름으로 설정하세요. 애플리케이션 구성에서 구성하거나 실행 중에 컨트롤러 작업에서 수정할 수 있습니다.

참고: 주제 이름은 대소문자를 구분합니다. 존재하지 않는 주제를 시작하려고 하면 yii: :app()->themenull 을 반환합니다.

테마 디렉터리의 콘텐츠는 애플리케이션 기본 경로 디렉터리와 동일한 방식으로 구성됩니다. 예를 들어 모든 뷰 파일은 views 아래에, 레이아웃 뷰 파일은 views/layouts 아래에, 시스템 뷰 파일은 views/system 아래에 있어야 합니다. 예를 들어 PostControllercreate 뷰 파일을 classic 테마로 바꾸고 싶다면 새 뷰 파일을 WebRoot/themes/classic/views/post/create.php로 저장하면 됩니다.

모듈의 컨트롤러 보기 파일의 경우 해당 테마 보기 파일은 views 디렉터리에 배치됩니다. 예를 들어 위의 PostControllerforum이라는 모듈에 있다면 create 뷰 파일을 WebRoot/themes/classic/views/forum/post/create.php로 저장해야 합니다. forum 모듈이 support이라는 다른 모듈에 중첩되어 있는 경우 뷰 파일은 WebRoot/themes/classic/views/support/forum/post/create.php이어야 합니다.

참고: views 디렉토리에는 보안에 민감한 데이터가 포함될 수 있으므로 네트워크 사용자의 액세스를 방지하도록 구성해야 합니다.

뷰를 표시하기 위해 render 또는 renderPartial을 호출하면 현재 활성화된 테마에서 해당 뷰 파일과 레이아웃 파일을 찾을 수 있습니다. 발견되면 해당 파일이 렌더링됩니다. 그렇지 않으면 viewPath 및layoutPath에 지정된 기본 위치로 대체됩니다.

baseurl 속성을 사용하면 이 이미지 파일에 대해 다음 URL을 생성할 수 있습니다.


yii">
로그인 후 복사

팁: 테마 관점에서 다른 테마 리소스 파일을 연결해야 하는 경우가 많습니다. 예를 들어 테마 아래의 images 디렉터리에 이미지 파일을 표시하려고 할 수 있습니다. 현재 활성화된 테마의 baseurl 속성을 사용하여 이 이미지 파일에 대해


yii: :app()->theme->baseUrl . '/images/FileName.gif'
로그인 후 복사

다음은

basic 두 가지 테마를 가진 애플리케이션의 디렉토리 구성 예입니다.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
로그인 후 복사
애플리케이션 구성에서

를 구성하면

return array(
    'theme'=>'basic',
    ......
);
로그인 후 복사

그러면

테마가 적용됩니다. 이는 애플리케이션의 레이아웃이 basic 디렉토리 아래의 테마를 사용하고 사이트의 색인이 사용됨을 의미합니다. view는 themes/basic/views/layouts 아래의 파일을 사용합니다. 테마에 보기 파일이 없으면 themes/basic/views/site 디렉토리 아래의 파일로 돌아갑니다.protected/views

1. 🎜> 1.1.5 버전부터는 위젯이 사용하는 뷰에도 테마를 적용할 수 있습니다. 특히 위젯 뷰를 렌더링하기 위해 CWidget::render()를 호출하면 Yii는 위젯뿐만 아니라 테마 폴더에서도 검색을 시도합니다. 원하는 보기 파일에 대한 보기 폴더.

클래스 이름이

인 위젯에 대한 보기

를 테마로 지정하려면 먼저

(위젯 클래스 이름과 동일)라는 폴더를 만들어야 합니다. 현재 활성 테마의 뷰 폴더 아래에 위젯 클래스가 xyz와 같이 네임스페이스가 있는 경우(PHP 5.3.0 이상에서 사용 가능) Foo이라는 폴더를 만들어야 합니다. 밑줄 문자.FooappwidgetsFoo그런 다음 새로 생성된 폴더 아래에 app_widgets_Foo이라는 보기 파일을 만듭니다. 이를 위해 위젯에서 원본을 대체하는 데 사용할

파일이 있어야 합니다. 현재 활성 테마가

인 경우 보기.xyz.phpthemes/basic/views/Foo/xyz.php2. 사용자 정의 전역 위젯basic

참고:
이 기능은 버전 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——专题:Theming(主题)的内容,更多关内容请关注PHP中文网(www.php.cn)!


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!