คู่มือการใช้งาน CodeIgniter เวอร์ชั่น 1.7.2


HTML Helper

ไฟล์ HTML Helper มีฟังก์ชันช่วยเหลือในการทำงานกับ HTML.

เรียกใช้งานผู้ช่วยนี้

ผู้ช่วยนี้จะถูกเรียกใช้งานได้จากโค้ดต่อไปนี้

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

มีฟังก์ชันที่ใช้งานได้ดังนี้

br()

สร้างแท็กขึ้นบรรทัดใหม่ (<br />) โดยขึ้นอยู่กับตัวเลขที่คุณใส่เข้ามา ตัวอย่างเช่น

echo br(3);

ด้านบนจะแสดงผลลัพธ์เป็น <br /><br /><br />

heading()

ให้คุณสร้างแท็ก HTML <h1> โดยตัวแรกจะเป็นข้อมูลที่จะใส่เข้าไปและค่าที่สองคือขนาดของหัวเรื่อง ตัวอย่างเช่น

echo heading('Welcome!', 3);

ด้านบนจะแสดงผลลัพธ์เป็น <h3>Welcome!</h3>

img()

ให้คุณสร้างแท็ก HTML <img /> โดยพารามิเตอร์ตัวแรกให้ใส่แหล่งของภาพตัวอย่างเช่น

echo img('images/picture.jpg');
// จะให้ผล <img src="http://site.com/images/picture.jpg" />

โดยมีพารามิเตอร์ที่สองเป็นทางเลือกคือค่า TRUE/FALSE โดยถ้าใส่ค่าเข้าไปจะมีค่าจาก $config['index_page'] เพิ่มเข้าไปเมื่อถูกสร้าง เป็นไปได้ที้ฟังก์ชันนี้จะถูกใช้ในสำหรับสื่อในตัวควบคุม (controller)

echo img('images/picture.jpg', TRUE);
// จะให้ผล <img src="http://site.com/index.php/images/picture.jpg" />

เพิ่มเติมเราสามารถใส่ค่าเป็นอาเรย์และส่งไปยังฟังก์ชัน img() ได้สำหรับควบคุมคุณลักษณะและค่าของคุณลักษณะทั้งหมด

$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 และ index_page ตามลำดับโดย index_page จะเป็นค่า TRUE/FALSE โดยเฉพาะถ้า href เป็นหน้าที่ถูกกำหนดโดย $config['index_page'] จะเพิ่มเจ้า 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');
// สร้างเป็น <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"
// สร้างเป็น <meta http-equiv="Content-type" content="text/html; charset=utf-8" />


echo meta(array('name' => 'robots', 'content' => 'no-cache'));
// สร้างเป็น <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);
// สร้างเป็น
// <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 ถูกใช้โดยปกติแต่มีหลาย doctypes ให้เลือกใช้

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">

รายการต่อไปนี้คือทางเลือกในการใช้ doctype สิ่งเหล่านี้สามารถปรับแต่งได้โดยถูกดึงมาจากไฟล์ 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">