PHPackages                             bojaghi/template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. bojaghi/template

ActiveLibrary[Templating &amp; Views](/categories/templating)

bojaghi/template
================

Bojaghi template package

1.0.2(1mo ago)037GPL-2.0-or-laterPHPPHP &gt;=8.0

Since Nov 29Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/bojaghi/template)[ Packagist](https://packagist.org/packages/bojaghi/template)[ Docs](https://github.com/bojaghi/template)[ RSS](/packages/bojaghi-template/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (8)Versions (4)Used By (0)

Template
========

[](#template)

워드프레스 플러그인과 테마에서 사용할 수 있는 템플릿

사용법
---

[](#사용법)

`Bojaghi\Template\Template` 클래스를 객체화 합니다. 아래처럼 직접 배열을 넣는 방법이 있습니다.

```
use Bojaghi\Template\Template

$t = new Template(
    [
        'extensions' => ['php'],
        'infix'      => 'tmpl',
        'scopes'     => [
            plugin_dir_path(__FILE__) . '/templates',
        ],
    ],
);
```

또는 동일한 배열을 리턴하는 파일 경로를 입력할 수도 있습니다.

```
use Bojaghi\Template\Template

$t = new Template('/path/to/config');
```

입력되는 인자는 하나의 연관배열이며 다음 값을 갖습니다.

- extensions: 문자열 배열로 템플릿으로 사용할 파일의 확장자를 지정합니다. 확장자 앞 점은 자동으로 입력되므로 생략해도 됩니다. 기본값은 `['html', 'php']`입니다. 먼저 작성된 확장자가 우선합니다. 즉, 'foo.html', 'foo.php'가 동시에 있을 때 `['html', 'php']`라면 html 파일이 우선순위가 더 높습니다.
- infix: 파일 이름과 확장자 사이에 추가적인 문자열을 넣을 수 있습니다. 기본값은 공백입니다. 만약 infix 가 'tmpl'로 정해졌다면 템플릿 파일은 항상 `.tmpl.php` 처럼 끝나야 합니다.
- scopes: 문자열 배열로 필수입니다. 유효한 절대 경로를 입력해야 합니다. 먼저 적힌 경로에서 발견된 템플릿 파일이 우선적으로 선택됩니다.

### 템플릿 불러오기

[](#템플릿-불러오기)

template() 메소드로 템플릿을 불러옵니다.

```
$output = $t->template('my-template', ['foo' => 'bar']);
```

`my-template` 템플릿을 찾아 HTML 코드를 그려냅니다. 이 때 'foo': 'bar' 가 문맥(context)로 주어집니다. `my-template`의 실제 파일은 scopes, extensions, infix를 고려한, 예를 들어, `{scope}/my-template{infix}{extension}`처럼 경로를 추적하여 실제 파일이 있는지 검사합니다. 있으면 그 파일을 인클루드 합니다. 이 파일 안에 출력할 요소가 작성되어 있습니다.

`$this->get('...')` 메소드를 사용하여 문맥변수로 입력한 값을 활용할 수 있습니다.

```
// my-template.php ?>
Hello,
