To a Bootstrap form tag library with attachments

Source: Internet
Author: User
Tags php foreach
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn. We often use the Bootstrap development background to create a tag library.
Namespace Think \ Template \ TagLib;

Use Think \ Template \ TagLib;

Class Bootstrap extends TagLib
{

Protected $ tags = array (
/**
* Id: Custom ID. default value is not defined (input + form name)
* Class custom class
* The input description of the help form is displayed below the form.
* Status: initialization status of the current form. Optional values: success, error, and warning.
* Inline forms
* Horizontal horizontally arranged form [Note: two numbers are separated by commas (,). To use this arrangement, add form-horizontal to the form.] [Example: horizontal = "3, 9 "]
*/
'Text' => array ('attr' => array ('id', 'class', 'name', 'value', 'label', 'placeholder ', 'help', 'status', 'inline', 'horizontal '), 'close' => 0 ),
'Password' => array ('attr' => array ('id', 'class', 'name', 'label', 'placeholder', 'help ', 'status', 'inline', 'horizontal '), 'close' => 0 ),
'Number' => array ('attr' => array ('id', 'class', 'name', 'value', 'label', 'placeholder ', 'help', 'status', 'inline', 'horizontal '), 'close' => 0 ),
'Email '=> array ('attr' => array ('id', 'class', 'name', 'value', 'label', 'placeholder ', 'help', 'status', 'inline', 'horizontal '), 'close' => 0 ),
'Url' => array ('attr' => array ('id', 'class', 'name', 'value', 'label', 'placeholder ', 'help', 'status', 'inline', 'horizontal '), 'close' => 0 ),
'File' => array ('attr' => array ('id', 'class', 'name', 'label', 'help', 'inline ', 'horizontal '), 'close' => 0 ),
'Checkbox' => array ('attr' => array ('id', 'class', 'name', 'label', 'inline', 'Disabled ', 'checked', 'default'), 'close' => 0 ),
'Radio' => array ('attr' => array ('id', 'class', 'name', 'label', 'inline', 'Disabled ', 'checked', 'default'), 'close' => 0 ),
/**
* Key: name of the value Field, default id
* Text: displays the name of the value field. The default value is name.
*/
'Texta' => array ('attr' => array ('id', 'class', 'name', 'label', 'value', 'label ', 'rows ', 'key', 'text', 'horizontal'), 'close' => 0 ),
'Select' => array ('attr' => array ('id', 'class', 'name', 'label', 'data', 'value ', 'key', 'horizontal '), 'close' => 0)
);
Private $ ids = array (); // The Automatic Production ID is saved to prevent duplication.

/**
* @ Param $ id string input ID
* @ Param $ name string input name value
* @ Return string
*/
Protected function setId ($ id, $ name)
{
Return empty ($ id )? 'Input'. ucfirst ($ name). mt_rand (1, 9999): $ id;
}

/**
* Return icons and statuses based on the form status.
* @ Param $ status the current status of the form
* @ Return array ()
*/
Protected function inputStatus ($ attr)
{
Switch ($ attr ['status']) {
Case 'success ':
$ Icon = '';
$ Class = 'has-success ';
Break;
Case 'error ':
$ Icon = '';
$ Class = 'has-error ';
Break;
Case 'warning ':
$ Icon = '';
$ Class = 'has-warning ';
Break;
Default:
$ Icon = '';
$ Class = '';
Break;
}
$ Icon = empty ($ icon )? $ Icon: $ icon. '('. $ attr ['status']. ')';
Return array ('statusclass '=> $ class, 'icon' => $ icon );
}

Protected function wrapDiv ($ attr)
{
$ Class = array ();
If (! Empty ($ attr ['inline']) array_push ($ class, 'form-inline ');
If (! Empty ($ attr ['statusclass ']) array_push ($ class, $ attr ['statusclass']);
If (! Empty ($ attr ['has _ feedback']) array_push ($ class, $ attr ['has _ feedback']);
If (! Empty ($ attr ['class']) array_push ($ class, $ attr ['class']);
If (empty ($ attr ['typeclass ']) {
// Input type
Array_push ($ class, 'form-group ');
} Else {
// Select the checkbox radio type
Array_push ($ class, $ attr ['typeclass ']);
}
Return'

';
}

Protected function wrapLabel ($ attr)
{
$ Id = $ attr ['id'];
$ Width = '';
If (array_key_exists ('horizontal ', $ attr )&&! Empty ($ attr ['horizontal ']) {
$ Width = 'col-xs-'. $ attr ['horizontal'] [0];
}
$ TypeClass = empty ($ attr ['typeclass '])? 'Control-label': $ attr ['typeclass '];
Return'';
}

Protected function input ($ attr)
{
$ Attrs = array ();
If (! Empty ($ attr ['holder']) array_push ($ attrs, $ attr ['placeholder']); // input box description
If (! Empty ($ attr ['describedby']) array_push ($ attrs, $ attr ['describedby']); // The input box is associated with the icon,
If (empty ($ attr ['typeclass ']) {
// Text/password/email ....
Array_push ($ attrs, 'class = "form-control" '); // calss attribute
If (! Empty ($ attr ['value']) array_push ($ attrs, 'value = '. $ attr ['value']); // Default value
} Else {
// Select the checkbox/radio type
If (! Empty ($ attr ['default']) {
$ Default = $ this-> _ trim ($ attr ['default']);
$ Default = explode ('.', $ default );
$ Default = $ default [0]. "['". $ default [1]. "']";
$ Checked =' ';
} Else {
$ Checked = empty ($ attr ['checked'])? '': 'Checked ';
}
If (! Empty ($ attr ['Disabled ']) array_push ($ attrs, 'Disabled'); // disable
Array_push ($ attrs, $ checked); // default value judgment
}
$ Input ="";
Return $ input. $ attr ['icon ']. $ attr ['help'];
}

/**
* Input box default form assignment
* @ Param $ attr
*/
Protected function set_input_attr ($ attr)
{
$ Attr ['id'] = $ this-> setId ($ attr ['id'], $ attr ['name']);
$ Status = $ this-> inputStatus ($ attr );
$ Attr ['statusclass '] = $ status ['statusclass']; // shown above wrapDiv
$ Attr ['icon '] = $ status ['icon']; // shown on the wrapLabel
$ Attr ['has _ feedback'] = empty ($ status ['icon '])? '': 'Has-feedback'; // shown above wrapDiv
$ Attr ['describedby'] = empty ($ status ['icon '])? '': 'Aria-describedby =" '. $ attr ['id']. 'status "'; // associated icon on the form
$ Attr ['help'] = empty ($ attr ['help'])? '':''. $ Attr ['help']. '';
$ Attr ['placeholder '] = empty ($ attr ['placeholder'])? '': 'Placeholder =" '. $ attr ['holder'].' "';
If (array_key_exists ('horizontal ', $ attr )&&! Empty ($ attr ['horizontal ']) {
$ Attr ['horizontal '] = explode (', ', $ attr ['horizontal']);
}
Return $ attr;
}

Public function _ text ($ attr)
{
$ Attr ['type'] = 'text ';
Return $ this-> inputClass ($ attr );
}

Public function _ password ($ attr)
{
$ Attr ['type'] = 'Password ';
Return $ this-> inputClass ($ attr );
}

Public function _ email ($ attr)
{
$ Attr ['type'] = 'email ';
Return $ this-> inputClass ($ attr );
}

Public function _ number ($ attr)
{
$ Attr ['type'] = 'number ';
Return $ this-> inputClass ($ attr );
}

Public function _ url ($ attr)
{
$ Attr ['type'] = 'url ';
Return $ this-> inputClass ($ attr );
}

Public function _ file ($ attr)
{
$ Attr ['type'] = 'file ';
Return $ this-> inputClass ($ attr );
}

Public function _ checkbox ($ attr)
{
$ Attr ['typeclass '] = empty ($ attr ['inline'])? 'Checkbox': 'checkbox-inline ';
$ Attr ['type'] = 'checkbox ';
Return $ this-> checkClass ($ attr );
}

Public function _ radio ($ attr)
{
$ Attr ['typeclass '] = empty ($ attr ['inline'])? 'Radio': 'radio-inline ';
$ Attr ['type'] = 'Radio ';
Return $ this-> checkClass ($ attr );
}

Public function _ textarea ($ attr)
{
$ Id = $ this-> setId ($ attr ['id'], $ attr ['name']);
$ Textarea =''. $ Attr ['value'].'';
Return $ this-> _ return ($ attr, $ textarea );
}

// Select a type form
Protected function checkClass ($ attr)
{
$ Attr = $ this-> set_input_attr ($ attr );
$ Input = $ this-> input ($ attr );
$ P = $ this-> wrapDiv ($ attr );
$ Label = $ this-> wrapLabel ($ attr );
Return $ p. $ label. $ input. $ attr ['label'].'

';
}

// Output input type form
Protected function inputClass ($ attr)
{
$ Input = $ this-> input ($ attr );
Return $ this-> _ return ($ attr, $ input );
}

// Remove left and right curly braces
Private function _ trim ($ field)
{
$ Field = ltrim ($ field ,'{');
Return rtrim ($ field ,'}');
}

Public function _ select ($ attr)
{
$ Data = $ this-> _ trim ($ attr ['data']);
$ Key = empty ($ attr ['key'])? 'Id': $ attr ['key'];
$ Text = empty ($ attr ['text'])? 'Name': $ attr ['text'];
$ Value = $ attr ['value'];
$ Id = $ this-> setId ($ attr ['id'], $ attr ['name']);
If (strpos ($ value ,'{')! = False ){
$ Value = $ this-> _ trim ($ value );
}
$ Select ='';$ Select. =' ';$ Select. ='>{$ V. '. $ text .'}';$ Select. =' ';$ Select. ='';
Return $ this-> _ return ($ attr, $ select );

}

Public function _ return ($ attr, $ form)
{
// Determine whether the form is horizontally arranged
$ Horizontal = false;
If (array_key_exists ('horizontal ', $ attr )&&! Empty ($ attr ['horizontal ']) {
$ Horizontal = true ;;
}
$ Attr = $ this-> set_input_attr ($ attr );
$ P = $ this-> wrapDiv ($ attr );
$ Label = $ this-> wrapLabel ($ attr );

If ($ horizontal ){
Return $ p. $ label. $ attr ['label'].'

'. $ Form .'

';
} Else {
Return $ p. $ label. $ attr ['label'].''. $ Form .'

';
}
}


}
Call

$ Area = array (
Array ('id' => 0, 'name' => 'beijing '),
Array ('id' => 1, 'name' => 'shanghai '),
Array ('id' => 2, 'name' => 'tianjin '),
Array ('id' => 3, 'name' => 'shenzhen ')
);
$ Member = array (
'Gender' => 2,
'Email '=> '2017 @ qq.com ',
'Sleep '=> 1,
'Not' => 1,
'Username' => 'xiaolang ',
'Url' => 'HTTP: // www.baidu.com'
);
?>
Running Effect

Bootstraptag repository .rar (3.94 KB download: 52 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.