当前位置:K88软件开发文章中心网站服务器框架Yii 2.0 → 文章内容

Html 助手(Html)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-26 15:30:10

> $username]) ?>type, model, model attribute name, options<?= Html::activeInput('text', $user, 'name', ['class' => $username]) ?>如果你知道 input 类型,更方便的做法是使用以下快捷方法:yii\helpers\Html::buttonInput()yii\helpers\Html::submitInput()yii\helpers\Html::resetInput()yii\helpers\Html::textInput(), yii\helpers\Html::activeTextInput()yii\helpers\Html::hiddenInput(), yii\helpers\Html::activeHiddenInput()yii\helpers\Html::passwordInput() / yii\helpers\Html::activePasswordInput()yii\helpers\Html::fileInput(), yii\helpers\Html::activeFileInput()yii\helpers\Html::textarea(), yii\helpers\Html::activeTextarea()Radios 和 checkboxes 在方法的声明上有一点点不同:<?= Html::radio('agree', true, ['label' => 'I agree']);<?= Html::activeRadio($model, 'agree', ['class' => 'agreement'])<?= Html::checkbox('agree', true, ['label' => 'I agree']);<?= Html::activeCheckbox($model, 'agree', ['class' => 'agreement'])Dropdown list 和 list box 将会如下渲染:<?= Html::dropDownList('list', $currentUserId, ArrayHelper::map($userModels, 'id', 'name')) ?><?= Html::activeDropDownList($users, 'id', ArrayHelper::map($userModels, 'id', 'name')) ?><?= Html::listBox('list', $currentUserId, ArrayHelper::map($userModels, 'id', 'name')) ?><?= Html::activeListBox($users, 'id', ArrayHelper::map($userModels, 'id', 'name')) ?>第一个参数是 input 的名称,第二个是当前选中的值,第三个则是一个下标为列表值, 值为列表标签的名值对数组。如果你需要使用多项选择, checkbox list 应该能够符合你的需求:<?= Html::checkboxList('roles', [16, 42], ArrayHelper::map($roleModels, 'id', 'name')) ?><?= Html::activeCheckboxList($user, 'role', ArrayHelper::map($roleModels, 'id', 'name')) ?>否则,用 radio list :<?= Html::radioList('roles', [16, 42], ArrayHelper::map($roleModels, 'id', 'name')) ?><?= Html::activeRadioList($user, 'role', ArrayHelper::map($roleModels, 'id', 'name')) ?>Labels 和 Errors如同 inputs 一样,Yii 也提供了两个方法用于生成表单 label 。 带 ative 方法用于从 model 中取数据,另外一个则是直接接收数据。<?= Html::label('User name', 'username', ['class' => 'label username']) ?><?= Html::activeLabel($user, 'username', ['class' => 'label username'])为了从一个或者一组 model 中显示表单的概要错误,你可以使用如下方法:<?= Html::errorSummary($posts, ['class' => 'errors']) ?>为了显示单个错误:<?= Html::error($post, 'title', ['class' => 'error']) ?>Input 的名和值Yii 提供了方法用于从 model 中获取 input 的名称,ids,值。这些主要用于内部调用, 但是有时候你也需要使用它们:// Post[title]echo Html::getInputName($post, 'title');// post-titleecho Html::getInputId($post, 'title');// my first postecho Html::getAttributeValue($post, 'title');// $post->authors[0]echo Html::getAttributeValue($post, '[0]authors[0]');在上面的例子中,第一个参数为模型,而第二个参数是属性表达式。 在最简单的表单中,这个属性表达式就是属性名称,但是在一些多行输入的时候, 它也可以是属性名以数组下标前缀或者后缀(也可能是同时)。[0]content?代表多行输入时第一个 model 的 content 属性的数据值。dates[0]?代表 dates 属性的第一个数组元素。[0]dates[0]?代表多行输入时第一个 model 的 dates 属性的第一个数组元素。为了获取一个没有前缀或者后缀的属性名称,我们可以如下做:// datesecho Html::getAttributeName('dates[0]');样式表和脚本Yii 提供两个方法用于生成包含内联样式和脚本代码的标签。<?= Html::style('.danger { color: #f00; }') ?>Gives you<style>.danger { color: #f00; }</style><?= Html::script('alert("Hello!");', ['defer' => true]);Gives you<script defer>alert("Hello!");</script>如果你想要外联 css 样式文件,可以如下做:<?= Html::cssFile('@web/css/ie5.css', ['condition' => 'IE 5']) ?>generates<!--[if IE 5]> <link href="http://example.com/css/ie5.css" /><![endif]-->第一个参数是 URL。第二个参数是标签属性数组。比普通的标签配置项额外多出的是,你可以指定:condition?来让?<link?被条件控制注释包裹( IE hacker )。 希望你在未来不再需要条件控制注释。noscript?可以被设置为?true?,这样?<link就会被?<noscript>包裹,如此那么这段代码只有在浏览器不支持 JavaScript 或者被用户禁用的时候才会被引入进来。为了外联 JavaScript 文件:<?= Html::jsFile('@web/js/main.js') ?>这个方法的第一个参数同 CSS 一样用于指定外联链接。第二个参数是一个标签属性数组。 同?cssFile?一样,你可以指定?condtion配置项。超链接有一个方法可以用于便捷的生成超链接:<?= Html::a('Profile', ['user/view', 'id' => $id], ['class' => 'profile-link']) ?>第一个参数是超链接的标题。它不会被转码,所以如果是用户输入数据, 你需要使用?Html::encode()?方法进行转码。第二个参数是<a?标签的?href?属性的值。 关于该参数能够接受的更详细的数据值,请参阅?Url::to()。第三个参数是标签的属性数组。在需要的时候,你可以用如下代码生成?mailto?链接:<?= Html::mailto('Contact us', 'admin@example.com') ?>图片为了生成图片标签,你可以如下做:<?= Html::img('@web/images/logo.png', ['alt' => 'My logo']) ?>generates<img src="https://7n.k88.net/attachments/image/cimg/logo.png" alt="My logo" />除了?aliases?之外,第一个参数可以接受 路由,查询,URLs。 同?Url::to()?一样。列表无序列表可以如下生成:<?= Html::ul($posts, ['item' => function($item, $index) { return Html::tag( 'li', $this->render('post', ['item' => $item]), ['class' => 'post'] );}]) ?>有序列表请使用?Html::ol()?方法。

上一页  [1] [2] 


Html 助手(Html)