Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
4b42d78f
Commit
4b42d78f
authored
Nov 01, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1117: added support to map a single view directory to multiple themed view directories.
parent
30e85fae
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
13 deletions
+38
-13
Theme.php
framework/yii/base/Theme.php
+38
-13
No files found.
framework/yii/base/Theme.php
View file @
4b42d78f
...
@@ -13,17 +13,38 @@ use yii\helpers\FileHelper;
...
@@ -13,17 +13,38 @@ use yii\helpers\FileHelper;
/**
/**
* Theme represents an application theme.
* Theme represents an application theme.
*
*
*
A theme is directory consisting of view and layout files which are meant to replace their
*
When [[View]] renders a view file, it will check the [[Application::theme|active theme]]
*
non-themed counterparts
.
*
to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead
.
*
*
* Theme uses [[pathMap]] to achieve the file replacement. A view or layout file will be replaced
* A theme is directory consisting of view files which are meant to replace their non-themed counterparts.
* with its themed version if part of its path matches one of the keys in [[pathMap]].
*
* Then the matched part will be replaced with the corresponding array value.
* Theme uses [[pathMap]] to achieve the view file replacement:
*
* 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
* 2. If such a key exists, the corresponding value will be used to replace the corresponding part
* in the view file path;
* 3. It will then check if the updated view file exists or not. If so, that file will be used
* to replace the original view file.
* 4. If Step 2 or 3 fails, the original view file will be used.
*
*
* For example, if [[pathMap]] is `['/web/views' => '/web/themes/basic']`,
* For example, if [[pathMap]] is `['/web/views' => '/web/themes/basic']`,
* then the themed version for a view file `/web/views/site/index.php` will be
* then the themed version for a view file `/web/views/site/index.php` will be
* `/web/themes/basic/site/index.php`.
* `/web/themes/basic/site/index.php`.
*
*
* It is possible to map a single path to multiple paths. For example,
*
* ~~~
* 'pathMap' => [
* '/web/views' => [
* '/web/themes/christmas',
* '/web/themes/basic',
* ],
* ]
* ~~~
*
* In this case, the themed version could be either `/web/themes/christmas/site/index.php` or
* `/web/themes/basic/site/index.php`. The former has precedence over the latter if both files exist.
*
* To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
* To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
* component like the following:
* component like the following:
*
*
...
@@ -75,16 +96,18 @@ class Theme extends Component
...
@@ -75,16 +96,18 @@ class Theme extends Component
if
(
empty
(
$this
->
pathMap
))
{
if
(
empty
(
$this
->
pathMap
))
{
if
(
$this
->
basePath
!==
null
)
{
if
(
$this
->
basePath
!==
null
)
{
$this
->
basePath
=
Yii
::
getAlias
(
$this
->
basePath
);
$this
->
basePath
=
Yii
::
getAlias
(
$this
->
basePath
);
$this
->
pathMap
=
[
Yii
::
$app
->
getBasePath
()
=>
$this
->
basePath
];
$this
->
pathMap
=
[
Yii
::
$app
->
getBasePath
()
=>
[
$this
->
basePath
]
];
}
else
{
}
else
{
throw
new
InvalidConfigException
(
'The "basePath" property must be set.'
);
throw
new
InvalidConfigException
(
'The "basePath" property must be set.'
);
}
}
}
}
$paths
=
[];
$paths
=
[];
foreach
(
$this
->
pathMap
as
$from
=>
$to
)
{
foreach
(
$this
->
pathMap
as
$from
=>
$to
s
)
{
$from
=
FileHelper
::
normalizePath
(
Yii
::
getAlias
(
$from
));
$from
=
FileHelper
::
normalizePath
(
Yii
::
getAlias
(
$from
));
$to
=
FileHelper
::
normalizePath
(
Yii
::
getAlias
(
$to
));
foreach
((
array
)
$tos
as
$to
)
{
$paths
[
$from
.
DIRECTORY_SEPARATOR
]
=
$to
.
DIRECTORY_SEPARATOR
;
$to
=
FileHelper
::
normalizePath
(
Yii
::
getAlias
(
$to
));
$paths
[
$from
.
DIRECTORY_SEPARATOR
][]
=
$to
.
DIRECTORY_SEPARATOR
;
}
}
}
$this
->
pathMap
=
$paths
;
$this
->
pathMap
=
$paths
;
if
(
$this
->
baseUrl
===
null
)
{
if
(
$this
->
baseUrl
===
null
)
{
...
@@ -103,12 +126,14 @@ class Theme extends Component
...
@@ -103,12 +126,14 @@ class Theme extends Component
public
function
applyTo
(
$path
)
public
function
applyTo
(
$path
)
{
{
$path
=
FileHelper
::
normalizePath
(
$path
);
$path
=
FileHelper
::
normalizePath
(
$path
);
foreach
(
$this
->
pathMap
as
$from
=>
$to
)
{
foreach
(
$this
->
pathMap
as
$from
=>
$to
s
)
{
if
(
strpos
(
$path
,
$from
)
===
0
)
{
if
(
strpos
(
$path
,
$from
)
===
0
)
{
$n
=
strlen
(
$from
);
$n
=
strlen
(
$from
);
$file
=
$to
.
substr
(
$path
,
$n
);
foreach
(
$tos
as
$to
)
{
if
(
is_file
(
$file
))
{
$file
=
$to
.
substr
(
$path
,
$n
);
return
$file
;
if
(
is_file
(
$file
))
{
return
$file
;
}
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment