HTML 헬퍼

HTML 헬퍼 파일에는 HTML 작업을 지원하는 함수가 포함되어 있습니다.

헬퍼 로드

이 헬퍼는 다음 코드를 사용하여 로드됩니다.

<?php

helper('html');

사용 가능한 함수

사용 가능한 함수는 다음과 같습니다.

img([$src = ''[, $indexPage = false[, $attributes = '']]])
Parameters
  • $src (string|array) – 이미지 소스 URI 또는 속성 및 값의 배열

  • $indexPage (bool) – $src를 라우팅된 URI 문자열로 취급할 지 여부

  • $attributes (mixed) – 추가 HTML 속성

Returns

HTML image 태그

Return type

string

HTML <img /> 태그를 만들 수 있습니다. 첫 번째 매개 변수는 이미지 소스를 포함합니다.

<?php

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

src에 생성된 주소에 $config['indexPage']로 지정된 페이지가 추가되어야 하는 경우에 해당하는 선택적 두 번째 매개 변수(true/false 값)가 있습니다. 미디어 컨트롤러를 사용하는 경우 유용합니다.

<?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([$src = ''[, $indexPage = false[, $attributes = '']]])
Parameters
  • $path (string) – 이미지 파일의 경로

  • $mime (string|null) – 사용할 MIME 유형 또는 null

Returns

base64로 인코딩된 이진 이미지 문자열

Return type

string

“data:” 프로토콜을 사용하여 이미지에서 src-ready 문자열을 생성합니다.

<?php

$src = img_data('public/images/picture.jpg'); // data:image/jpg;base64,R0lGODl...
echo img($src);

두 번째 매개 변수 MIME 유형을 지정할 수 있습니다. 지정하지 않으면 MIME 구성을 사용하여 추측합니다.

<?php

$src = img_data('path/img_without_extension', 'image/png'); // data:image/png;base64,HT5A822...

$path가 존재해야하며 data: 프로토콜에서 지원하는 읽을 수 있는 이미지 형식이어야 합니다. 이 기능은 매우 큰 파일에는 권장되지 않지만 웹 액세스가 (예: public/) 불가능한 앱에서 이미지를 편리하게 제공할 수 있습니다.

Parameters
  • $href (string) – 링크 파일의 소스

  • $rel (string) – 관계 유형

  • $type (string) – 관련 문서의 종류

  • $title (string) – 링크 제목

  • $media (string) – 미디어 타입

  • $indexPage (bool) – $src를 라우팅된 URI 문자열로 취급할 지 여부

  • $hreflang (string) – Hreflang 타입

Returns

HTML link 태그

Return type

string

HTML <link /> 태그를 만들 수 있습니다. 스타일 시트 링크 및 기타 링크에 유용합니다.

필수 매개 변수는 href 이며 선택적 매개 변수는 rel, type, title, media, indexPage 입니다.

indexPagehref가 생성한 주소에 $config['indexPage']로 지정된 페이지를 추가해야 하는지 여부를 지정하는 부울 값입니다.

<?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]])
Parameters
  • $src (mixed) – JavaScript 파일의 소스 URL 또는 속성을 지정하는 연관 배열

  • $indexPage (bool) – $src를 라우팅된 URI 문자열로 취급할 지 여부

Returns

HTML script 태그

Return type

string

HTML <script></script> 태그를 만듭니다. 필수 매개 변수는 src 이며 선택적 매개 변수는 * indexPage * 입니다.

indexPagesrc가 생성한 주소에 $config['indexPage']로 지정된 페이지를 추가해야 하는지 여부를 지정하는 부울 값입니다.

<?php

echo script_tag('js/mystyles.js');
// <script src="http://site.com/js/mystyles.js" type="text/javascript"></script>

또한 script_tag() 함수에 모든 속성과 값을 연관 배열로 전달할 수 있습니다

<?php

$script = ['src' => 'js/printer.js', 'defer' => null];

echo script_tag($script);
// <script src="http://site.com/js/printer.js" defer type="text/javascript"></script>
ul($list[, $attributes = ''])
Parameters
  • $list (array) – 목록 항목

  • $attributes (array) – HTML 속성

Returns

HTML 형식의 비 순차 목록

Return type

string

단순 또는 다차원 배열에서 정렬되지 않은 HTML 목록을 생성합니다.

<?php

