yii2 html帮助类,Yii2 学习笔记之助手类(HelperClass)

本文详细介绍了Yii框架中的助手类功能,包括数组助手类的map方法使用、HTML助手类的各种输入方法及表单部件的具体实现方式。同时,还提供了URL助手类和面包屑小部件的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、数组助手类

// 常用的就是建立哈希表,map()方法。一般在使用dropDownList的时候,

// 会从查询出来的对象列表中获取到这样的$array供其使用。

// 参考http://www.yiichina.com/doc/guide/2.0/helper-array

$array = [

['id' => '123', 'name' => 'aaa', 'class' => 'x'],

['id' => '124', 'name' => 'bbb', 'class' => 'x'],

['id' => '345', 'name' => 'ccc', 'class' => 'y'],

];

ArrayHelper::map($array, 'id', 'name');

// 结果是:

[

'123' => 'aaa',

'124' => 'bbb',

'345' => 'ccc',

]

二、HTML 助手类

如果你知道 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 在方法的声明上有一点点不同:

// http://www.yiichina.com/doc/guide/2.0/helper-html

= 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')) ?>

三、HTML Activeform表单部件

textInput(); // 文本框

passwordInput(); // 密码框

radio(),radioList(); // 单选框

checkbox() // 复选框 1

checkboxList(); // 复选框 2

dropDownList(); // 下拉框

hiddenInput(); // 隐藏域

textarea(['rows'=3]); // 文本域

fileInput(); // 文件上传

submitButton(); // 提交按钮

resetButtun(); // 重置按钮

= $form = ActiveForm::begin(['action' => ['test/getpost'], 'method'=>'post',]) ?>

= $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>

= $form->field($model, 'create_at')->widget(DatePicker::className(), ['clientOptions' => ['dateFormat' => 'yy-mm-dd']])->textInput(['placeholder' => '创建时间']) ?>

= $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>

= $form->field($model, 'sex')->radioList(['1'=>'男', '0'=>'女']) ?>

= $form->field($model, 'edu')->dropDownList(['1'=>'大学', '2'=>'高中', '3'=>'初中'], ['prompt'=>'请选择', 'style'=>'width:120px']) ?>

= $form->field($model, 'file')->fileInput() ?>

= $form->field($model, 'hobby')->checkboxList(['0'=>'篮球', '1'=>'足球', '2'=>'羽毛球', '3'=>'乒乓球']) ?>

= $form->field($model, 'info')->textarea(['rows'=>3]) ?>

= $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>

= Html::submitButton('提交', ['class'=>'btn btn-primary', 'name' =>'submit-button']) ?>

= Html::resetButton('重置', ['class'=>'btn btn-primary', 'name' =>'submit-button']) ?>

= ActiveForm::end() ?>

四、URL助手类

// 参考http://www.yiichina.com/doc/guide/2.0/helper-url

// 返回首页

$relativeHomeUrl = Url::home();

// /index.php?r=site/index

echo Url::to(['site/index']);

// /index.php?r=site/index&src=ref1#name

echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);

// /index.php?r=post/edit&id=100 assume the alias "@postEdit" is defined as "post/edit"

echo Url::to(['@postEdit', 'id' => 100]);

// the currently requested URL

echo Url::to();

// /images/logo.gif

echo Url::to('@web/images/logo.gif');

// images/logo.gif

echo Url::to('images/logo.gif');

// http://www.example.com/images/logo.gif

echo Url::to('@web/images/logo.gif', true);

// https://www.example.com/images/logo.gif

echo Url::to('@web/images/logo.gif', 'https');

五、面包屑小部件

// 摘自view文件的代码

$this->params['breadcrumbs'][] = ['label' => '文章管理', 'url' => ['index']];

$this->params['breadcrumbs'][] = $this->title;

// 摘自layout文件的代码

echo Breadcrumbs::widget([

'tag'=>'ol',

'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],

]);

(完)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值