Yii2 topic (Theme) Usage _php instances

Source: Internet
Author: User
Tags smarty template yii
This example describes the Yii2 topic (Theme) usage. Share to everyone for your reference, as follows:

First look at the main configuration methods:

' components ' + = [' view ' = ' theme ' + ['    pathmap ' = ' + ' @app/views ' + ' and      ' @app/themes/basic '] ,      ' baseUrl ' = ' @web/themes/basic ',    ],],  

The topic function in Yii is mainly implemented by the Yii\base\theme class, whose main idea is to define a string mapping array corresponding to the good one one by one, and then to replace the string with the given string by the mapping relationship in the array.

The following mappings are available:

$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 above mapping relationship shows that @app/a is replaced with @app/aaa, resulting in @app/aaa/site/index.php.

But be aware that this is not the end result. Because the file path is manipulated in Yii, if the @app/aaa/site/index.php file exists, the path is returned, otherwise the original path is returned: @app/a/site/index.php

If there is a string @app/c/site/index.php, because the above mapping knows that @app/c corresponds to 2 replacements, Yii will be replaced from the previous one, Mr. Cheng @app/ccc/xxx/site/ index.php, if this file exists, return to this path, otherwise continue to replace.

If no corresponding file exists for all substitution results, then the original path is returned.

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

Now there is a default theme, if you want to add a black theme now, there are two ways to do it.

The first: Copy all views from default to the blank directory.

Second: Copy only one layout file to 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 ',      ];

Good to see, if the file is not found in blank, it will be looked up from default, that is, the file in blank overwrites the file that exists in the default, thus implementing the theme inheritance.

Properties in the theme:

$pathMap: This is used to set the substitution 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 themes apply to views, modules and widgets respectively.

$BASEURL: This is used to set the URL of the resource to be accessed (the end does not add "/")

$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.         * This means that $pathMap and $basepath must be set at least one, if all two are set, the priority is to use $pathmap         */      if ($basePath = $this->getbasepath ()) = = = NULL) {        throw new invalidconfigexception (' The ' BasePath ' property must is set. ');      }      Sets the mapping of the current module's path and $basepath      $this->pathmap = [Yii:: $app->getbasepath () = [$basePath]];    }}

Public Function ApplyTo ($path)

This is the public function ApplyTo ($path) {    ///For the "/", "\" in the path) to replace the $path with the mapping relationship defined in the $pathMap    $path = Filehelper::normalizepath ($path);    foreach ($this->pathmap as $from + $tos) {       //Map source in array (old value)      $from = Filehelper::normalizepath (Yii:: Getalias ($from)). Directory_separator;      If there is a replaceable old value in $path if      (Strpos ($path, $from) = = = 0) {        $n = strlen ($from);        For the target value loop, 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, return directly if          (Is_file ($file)) {            return $file;    }}} return $path;}

For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article is helpful to the PHP program design based on 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.