CodeIgniter 使用手冊版本 2.1.4


HTML 輔助函數

HTML 輔助函數提供一些輔助 html 操作的相關函數

載入輔助函數

輔助函數使用下列方式載入:

$this->load->helper('html');

可用函數如下所示:

br()

根據您所輸入的數字來產生斷行符號標籤(<br />). 參考範例:

echo br(3);

上述程式碼將產生: <br /><br /><br />

heading()

讓您產生 <h1> 相關標籤. 第一個參數請輸入顯示字串,第二個參數來代表您所要顯示的 heading 大小. 參考範例:

echo heading('Welcome!',3);

上述程式碼將產生: <h3>Welcome!</h3>

Additionally, in order to add attributes to the heading tag such as HTML classes, ids or inline styles, a third parameter is available.

echo heading('Welcome!', 3, 'class="pink"')

The above code produces: <h3 class="pink">Welcome!<<h3>

img()

讓您產生 <img /> 相關標籤。第一個參數包含圖片相關原始碼. 參考範例:

echo img('images/picture.jpg');
// 產生 <img src="http://site.com/images/picture.jpg" />

第二個參數可以不用設定,如需設定,其值可為 TRUE/FALSE,此參數是用來決定 img 路徑中是否包含 $config['index_page'] 所定義的網址。一般來說,在媒體播放的時候就會需要此參數。

echo img('images/picture.jpg',TRUE);
// 產生 <img src="http://site.com/index.php/images/picture.jpg" alt="" />

Additionally, an associative array can be passed to the img() function for complete control over all attributes and values. If an alt attribute is not provided, CodeIgniter will generate an empty string.

$image_properties = array(
          'src' => 'images/picture.jpg',
          'alt' => 'Me,demonstrating how to eat 4 slices of pizza at one time',
          'class' => 'post_images',
          'width' => '200',
          'height' => '200',
          'title' => 'That was quite a night',
          'rel' => 'lightbox',
);

img($image_properties);
// <img src="http://site.com/index.php/images/picture.jpg" alt="Me,demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a night" rel="lightbox" />

link_tag()

讓您建立 HTML <link /> 標籤。對於產生 stylesheet 或者是其他內容都相當有用的。參數包含 href,以及選擇性 rel,type,title,media and index_page 參數,index_page 其值可為 TRUE/FALSE,此參數是用來決定 link 路徑中是否包含 $config['index_page'] 所定義的網址。 echo link_tag('css/mystyles.css');
// gives <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />

更多範例:

echo link_tag('favicon.ico','shortcut icon','image/ico');
// <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" />

echo link_tag('feed','alternate','application/rss+xml','My RSS Feed');
// <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />

此外,您也以設定相關陣列參數來傳入 link() 函數中,用來實做屬性跟其值的對應。

$link = array(
          'href' => 'css/printer.css',
          'rel' => 'stylesheet',
          'type' => 'text/css',
          'media' => 'print'
);

echo link_tag($link);
// <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />

nbs()

根據所自訂的數量來產生非斷行空白(&nbsp;). 參考範例:

echo nbs(3);

上述程式碼產生: &nbsp;&nbsp;&nbsp;

ol()  and  ul()

云許您透過單一或多維陣列來傳遞參數,以便產生有順序條列式的 html。參考範例:

$this->load->helper('html');

$list = array(
            'red',
            'blue',
            'green',
            'yellow'
            );

$attributes = array(
                    'class' => 'boldlist',
                    'id'    => 'mylist'
                    );

echo ul($list,$attributes);

上面程式碼將會顯示:

<ul class="boldlist" id="mylist">
  <li>red</li>
  <li>blue</li>
  <li>green</li>
  <li>yellow</li>
</ul>

這是一個更複雜的例子,用多為陣列產生:

$this->load->helper('html');

$attributes = array(
                    'class' => 'boldlist',
                    'id'    => 'mylist'
                    );

$list = array(
            'colors' => array(
                                'red',
                                'blue',
                                'green'
                            ),
            'shapes' => array(
                                'round',
                                'square',
                                'circles' => array(
                                                    'ellipse',
                                                    'oval',
                                                    'sphere'
                                                    )
                            ),
            'moods'    => array(
                                'happy',
                                'upset' => array(
                                                    'defeated' => array(
                                                                        'dejected',
                                                                        'disheartened',
                                                                        'depressed'
                                                                        ),
                                                    'annoyed',
                                                    'cross',
                                                    'angry'
                                                )
                            )
            );


echo ul($list,$attributes);

上述程式碼將產生如下:

<ul class="boldlist" id="mylist">
  <li>colors
    <ul>
      <li>red</li>
      <li>blue</li>
      <li>green</li>
    </ul>
  </li>
  <li>shapes
    <ul>
      <li>round</li>
      <li>suare</li>
      <li>circles
        <ul>
          <li>elipse</li>
          <li>oval</li>
          <li>sphere</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>moods
    <ul>
      <li>happy</li>
      <li>upset
        <ul>
          <li>defeated
            <ul>
              <li>dejected</li>
              <li>disheartened</li>
              <li>depressed</li>
            </ul>
          </li>
          <li>annoyed</li>
          <li>cross</li>
          <li>angry</li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

meta()

幫助您產生 meta 標籤資訊。您可以透過傳遞字串、單一陣列、多重陣列到函數裡面。範例:

echo meta('description','My Great site');
// Generates: <meta name="description" content="My Great Site" />


echo meta('Content-type','text/html; charset=utf-8','equiv'); // Note the third parameter. Can be "equiv" or "name"
// Generates: <meta http-equiv="Content-type" content="text/html; charset=utf-8" />


echo meta(array('name' => 'robots','content' => 'no-cache'));
// Generates: <meta name="robots" content="no-cache" />


$meta = array(
        array('name' => 'robots','content' => 'no-cache'),
        array('name' => 'description','content' => 'My Great Site'),
        array('name' => 'keywords','content' => 'love,passion,intrigue,deception'),
        array('name' => 'robots','content' => 'no-cache'),
        array('name' => 'Content-type','content' => 'text/html; charset=utf-8','type' => 'equiv')
    );

echo meta($meta);
// Generates:
// <meta name="robots" content="no-cache" />
// <meta name="description" content="My Great Site" />
// <meta name="keywords" content="love,passion,intrigue,deception" />
// <meta name="robots" content="no-cache" />
// <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

doctype()

幫助你建立檔案類型聲明以及 DTD。預設值是 XHTML 1.0 Strict ,但你也可以指定其他很多檔案類型。

echo docytype();
// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

echo doctype('html4-trans');
// <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

下面是支援的檔案類型列表。它們都是可靈活設定的,可以在 application/config/doctypes.php 加以設定。

Doctype Option Result
XHTML 1.1 doctype('xhtml11') <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
XHTML 1.0 Strict doctype('xhtml1-strict') <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
XHTML 1.0 Transitional doctype('xhtml1-trans') <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
XHTML 1.0 Frameset doctype('xhtml1-frame') <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
HTML 5 doctype('html5') <!DOCTYPE html>
HTML 4 Strict doctype('html4-strict') <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
HTML 4 Transitional doctype('html4-trans') <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
HTML 4 Frameset doctype('html4-frame') <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">