$list = [
    'red',
    'blue',
    'green',
    'yellow',
];

$attributes = [
    'class' => 'boldlist',
    'id'    => 'mylist',
];

echo ul($list, $attributes);

위의 코드는 아래 HTML을 생성합니다.

<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);

위의 코드는 아래의 HTML을 생성합니다.

<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 = '')
Parameters
  • $list (array) – 목록 항목

  • $attributes (array) – HTML 속성

Returns

HTML 형식의 정렬 된 목록

Return type

string

ul()과 동일하지만 <ul> 대신 <ol> 태그를 사용하여 정렬된 목록을 생성합니다.

video($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])
Parameters
  • $src (mixed) – 소스 문자열 또는 source 함수의 배열. source()함수 참조

  • $unsupportedMessage (string) – 미디어 태그를 지원하지 않는 브라우저에 표시되는 메시지

  • $attributes (string) – HTML 속성

  • $tracks (array) – track 함수의 배열. track()함수 참조

  • $indexPage (bool) – $src를 라우팅된 URI 문자열로 취급할 지 여부

Returns

HTML 형식의 비디오 요소

Return type

string

단순 또는 소스 배열에서 HTML 비디오 요소를 생성합니다.

<?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
);

위의 코드는 아래의 HTML을 생성합니다.

<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]]]])
Parameters
  • $src (mixed) – 소스 문자열 또는 source 함수의 배열. source()함수 참조

  • $unsupportedMessage (string) – 미디어 태그를 지원하지 않는 브라우저에 표시되는 메시지

  • $attributes (string) –

  • $tracks (array) – track 함수의 배열. track()함수 참조

  • $indexPage (bool) – $src를 라우팅된 URI 문자열로 취급할 지 여부

Returns

HTML 형식의 오디오 요소

Return type

string

video()와 동일하지만 <video> 대신 <audio> 태그를 생성합니다.

source($src = ''[, $type = false[, $attributes = '']])
Parameters
  • $src (string) – 미디어 리소스의 경로

  • $type (bool) – 선택적 코덱 매개 변수가있는 리소스의 MIME 유형

  • $attributes (array) – HTML 속성

Returns

HTML source 태그

Return type

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]]])
Parameters
  • $src (string) – embed할 리소스의 경로

  • $type (bool) – MIME-type

  • $attributes (array) – HTML 속성

  • $indexPage (bool) – $src를 라우팅된 URI 문자열로 취급할 지 여부

Returns

HTML embed 태그

Return type

string

HTML <embed /> 태그를 만듭니다. 첫 번째 매개 변수에는 소스를 포함합니다.

. literalinclude:: html_helper/016.php

object($data = ''[, $type = false[, $attributes = '']])
Parameters
  • $data (string) – 리소스 URL

  • $type (bool) – 리소스의 Content-type

  • $attributes (array) – HTML 속성

  • $params (array) – param 함수의 배열. param()함수 참조

Returns

HTML object tag

Return type

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"'),
    ]
);

위의 코드는 아래의 HTML을 생성합니다.

<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 = ''[, $type = false[, $attributes = '']])
Parameters
  • $name (string) – 매개 변수의 이름

  • $value (string) – 매개 변수의 값

  • $attributes (array) – HTML 속성

Returns

HTML param 태그

Return type

string

HTML <param /> 태그를 만듭니다. 첫 번째 매개 변수는 param 소스를 포함합니다.

<?php

echo param('movie.mov', 'video/quicktime', 'class="test"');
// <param src="movie.mov" type="video/quicktime" class="test"/>
track($name = ''[, $type = false[, $attributes = '']])
Parameters
  • $name (string) – 매개 변수의 이름

  • $value (string) – 매개 변수의 값

  • $attributes (array) – HTML 속성

Returns

HTML track 태그

Return type

string

시간이 지정된 트랙을 지정하기 위해 트랙 요소를 생성합니다. 트랙은 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'])
Parameters
  • $type (string) – Doctype 이름

Returns

HTML DocType 태그

Return type

string

문서 유형(DocType) 선언 또는 DTD를 생성하는데 도움을 줍니다. HTML 5가 기본적으로 사용되지만 많은 문서 유형을 사용할 수 있습니다.

<?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 선택 목록입니다. 이 정보는 application/Config/DocTypes.php에 있으며, .env 설정을 통하여 오버라이드될 수 있습니다.

Document type

Option

Result

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