외부 코드를 Composer 기반으로 변경
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
||||
---
|
||||
layout: default
|
||||
permalink: engine/extensions/
|
||||
title: Extensions
|
||||
---
|
||||
|
||||
Extensions
|
||||
==========
|
||||
|
||||
Creating extensions couldn't be easier, and can really make Plates sing for your specific project. Start by creating a class that implements `\League\Plates\Extension\ExtensionInterface`. Next, register your template [functions](/engine/functions/) within a `register()` method.
|
||||
|
||||
## Simple extensions example
|
||||
|
||||
~~~ php
|
||||
use League\Plates\Engine;
|
||||
use League\Plates\Extension\ExtensionInterface;
|
||||
|
||||
class ChangeCase implements ExtensionInterface
|
||||
{
|
||||
public function register(Engine $engine)
|
||||
{
|
||||
$engine->registerFunction('uppercase', [$this, 'uppercaseString']);
|
||||
$engine->registerFunction('lowercase', [$this, 'lowercaseString']);
|
||||
}
|
||||
|
||||
public function uppercaseString($var)
|
||||
{
|
||||
return strtoupper($var);
|
||||
}
|
||||
|
||||
public function lowercaseString($var)
|
||||
{
|
||||
return strtolower($var);
|
||||
}
|
||||
}
|
||||
~~~
|
||||
|
||||
To use this extension in your template, simply call your new functions:
|
||||
|
||||
~~~ php
|
||||
<p>Hello, <?=$this->e($this->uppercase($name))?></p>
|
||||
~~~
|
||||
|
||||
They can also be used in a [batch](/templates/functions/#batch-function-calls) compatible function:
|
||||
|
||||
~~~ php
|
||||
<h1>Hello <?=$this->e($name, 'uppercase')</h1>
|
||||
~~~
|
||||
|
||||
## Single method extensions
|
||||
|
||||
Alternatively, you may choose to expose the entire extension object to the template using a single function. This can make your templates more legible and also reduce the chance of conflicts with other extensions.
|
||||
|
||||
~~~ php
|
||||
use League\Plates\Engine;
|
||||
use League\Plates\Extension\ExtensionInterface;
|
||||
|
||||
class ChangeCase implements ExtensionInterface
|
||||
{
|
||||
public function register(Engine $engine)
|
||||
{
|
||||
$engine->registerFunction('case', [$this, 'getObject']);
|
||||
}
|
||||
|
||||
public function getObject()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function upper($var)
|
||||
{
|
||||
return strtoupper($var);
|
||||
}
|
||||
|
||||
public function lower($var)
|
||||
{
|
||||
return strtolower($var);
|
||||
}
|
||||
}
|
||||
~~~
|
||||
|
||||
To use this extension in your template, first call the primary function, then the secondary functions:
|
||||
|
||||
~~~ php
|
||||
<p>Hello, <?=$this->e($this->case()->upper($name))?></p>
|
||||
~~~
|
||||
|
||||
## Loading extensions
|
||||
|
||||
To enable an extension, load it into the [engine](/engine/) object using the `loadExtension()` method.
|
||||
|
||||
~~~ php
|
||||
$engine->loadExtension(new ChangeCase());
|
||||
~~~
|
||||
|
||||
## Accessing the engine and template
|
||||
|
||||
It may be desirable to access the `engine` or `template` objects from within your extension. Plates makes both of these objects available to you. The engine is automatically passed to the `register()` method, and the template is assigned as a parameter on each function call.
|
||||
|
||||
~~~ php
|
||||
use League\Plates\Engine;
|
||||
use League\Plates\Extension\ExtensionInterface;
|
||||
|
||||
class MyExtension implements ExtensionInterface
|
||||
{
|
||||
protected $engine;
|
||||
public $template; // must be public
|
||||
|
||||
public function register(Engine $engine)
|
||||
{
|
||||
$this->engine = $engine;
|
||||
|
||||
// Access template data:
|
||||
$data = $this->template->data();
|
||||
|
||||
// Register functions
|
||||
// ...
|
||||
}
|
||||
}
|
||||
~~~
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
layout: default
|
||||
permalink: engine/file-extensions/
|
||||
title: File extensions
|
||||
---
|
||||
|
||||
File extensions
|
||||
===============
|
||||
|
||||
Plates does not enforce a specific template file extension. By default it assumes `.php`. This file extension is automatically appended to your template names when rendered. You are welcome to change the default extension using one of the following methods.
|
||||
|
||||
## Constructor method
|
||||
|
||||
~~~ php
|
||||
// Create new engine and set the default file extension to ".tpl"
|
||||
$template = new League\Plates\Engine('/path/to/templates', 'tpl');
|
||||
~~~
|
||||
|
||||
## Setter method
|
||||
|
||||
~~~ php
|
||||
// Sets the default file extension to ".tpl" after engine instantiation
|
||||
$template->setFileExtension('tpl');
|
||||
~~~
|
||||
|
||||
## Manually assign
|
||||
|
||||
If you prefer to manually set the file extension, simply set the default file extension to `null`.
|
||||
|
||||
~~~ php
|
||||
// Disable automatic file extensions
|
||||
$template->setFileExtension(null);
|
||||
|
||||
// Render template
|
||||
echo $templates->render('home.php');
|
||||
~~~
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
---
|
||||
layout: default
|
||||
permalink: engine/folders/
|
||||
title: Folders
|
||||
---
|
||||
|
||||
Folders
|
||||
=======
|
||||
|
||||
Folders make it really easy to organize and access your templates. Folders allow you to group your templates under different namespaces, each of which having their own file system path.
|
||||
|
||||
## Creating folders
|
||||
|
||||
To create folders, use the `addFolder()` method:
|
||||
|
||||
~~~ php
|
||||
// Create new Plates instance
|
||||
$templates = new League\Plates\Engine();
|
||||
|
||||
// Add folders
|
||||
$templates->addFolder('admin', '/path/to/admin/templates');
|
||||
$templates->addFolder('emails', '/path/to/email/templates');
|
||||
~~~
|
||||
|
||||
## Using folders
|
||||
|
||||
To use the folders you created within your project simply append the folder name with two colons before the template name. For example, to render a welcome email:
|
||||
|
||||
~~~ php
|
||||
$email = $templates->render('emails::welcome');
|
||||
~~~
|
||||
|
||||
This works with template functions as well, such as layouts or nested templates. For example:
|
||||
|
||||
~~~ php
|
||||
<?php $this->layout('shared::template') ?>
|
||||
~~~
|
||||
|
||||
## Folder fallbacks
|
||||
|
||||
When enabled, if a folder template is missing, Plates will automatically fallback and look for a template with the **same** name in the default folder. This can be helpful when using folders to manage themes. To enable fallbacks, simply pass `true` as the third parameter in the `addFolders()` method.
|
||||
|
||||
~~~ php
|
||||
// Create new Plates engine
|
||||
$templates = new \League\Plates\Engine('/path/to/default/theme');
|
||||
|
||||
// Add themes
|
||||
$templates->addFolder('theme1', '/path/to/theme/1', true);
|
||||
$templates->addFolder('theme2', '/path/to/theme/2', true);
|
||||
~~~
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
---
|
||||
layout: default
|
||||
permalink: engine/functions/
|
||||
title: Functions
|
||||
---
|
||||
|
||||
Functions
|
||||
=========
|
||||
|
||||
While [extensions](/engine/extensions/) are awesome for adding additional reusable functionality to Plates, sometimes it's easier to just create a one-off function for a specific use case. Plates makes this easy to do.
|
||||
|
||||
## Registering functions
|
||||
|
||||
~~~ php
|
||||
// Create new Plates engine
|
||||
$templates = new \League\Plates\Engine('/path/to/templates');
|
||||
|
||||
// Register a one-off function
|
||||
$templates->registerFunction('uppercase', function ($string) {
|
||||
return strtoupper($string);
|
||||
});
|
||||
~~~
|
||||
|
||||
To use this function in a template, simply call it like any other function:
|
||||
|
||||
~~~ php
|
||||
<h1>Hello <?=$this->e($this->uppercase($name))</h1>
|
||||
~~~
|
||||
|
||||
It can also be used in a [batch](/templates/functions/#batch-function-calls) compatible function:
|
||||
|
||||
~~~ php
|
||||
<h1>Hello <?=$this->e($name, 'uppercase')</h1>
|
||||
~~~
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
---
|
||||
layout: default
|
||||
permalink: engine/
|
||||
title: The Engine
|
||||
---
|
||||
|
||||
The Engine
|
||||
==========
|
||||
|
||||
Plates uses a central object called the `Engine`, which is used to store the environment configuration, functions and extensions. It helps decouple your templates from the file system and other dependencies. For example, if you want to change the folder where your templates are stored, you can do so by simply changing the path in one location.
|
||||
|
||||
## Basic usage
|
||||
|
||||
~~~ php
|
||||
// Create new Plates engine
|
||||
$templates = new League\Plates\Engine('/path/to/templates');
|
||||
|
||||
// Add any any additional folders
|
||||
$templates->addFolder('emails', '/path/to/emails');
|
||||
|
||||
// Load any additional extensions
|
||||
$templates->loadExtension(new League\Plates\Extension\Asset('/path/to/public'));
|
||||
|
||||
// Create a new template
|
||||
$template = $templates->make('emails::welcome');
|
||||
~~~
|
||||
|
||||
## Dependency Injection
|
||||
|
||||
Plates is designed to be easily passed around your application and easily injected in your controllers or other application objects. Simply pass an instance of the `Engine` to any consuming objects, and then use either the `make()` method to create a new template, or the `render()` method to render it immediately. For example:
|
||||
|
||||
~~~ php
|
||||
class Controller
|
||||
{
|
||||
private $templates;
|
||||
|
||||
public function __construct(League\Plates\Engine $templates)
|
||||
{
|
||||
$this->templates = $templates;
|
||||
}
|
||||
|
||||
// Create a template object
|
||||
public function getIndex()
|
||||
{
|
||||
$template = $this->templates->make('home');
|
||||
|
||||
return $template->render();
|
||||
}
|
||||
|
||||
// Render a template directly
|
||||
public function getIndex()
|
||||
{
|
||||
return $this->templates->render('home');
|
||||
}
|
||||
}
|
||||
~~~
|
||||
Reference in New Issue
Block a user