Yii2 Theme (Theme) usage detailed _php example

Source: Internet
Author: User
Tags php programming smarty template yii

This example describes the Yii2 theme (Theme) usage. Share to everyone for your reference, specific as follows:

First look at the main configuration options:

' Components ' => ['
  view ' => [' theme ' => [' Pathmap ' =>
      ] [' @app/views ' => ' @app/themes/basic '] ,
      ' BaseURL ' => ' @web/themes/basic ',
    ],
  ],


The theme features in Yii are implemented primarily by the Yii\base\theme class, whose main idea is to first define a one by one-mapped array of string mappings, and then replace the string with the given string according to the mapping relationship in the array.

There are the following mappings:

$pathMap =[
    ' @app/A ' => ' @app/aaa ',
    ' @app/b ' => ' @app/bbb ',
    ' @app/C ' => [
        ' @app/ccc/xxx ' ,
        ' @app/ccc/yyy ',
      ],
];

For string @app/a/site/index.php, the mapping relationship above shows that @app/a will be replaced with @app/aaa, resulting in @app/aaa/site/index.php.

But be aware that this is not the end result. Because in Yii is the operation of the file path, so if @app/aaa/site/index.php this file exists, it will return this path, otherwise return to the original path that is: @app/a/site/index.php

If there is a string @app/c/site/index.php, since the above mapping knows that @app/c corresponds to 2 replacements, Yii will be replaced from the beginning, and Mr. @app/ccc/xxx/site/ index.php, if the file exists, return the path, or continue with the replacement.

If no corresponding file exists for any of the replacement results, the original path is finally returned.

There is a benefit to writing multiple replacement target values at the same time: implementing inheritance of the theme.

There are now default theme defaults, and if you want to add a set of black themes now, there are two ways to do it.

First: Copy all the views in default to the blank directory.

Second: Copy only one copy of the layout layout file into the blank directory, and then modify the overall color in the layout file. Then set to

$pathMap =[
    ' @app/C ' => [
        ' @app/ccc/blank ',
        ' @app/ccc/default ',
      ],
];

Benefits See, if a file is not found in blank, it is looked up from default, meaning that the file in blank overwrites the file that exists in default, thereby inheriting the subject.

Properties in topic:

$pathMap: This is used to set the replacement mapping relationship.

' Pathmap ' =>[
    ' @app/views ' => [
        ' @app/themes/blank ',
        ' @app/themes/default ',
    ],
    ' @app/ Modules ' => ' @app/themes/default/modules ',
    ' @app/widgets ' => ' @app/themes/default/widgets '
],

These three apply themes to views, modules, and widgets respectively.

$BASEURL: This URL to set the resource to access (ending without "/")

$basePath: Set the file directory where the resource resides

Methods in the topic:

Public Function init ()

Public function init ()
{
    parent::init ();
    If the $PATHMAP mapping is not set, the $basepath is used, if
    (empty ($this->pathmap)) {/
         * * If $basepath is not set, an exception is thrown.
         * That is to say $pathMap and $basepath must be set at least one, if two are set, priority to use $pathmap
         * *
      ($basePath = $this->getbasepath ()) = = NULL) {
        throw new invalidconfigexception (' The ' BasePath ' property must is set. ');
      Sets the path and $basepath mapping relationship for the current module
      $this->pathmap = [Yii:: $app->getbasepath () => [$basePath]];
    }


Public Function ApplyTo ($path)

This is the public function ApplyTo ($path) {///for the $path of the defined mapping relationship in the $pathMap to
replace the
    "/", "\" in the path
    $ Path = Filehelper::normalizepath ($path);
    foreach ($this->pathmap as $from => $tos) {
       //mapping the source (old value) in an array
      $from = Filehelper::normalizepath (Yii:: Getalias ($from)). Directory_separator;
      If there are replaceable old values in the $path if
      (Strpos ($path, $from) = = 0) {
        $n = strlen ($from);
        Loops on the target value, the
        foreach (array) $tos as $to) {
          $to = Filehelper::normalizepath (Yii::getalias ($to)). Directory_separator;
          Replace $from in $path with $to
          $file = $to. substr ($path, $n);
          If it is a file, it returns if
          (Is_file ($file)) {return
            $file
    }}}} return $path;
}

For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with your PHP programming based on the YII framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.