mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-20 10:27:18 +08:00
more view docs
This commit is contained in:
@ -183,8 +183,32 @@ page. We're using third argument so one of the views could override it.
|
|||||||
|
|
||||||
### Registering scripts
|
### Registering scripts
|
||||||
|
|
||||||
|
With View object you can register scripts. There are two dedicated methods for it: `registerScript` for inline scripts
|
||||||
|
and `registerJsFile` for external scripts. Inline scripts are useful for configuration and dynamically generated code.
|
||||||
|
The method for adding these can be used as follows:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$this->registerScript("var options = ".json_encode($options).";", View::POS_END, 'my-options');
|
||||||
|
```
|
||||||
|
|
||||||
|
First argument is the actual code where we're converting a PHP array of options to JavaScript one. Second argument
|
||||||
|
determines where script should be in the page. Possible values are:
|
||||||
|
|
||||||
|
- `View::POS_HEAD` for head section.
|
||||||
|
- `View::POS_BEGIN` for right after opening `<body>`.
|
||||||
|
- `View::POS_END` for right before closing `</body>`.
|
||||||
|
- `View::POS_READY` for executing code on document `ready` event. This one registers jQuery automatically.
|
||||||
|
|
||||||
|
The last argument is unique script ID that is used to identify code block and replace existing one with the same ID
|
||||||
|
instead of adding a new one.
|
||||||
|
|
||||||
|
External script can be added like the following:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$this->registerJsFile('http://example.com/js/main.js');
|
||||||
|
```
|
||||||
|
|
||||||
|
Same as with external CSS it's preferred to use asset bundles for external scripts.
|
||||||
|
|
||||||
### Registering asset bundles
|
### Registering asset bundles
|
||||||
|
|
||||||
@ -198,6 +222,40 @@ frontend\config\AppAsset::register($this);
|
|||||||
|
|
||||||
### Layout
|
### Layout
|
||||||
|
|
||||||
|
A layout is a very convenient way to represent the part of the page that is common for all or at least for most pages
|
||||||
|
generated by your application. Typically it includes `<head>` section, footer, main menu and alike elements.
|
||||||
|
You can fine a fine example of the layout in a [basic application template](apps-basic.md). Here we'll review the very
|
||||||
|
basic one without any widgets or extra markup.
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
use yii\helpers\Html;
|
||||||
|
?>
|
||||||
|
<?php $this->beginPage(); ?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="<?php echo Yii::$app->charset; ?>">
|
||||||
|
<head>
|
||||||
|
<meta charset="<?php echo Yii::$app->charset; ?>"/>
|
||||||
|
<title><?php echo Html::encode($this->title); ?></title>
|
||||||
|
<?php $this->head(); ?>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<?php $this->beginBody(); ?>
|
||||||
|
<div class="container">
|
||||||
|
<?php echo $content; ?>
|
||||||
|
</div>
|
||||||
|
<footer class="footer">© 2013 me :)</footer>
|
||||||
|
<?php $this->endBody(); ?>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
<?php $this->endPage(); ?>
|
||||||
|
```
|
||||||
|
|
||||||
|
In the markup above there's some code. First of all, `$content` is a variable that will contain result of views rendered
|
||||||
|
with controller's `$this->render()` method.
|
||||||
|
|
||||||
|
TBD
|
||||||
|
|
||||||
### Partials
|
### Partials
|
||||||
|
|
||||||
Often you need to reuse some HTML markup in many views and often it's too simple to create a full-featured widget for it.
|
Often you need to reuse some HTML markup in many views and often it's too simple to create a full-featured widget for it.
|
||||||
@ -255,4 +313,4 @@ echo $this->context->getRoute();
|
|||||||
|
|
||||||
### Caching blocks
|
### Caching blocks
|
||||||
|
|
||||||
|
To learn about caching of view fragments please refer to [caching](caching.md) section of the guide.
|
||||||
|
@ -654,7 +654,7 @@ class View extends Component
|
|||||||
* - [[POS_BEGIN]]: at the beginning of the body section
|
* - [[POS_BEGIN]]: at the beginning of the body section
|
||||||
* - [[POS_END]]: at the end of the body section
|
* - [[POS_END]]: at the end of the body section
|
||||||
* - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value.
|
* - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value.
|
||||||
* Note that by using this position, the method will automatically register the jquery js file.
|
* Note that by using this position, the method will automatically register the jQuery js file.
|
||||||
*
|
*
|
||||||
* @param string $key the key that identifies the JS code block. If null, it will use
|
* @param string $key the key that identifies the JS code block. If null, it will use
|
||||||
* $js as the key. If two JS code blocks are registered with the same key, the latter
|
* $js as the key. If two JS code blocks are registered with the same key, the latter
|
||||||
|
Reference in New Issue
Block a user