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
269ce903
Commit
269ce903
authored
Apr 15, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
view.
parent
9d1b9718
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
46 deletions
+127
-46
View.php
framework/base/View.php
+122
-41
FileHelper.php
framework/util/FileHelper.php
+5
-5
No files found.
framework/base/View.php
View file @
269ce903
...
@@ -17,62 +17,142 @@ use yii\util\FileHelper;
...
@@ -17,62 +17,142 @@ use yii\util\FileHelper;
*/
*/
class
View
extends
Component
class
View
extends
Component
{
{
public
$file
;
public
$name
;
/**
/**
* @var
string|array
* @var
Object the owner of this view
*/
*/
public
$basePath
;
public
$owner
;
public
$owner
;
public
$locale
;
/**
* @var string|array the base path where the view file should be looked for using the specified view name.
* This can be either a string representing a single base path, or an array representing multiple base paths.
* If the latter, the view file will be looked for in the given base paths in the order they are specified.
* This property must be set before calling [[render()]].
*/
public
$basePath
;
/**
* @var string the language that the view should be rendered in. If not set, it will use
* the value of [[Application::language]].
*/
public
$language
;
/**
* @var string the language that the original view is in. If not set, it will use
* the value of [[Application::sourceLanguage]].
*/
public
$sourceLanguage
;
public
function
render
(
$_params_
=
array
())
/**
* Renders a view.
*
* The method first identifies the actual view file corresponding to the specified view.
* It then calls [[renderFile()]] to render the view file. The rendering result is returned
* as a string. If the view file does not exist, an exception will be thrown.
*
* To determine which view file should be rendered, the method calls [[findViewFile()]] which
* will search in the directories as specified by [[basePath]].
*
* View name can be a path alias representing an absolute file path (e.g. `@app/views/layout/index`),
* or a path relative to [[basePath]]. The file suffix is optional and defaults to `.php` if not given
* in the view name.
*
* @param string $view the view to be rendered. This can be a path alias or a path relative to [[basePath]].
* @param array $params the parameters that should be made available in the view. The PHP function `extract()`
* will be called on this variable to extract the variables from this parameter.
* @throws Exception
*/
public
function
render
(
$view
,
$params
=
array
())
{
{
$
this
->
resolveViewFile
(
);
$
file
=
$this
->
findViewFile
(
$view
);
extract
(
$_params_
,
EXTR_OVERWRITE
);
if
(
$file
!==
false
)
{
ob_start
(
);
return
$this
->
renderFile
(
$file
,
$params
);
ob_implicit_flush
(
false
);
}
else
{
require
(
$this
->
file
);
throw
new
Exception
(
"Unable to find the view file for view '
$view
'."
);
return
ob_get_clean
();
}
}
}
public
function
re
solveViewFile
(
)
public
function
re
nderFile
(
$file
,
$params
=
array
()
)
{
{
if
(
$this
->
file
!==
null
)
{
$this
->
renderFileInternal
(
$file
,
$params
);
return
$this
->
file
;
}
}
if
(
$this
->
name
===
null
||
$this
->
basePath
)
{
public
function
widget
(
$class
,
$properties
=
array
(),
$returnOutput
=
false
)
{
}
}
if
(
empty
(
$viewName
))
public
function
beginWidget
(
$class
,
$properties
=
array
(
))
return
false
;
{
if
(
$moduleViewPath
===
null
)
}
$moduleViewPath
=
$basePath
;
if
((
$renderer
=
Yii
::
app
()
->
getViewRenderer
())
!==
null
)
public
function
endWidget
(
$returnOutput
=
false
)
$extension
=
$renderer
->
fileExtension
;
else
$extension
=
'.php'
;
if
(
$viewName
[
0
]
===
'/'
)
{
{
if
(
strncmp
(
$viewName
,
'//'
,
2
)
===
0
)
$viewFile
=
$basePath
.
$viewName
;
else
$viewFile
=
$moduleViewPath
.
$viewName
;
}
else
if
(
strpos
(
$viewName
,
'.'
))
$viewFile
=
Yii
::
getPathOfAlias
(
$viewName
);
else
$viewFile
=
$viewPath
.
DIRECTORY_SEPARATOR
.
$viewName
;
if
(
is_file
(
$viewFile
.
$extension
))
return
Yii
::
app
()
->
findLocalizedFile
(
$viewFile
.
$extension
);
else
if
(
$extension
!==
'.php'
&&
is_file
(
$viewFile
.
'.php'
))
return
Yii
::
app
()
->
findLocalizedFile
(
$viewFile
.
'.php'
);
else
return
false
;
}
}
public
function
beginClip
(
$id
,
$properties
=
array
())
{
}
public
function
endClip
()
{
}
public
function
beginCache
(
$id
,
$properties
=
array
())
{
}
public
function
endCache
()
{
}
public
function
beginContent
()
{
}
public
function
endContent
()
{
}
protected
function
renderFileInternal
(
$_file_
,
$_params_
=
array
())
{
extract
(
$_params_
,
EXTR_OVERWRITE
);
ob_start
();
ob_implicit_flush
(
false
);
require
(
$_file_
);
return
ob_get_clean
();
}
public
function
findViewFile
(
$view
)
{
if
(
$view
[
0
]
===
'/'
)
{
throw
new
Exception
(
'The view name "$view" should not start with a slash "/".'
);
}
if
((
$extension
=
FileHelper
::
getExtension
(
$view
))
===
''
)
{
$view
.=
'.php'
;
}
if
(
$view
[
0
]
===
'@'
)
{
$file
=
\Yii
::
getAlias
(
$view
[
0
]);
}
elseif
(
!
empty
(
$this
->
basePath
))
{
$basePaths
=
is_array
(
$this
->
basePath
)
?
$this
->
basePath
:
array
(
$this
->
basePath
);
$found
=
false
;
foreach
(
$basePaths
as
$basePath
)
{
$file
=
$basePath
.
DIRECTORY_SEPARATOR
.
$view
;
if
(
is_file
(
$file
))
{
$found
=
true
;
break
;
}
}
if
(
!
$found
)
{
return
false
;
}
}
$file
=
FileHelper
::
localize
(
$file
,
$this
->
language
,
$this
->
sourceLanguage
);
return
is_file
(
$file
)
?
$file
:
false
;
}
}
}
\ No newline at end of file
framework/util/FileHelper.php
View file @
269ce903
...
@@ -46,22 +46,22 @@ class FileHelper
...
@@ -46,22 +46,22 @@ class FileHelper
* in lower case and in the format of LanguageID_RegionID (e.g. "en_us").
* in lower case and in the format of LanguageID_RegionID (e.g. "en_us").
*
*
* @param string $file the original file
* @param string $file the original file
* @param string $
targetL
anguage the target language that the file should be localized to.
* @param string $
l
anguage the target language that the file should be localized to.
* If not set, the value of [[\yii\base\Application::language]] will be used.
* If not set, the value of [[\yii\base\Application::language]] will be used.
* @param string $sourceLanguage the language that the original file is in.
* @param string $sourceLanguage the language that the original file is in.
* If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used.
* If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used.
* @return string the matching localized file, or the original file if the localized version is not found.
* @return string the matching localized file, or the original file if the localized version is not found.
* If the target and the source language codes are the same, the original file will be returned.
* If the target and the source language codes are the same, the original file will be returned.
*/
*/
public
static
function
localize
(
$file
,
$
targetL
anguage
=
null
,
$sourceLanguage
=
null
)
public
static
function
localize
(
$file
,
$
l
anguage
=
null
,
$sourceLanguage
=
null
)
{
{
if
(
$
targetL
anguage
===
null
)
{
if
(
$
l
anguage
===
null
)
{
$
targetL
anguage
=
\Yii
::
$application
->
getLanguage
();
$
l
anguage
=
\Yii
::
$application
->
getLanguage
();
}
}
if
(
$sourceLanguage
===
null
)
{
if
(
$sourceLanguage
===
null
)
{
$sourceLanguage
=
\Yii
::
$application
->
sourceLanguage
;
$sourceLanguage
=
\Yii
::
$application
->
sourceLanguage
;
}
}
if
(
$
targetL
anguage
===
$sourceLanguage
)
{
if
(
$
l
anguage
===
$sourceLanguage
)
{
return
$file
;
return
$file
;
}
}
$desiredFile
=
dirname
(
$file
)
.
DIRECTORY_SEPARATOR
.
$sourceLanguage
.
DIRECTORY_SEPARATOR
.
basename
(
$file
);
$desiredFile
=
dirname
(
$file
)
.
DIRECTORY_SEPARATOR
.
$sourceLanguage
.
DIRECTORY_SEPARATOR
.
basename
(
$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