HTML 헬퍼(HTML Helper)
HTML 헬퍼 파일은 HTML 작업을 돕는 함수들을 포함하고 있습니다.
설정
v4.3.0부터 html_helper 함수의 void HTML 요소(예: <img>)는 기본적으로 HTML5 호환으로 변경되었습니다. XHTML과 호환되어야 하는 경우 app/Config/DocTypes.php의 $html5 속성을 false로 설정해야 합니다.
헬퍼 로드하기
이 헬퍼는 다음 코드를 사용하여 로드합니다:
<?php
helper('html');
사용 가능한 함수
다음 함수들을 사용할 수 있습니다:
- img([$src = ''[, $indexPage = false[, $attributes = '']]])
- 매개변수:
$src (
string|array) – 이미지 소스 URI, 또는 속성과 값의 배열$indexPage (
bool) – 소스 경로에Config\App::$indexPage를 추가할지 여부$attributes (
mixed) – 추가 HTML 속성
- 반환:
HTML 이미지 요소
- 반환 형식:
string
HTML
<img>요소를 생성할 수 있습니다. 첫 번째 매개변수에는 이미지 소스가 들어갑니다. 예제:<?php echo img('images/picture.jpg'); // <img src="http://site.com/images/picture.jpg">
선택적 두 번째 매개변수로 true/false 값을 사용하여 생성되는 주소에
Config\App::$indexPage를 src에 추가할지 여부를 지정할 수 있습니다. 미디어 컨트롤러를 사용하는 경우에 해당합니다:<?php echo img('images/picture.jpg', true); // <img src="http://site.com/index.php/images/picture.jpg" alt="">
또한 첫 번째 매개변수로 연관 배열을 전달하면 모든 속성과 값을 완전히 제어할 수 있습니다. alt 속성이 제공되지 않으면 CodeIgniter가 빈 문자열로 자동 생성합니다.
예제:
<?php $imageProperties = [ '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($imageProperties); // <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">
- img_data($path[, $mime = null])
- 매개변수:
$path (
string) – 이미지 파일 경로$mime (
string|null) – 사용할 MIME 타입, 또는 자동 감지를 위한 null
- 반환:
base64 인코딩된 바이너리 이미지 문자열
- 반환 형식:
string
“data:” 프로토콜을 사용하여 이미지로부터 src에 사용 가능한 문자열을 생성합니다. 예제:
<?php $src = img_data('public/images/picture.jpg'); // data:image/jpg;base64,R0lGODl... echo img($src);
선택적 두 번째 매개변수로 MIME 타입을 지정할 수 있으며, 지정하지 않으면 함수가 Mimes 설정을 사용하여 추측합니다:
<?php $src = img_data('path/img_without_extension', 'image/png'); // data:image/png;base64,HT5A822...
$path는 존재해야 하며data:프로토콜이 지원하는 읽기 가능한 이미지 형식이어야 합니다. 이 함수는 매우 큰 파일에는 권장되지 않지만, 웹에서 직접 접근할 수 없는 이미지(예: public/ 외부)를 앱에서 제공하는 편리한 방법을 제공합니다.
- link_tag([$href = ''[, $rel = 'stylesheet'[, $type = 'text/css'[, $title = ''[, $media = ''[, $indexPage = false[, $hreflang = '']]]]]]])
- 매개변수:
$href (
string) – 링크 파일의 소스$rel (
string) – 관계 타입$type (
string) – 연결된 문서의 타입$title (
string) – 링크 제목$media (
string) – 미디어 타입$indexPage (
bool) – 링크 경로에 indexPage를 추가할지 여부$hreflang (
string) – Hreflang 타입
- 반환:
HTML link 요소
- 반환 형식:
string
HTML
<link>요소를 생성할 수 있습니다. 스타일시트 링크 및 다른 링크에 유용합니다. 매개변수는 href이며, 선택적으로 rel, type, title, media, indexPage, hreflang을 사용할 수 있습니다.indexPage는 생성되는 주소에
$config['indexPage']로 지정된 페이지를 href에 추가할지 여부를 지정하는 불리언 값입니다.예제:
<?php echo link_tag('css/mystyles.css'); // <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css">
추가 예제:
<?php 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_tag()함수에 전달하면 모든 속성과 값을 완전히 제어할 수 있습니다:<?php $link = [ '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">
- script_tag([$src = ''[, $indexPage = false]])
- 매개변수:
$src (
array|string) – JavaScript 파일의 소스 이름 또는 URL, 또는 속성을 지정하는 연관 배열$indexPage (
bool) –$src를 라우팅된 URI 문자열로 처리할지 여부
- 반환:
HTML script 요소
- 반환 형식:
string
HTML
<script>요소를 생성할 수 있습니다. 매개변수는 src와 선택적 indexPage입니다.indexPage는 생성되는 주소에
$config['indexPage']로 지정된 페이지를 src에 추가할지 여부를 지정하는 불리언 값입니다.예제:
<?php echo script_tag('js/mystyles.js'); // <script src="http://site.com/js/mystyles.js"></script>
또는 연관 배열을
script_tag()함수에 전달하면 모든 속성과 값을 완전히 제어할 수 있습니다:<?php $script = ['src' => 'js/printer.js', 'defer' => null]; echo script_tag($script); // <script src="http://site.com/js/printer.js" defer></script>
- ul($list[, $attributes = ''])
- 매개변수:
$list (
array) – 목록 항목$attributes (
array) – HTML 속성
- 반환:
HTML 비순서 목록 요소
- 반환 형식:
string
단순 배열 또는 다차원 배열로부터 비순서 HTML 목록을 생성할 수 있습니다. 예제:
<?php $list = [ 'red', 'blue', 'green', 'yellow', ]; $attributes = [ '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>
다차원 배열을 사용하는 더 복잡한 예제입니다:
<?php $attributes = [ 'class' => 'boldlist', 'id' => 'mylist', ]; $list = [ 'colors' => [ 'red', 'blue', 'green', ], 'shapes' => [ 'round', 'square', 'circles' => [ 'ellipse', 'oval', 'sphere', ], ], 'moods' => [ 'happy', 'upset' => [ 'defeated' => [ '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>square</li> <li>circles <ul> <li>ellipse</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>
- ol($list, $attributes = '')
- 매개변수:
$list (
array) – 목록 항목$attributes (
array) – HTML 속성
- 반환:
HTML 순서 목록 요소
- 반환 형식:
string
ul()과 동일하지만,<ul>대신 순서 목록을 위한<ol>요소를 생성합니다.
- video($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])
- 매개변수:
- 반환:
HTML video 요소
- 반환 형식:
string
소스 문자열 또는 소스 배열로부터 HTML video 요소를 생성할 수 있습니다. 예제:
<?php $tracks = [ track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No'), track('subtitles_yes.vtt', 'subtitles', 'yes', 'Norwegian Yes'), ]; echo video('test.mp4', 'Your browser does not support the video tag.', 'controls'); echo video( 'http://www.codeigniter.com/test.mp4', 'Your browser does not support the video tag.', 'controls', $tracks, ); echo video( [ source('movie.mp4', 'video/mp4', 'class="test"'), source('movie.ogg', 'video/ogg'), source('movie.mov', 'video/quicktime'), source('movie.ogv', 'video/ogv; codecs=dirac, speex'), ], 'Your browser does not support the video tag.', 'class="test" controls', $tracks, );
위의 코드는 다음을 생성합니다:
<video src="test.mp4" controls> Your browser does not support the video tag. </video> <video src="http://www.codeigniter.com/test.mp4" controls> <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" /> <track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" /> Your browser does not support the video tag. </video> <video class="test" controls> <source src="movie.mp4" type="video/mp4" class="test" /> <source src="movie.ogg" type="video/ogg" /> <source src="movie.mov" type="video/quicktime" /> <source src="movie.ogv" type="video/ogv; codecs=dirac, speex" /> <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" /> <track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" /> Your browser does not support the video tag. </video>
- audio($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])
- 매개변수:
- 반환:
HTML audio 요소
- 반환 형식:
string
video()와 동일하지만,<video>대신<audio>요소를 생성합니다.
- source($src, $type = 'unknown', $attributes = '', $indexPage = false)
- 매개변수:
$src (
string) – 미디어 리소스의 경로$type (
bool) – 선택적 코덱 매개변수가 포함된 리소스의 MIME 타입$attributes (
array) – HTML 속성
- 반환:
HTML source 요소
- 반환 형식:
string
HTML
<source>요소를 생성할 수 있습니다. 첫 번째 매개변수에는 리소스의 경로가 들어갑니다. 예제:<?php echo source('movie.mp4', 'video/mp4', 'class="test"'); // <source src="movie.mp4" type="video/mp4" class="test">
- embed($src = ''[, $type = false[, $attributes = ''[, $indexPage = false]]])
- 매개변수:
$src (
string) – 삽입할 리소스의 경로$type (
bool) – MIME 타입$attributes (
array) – HTML 속성$indexPage (
bool) – 소스 경로에 indexPage를 추가할지 여부
- 반환:
HTML embed 요소
- 반환 형식:
string
HTML
<embed>요소를 생성할 수 있습니다. 첫 번째 매개변수에는 삽입 소스가 들어갑니다. 예제:<?php echo embed('movie.mov', 'video/quicktime', 'class="test"'); // <embed src="movie.mov" type="video/quicktime" class="test">
- object($data[, $type = 'unknown'[, $attributes = ''[, $params = [][, $indexPage = false]]]])
- 매개변수:
$data (
string) – 리소스 URL$type (
bool) – 리소스의 Content-type$attributes (
array) – HTML 속성$indexPage (
bool) – 리소스 URL에 indexPage를 추가할지 여부$params (
array) – 배열 안에서 param 함수를 사용합니다.param()함수 참고
- 반환:
HTML object 요소
- 반환 형식:
string
HTML
<object>요소를 생성할 수 있습니다. 첫 번째 매개변수에는 object 데이터가 들어갑니다. 예제:<?php echo object('movie.swf', 'application/x-shockwave-flash', 'class="test"'); echo object( 'movie.swf', 'application/x-shockwave-flash', 'class="test"', [ param('foo', 'bar', 'ref', 'class="test"'), param('hello', 'world', 'ref', 'class="test"'), ], );
위의 코드는 다음을 생성합니다:
<object data="movie.swf" class="test"></object> <object data="movie.swf" class="test"> <param name="foo" type="ref" value="bar" class="test" /> <param name="hello" type="ref" value="world" class="test" /> </object>
- param($name, $value[, $type = 'ref'[, $attributes = '']])
- 매개변수:
$name (
string) – 매개변수의 이름$value (
string) – 매개변수의 값$attributes (
array) – HTML 속성
- 반환:
HTML param 요소
- 반환 형식:
string
HTML
<param>요소를 생성할 수 있습니다. 첫 번째 매개변수에는 param 소스가 들어갑니다. 예제:<?php echo param('movie.mov', 'video/quicktime', 'class="test"'); // <param src="movie.mov" type="video/quicktime" class="test">
- track($src, $kind, $srcLanguage, $label)
- 매개변수:
$src (
string) – 트랙 파일(.vtt 파일)의 경로$kind (
string) – 시간 기반 트랙의 종류$srcLanguage (
string) – 시간 기반 트랙의 언어$label (
string) – 시간 기반 트랙의 사용자가 읽을 수 있는 제목
- 반환:
HTML track 요소
- 반환 형식:
string
시간 기반 트랙을 지정하는 track 요소를 생성합니다. 트랙은 WebVTT 형식으로 작성됩니다. 예제:
<?php echo track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No'); // <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No">
- doctype([$type = 'html5'])
- 매개변수:
$type (
string) – Doctype 이름
- 반환:
HTML DocType 선언
- 반환 형식:
string
문서 타입 선언(DTD)을 생성하는 데 도움을 줍니다. 기본적으로 HTML 5가 사용되지만, 다양한 doctype을 사용할 수 있습니다.
예제:
<?php echo doctype(); // <!DOCTYPE html> echo doctype('html4-trans'); // <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
다음은 미리 정의된 doctype 목록입니다. 이 값들은 app/Config/DocTypes.php에서 가져오거나 .env 설정에서 재정의할 수 있습니다.
문서 타입
$type 매개변수
결과
XHTML 1.1
xhtml11
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
XHTML 1.0 Strict
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
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
xhtml1-frame
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Frameset//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd”>
XHTML Basic 1.1
xhtml-basic11
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML Basic 1.1//EN” “http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd”>
HTML 5
html5
<!DOCTYPE html>
HTML 4 Strict
html4-strict
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
HTML 4 Transitional
html4-trans
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
HTML 4 Frameset
html4-frame
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Frameset//EN” “http://www.w3.org/TR/html4/frameset.dtd”>
MathML 1.01
mathml1
<!DOCTYPE math SYSTEM “http://www.w3.org/Math/DTD/mathml1/mathml.dtd”>
MathML 2.0
mathml2
<!DOCTYPE math PUBLIC “-//W3C//DTD MathML 2.0//EN” “http://www.w3.org/Math/DTD/mathml2/mathml2.dtd”>
SVG 1.0
svg10
<!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.0//EN” “http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd”>
SVG 1.1 Full
svg11
<!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.1//EN” “http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd”>
SVG 1.1 Basic
svg11-basic
<!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.1 Basic//EN” “http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd”>
SVG 1.1 Tiny
svg11-tiny
<!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.1 Tiny//EN” “http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd”>
XHTML+MathML+SVG (XHTML host)
xhtml-math-svg-xh
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN” “http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd”>
XHTML+MathML+SVG (SVG host)
xhtml-math-svg-sh
<!DOCTYPE svg:svg PUBLIC “-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN” “http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd”>
XHTML+RDFa 1.0
xhtml-rdfa-1
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML+RDFa 1.0//EN” “http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd”>
XHTML+RDFa 1.1
xhtml-rdfa-2
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML+RDFa 1.1//EN” “http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd”>