Yii2-助手類(Html)

胡勇健發表於2024-03-30

Yii2-助手類(Html)

引入名稱空間

Yii2-助手類(Html)
use yii\helpers\Html;

使用

Yii2-助手類(Html)
<?php //【一】表單:Html::beginForm(提交地址,提交方法,屬性陣列);?>

<?=Html::beginForm('','post',['id'=>'form','class'=>'form','data'=>'myself']);?>

	<?php //【二】輸入框:Html::input(型別,name值,預設值,屬性陣列);?>

	<?=Html::input('text','test','',['class' => 'form-control','placeholder'=>'hehe']);?>
	<?=Html::input('email','email','admin@admin.com',['class' => 'form-control']);?>
	<?=Html::input('password','pwd','',['class' => 'form-control']);?>
	<?=Html::input('hidden','hidden','',['class' => 'form-control']);?>

	<hr />
	<?php //Html::表單型別Input(name值,預設值,屬性陣列);?>

	<?=Html::textInput('test','hehe',['class' => 'form-control']);?>
	<?=Html::textInput('email','admin@admin.com',['class' => 'form-control']);?>
	<?=Html::passwordInput('pwd','',['class' => 'form-control']);?>
	<?=Html::hiddenInput('hidden','',['class' => 'form-control']);?>

	<hr />
	<?php //【三】文字域:Html::textarea(表單name,預設值,屬性陣列);?>
	<?=Html::textarea('area','',['class'=>'form-control','rows'=>'3']);?>

	<hr />
	<?php //【四】單選按鈕:Html::radio(name值,是否選中,屬性陣列);?>
	<?=Html::radio('sex',true,['calss'=>'form-control']);?>
	<?php //單選按鈕列表:Html:;radioList(name值,選中的值,鍵值對列表,屬性陣列); ?>
	<?=Html::radioList('height','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

	<hr />
	<?php //【五】核取方塊:Html::checkbox(name值,是否選中,屬性陣列);?>
	<?=Html::checkbox('haha',true,['calss'=>'form-control']);?>
	<?php //單選按鈕列表:Html:;checkboxList(name值,選中的值,鍵值對列表,屬性陣列); ?>
	<?=Html::checkboxList('xixi','1',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

	<hr />
	<?php //【六】下拉選單:Html:;dropDownList(name值,選中的值,鍵值對列表,屬性陣列); ?>
	<?=Html::dropDownList('list','2',['1'=>'160','2'=>'170','3'=>'180'],['class'=>'form-control']);?>

	<hr />
	<?php //【七】控制標籤Label:Html::label(顯示內容,for值,屬性陣列); ?>
	<?=Html::label('顯示的','test',['style'=>'color:#ff0000']);?>

	<hr />
	<?php //【八】上傳控制元件:Html::fileInput(name值,預設值,屬性陣列); ?>
	<?=Html::fileInput('img',null,['class'=>'btn btn-default']);?>

	<hr />
	<?php //【九】按鈕:; ?>
	<?=Html::button('普通按鈕',['class'=>'btn btn-primary']);?>
	<?=Html::submitButton('提交按鈕',['class'=>'btn btn-primary']);?>
	<?=Html::resetButton('重置按鈕',['class'=>'btn btn-primary']);?>

<?=Html::endForm();?>

與模型關聯的Html

Yii2-助手類(Html)
<?php
use yii\helpers\Html;
?>
<?=Html::beginForm('','post',['name'=>'article']);?>
	<?=Html::activeInput('text',$model,'title',['class'=>'form-control']);?>
	<hr />
	<?=Html::activeTextInput($model,'title',['class'=>'form-control']);?>
	<?=Html::activeTextArea($model,'desc',['class' => 'form-control']);?>
	<?=Html::activeTextArea($model,'content',['class'=>'form-control']);?>
<?=Html::endForm();?>

相關文章