Skip to content

Commit

Permalink
Fork it, and generalize Template to Resolvable
Browse files Browse the repository at this point in the history
(All tests still pass.)
  • Loading branch information
Dominick Johnson committed Apr 9, 2024
1 parent c32cdbd commit 4a10f87
Show file tree
Hide file tree
Showing 46 changed files with 406 additions and 268 deletions.
103 changes: 55 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
Plates
======

[![Maintainer](http://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/ragboyjr)
[![Source Code](http://img.shields.io/badge/source-league/plates-blue.svg?style=flat-square)](https://github.com/thephpleague/plates)
[![Latest Version](https://img.shields.io/github/release/thephpleague/plates.svg?style=flat-square)](https://github.com/thephpleague/plates/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Build Status](https://img.shields.io/github/actions/workflow/status/thephpleague/plates/php.yml?style=flat-square)](https://github.com/thephpleague/plates/actions?query=workflow%3APHP+branch%3Av3)
[![Quality Score](https://img.shields.io/scrutinizer/g/thephpleague/plates.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/plates)
[![Total Downloads](https://img.shields.io/packagist/dt/league/plates.svg?style=flat-square)](https://packagist.org/packages/league/plates)

Plates is a native PHP template system that's fast, easy to use and easy to extend. It's inspired by the excellent [Twig](http://twig.sensiolabs.org/) template engine and strives to bring modern template language functionality to native PHP templates. Plates is designed for developers who prefer to use native PHP templates over compiled template languages, such as Twig or Smarty.

### Highlights

- Native PHP templates, no new [syntax](https://platesphp.com/templates/syntax/) to learn
- Plates is a template system, not a template language
- Plates encourages the use of existing PHP functions
- Increase code reuse with template [layouts](https://platesphp.com/templates/layouts/) and [inheritance](https://platesphp.com/templates/inheritance/)
- Template [folders](https://platesphp.com/engine/folders/) for grouping templates into namespaces
- [Data](https://platesphp.com/templates/data/#preassigned-and-shared-data) sharing across templates
- Preassign [data](https://platesphp.com/templates/data/#preassigned-and-shared-data) to specific templates
- Built-in [escaping](https://platesphp.com/templates/escaping/) helpers
- Easy to extend using [functions](https://platesphp.com/engine/functions/) and [extensions](https://platesphp.com/engine/extensions/)
- Framework-agnostic, will work with any project
- Decoupled design makes templates easy to test
- Composer ready and PSR-2 compliant

## Installation

Plates is available via Composer:
Contemplate
===========

```
composer require league/plates
```
"Contemplate" is short for "Controllers and Templates". It is somewhat more than a mere templating library, but a great deal less than a full web framework.

## Documentation
This is an extended fork of [Plates](https://github.com/thephpleague/plates) that adds support for additional functionality, such as:

Full documentation can be found at [platesphp.com](https://platesphp.com/).
* Loading controllers (or, any arbitrary function or object) using the same loader used to load templates
* Loading static resources (but *not* public web assets...for now) using the same loader used to load templates
* Name-based associations between templates, controllers, and resources

## Testing
Plates is a very handy little project, but doesn't appear to be receiving new features or responding to pull requests. Contemplate is a drop-in replacement for Plates; you should be able to simply change the import, and everything should "just work". You can then add additional features over time using Contemplate's extended functionality.

```bash
composer test
```
## Documentation

## Contributing
The original documentation for Plates can be found at [platesphp.com](https://platesphp.com/). Additional documentation for Contemplate-specific features will be forthcoming, but a brief overview of the differences can be found below.

Please see [CONTRIBUTING](https://github.com/thephpleague/plates/blob/master/CONTRIBUTING.md) for details.
First, `Template` has been generalized to `Resolvable`. `Resolvable` can be used as a base class for loading other types of resources (controllers or static resources). `Template` is a subclass of `Resolvable`.

## Security
Second, many methods now take an optional `type` parameter. This parameter is a string used to specify which type of resource to resolve. For example, you may have a directory structure like this for your templates and other resources:

If you discover any security related issues, please email [email protected] instead of using the issue tracker.
```
app
+-- index.get.php
+-- index.tpl.php
+-- some_form.get.php
+-- some_form.post.php
+-- some_form.tpl.php
+-- some_article.get.php
+-- some_article.tpl.php
+-- some_article.md
```

## Credits
This structure represents a theoretical site with three pages: index, some_form, and some_article. However, each of these pages has multiple different resolvable resources associated with it. All three have a template (`x.tpl.php`) and a controller for GET requests (`x.get.php`). The form has an additional controller for POST requests (`some_form.post.php`), and the article contains some content in a markdown document (`some_article.md`).

You can associate these different types of resolvable objects with different file extensions:

```php
// The default file extension for unknown or unspecified types
$engine->setFileExtension('php');
// File extensions for special built-in types
// Using these types is optional, but provides some additional features for convenience
$engine->setFileExtension('tpl.php', Resolvable::TYPE_TEMPLATE);
$engine->setFileExtension('get.php', Resolvable::TYPE_CONTROLLER_GET);
$engine->setFileExtension('post.php', Resolvable::TYPE_CONTROLLER_POST);
// Custom extensions for custom types
// These names are arbitrary--you can use whatever makes sense for your application
$engine->setFileExtension('md', 'markdown');
```

- [RJ Garcia](https://github.com/ragboyjr) (Current Maintainer)
- [Jonathan Reinink](https://github.com/reinink) (Original Author)
- [All Contributors](https://github.com/thephpleague/plates/contributors)
Then, when interacting with the engine to resolve objects, you can specify the relevant type either implicitly or explicitly to resolve different objects:

```php
// Templates implicitly use `Resolvable::TYPE_TEMPLATE`
$engine->make('index');
// Controllers (a resolvable type unique to Contemplate) can be called implicitly using the detected HTTP method
$engine->callController('some_form');
// Or explicitly using a built-in type
$engine->callController('some_form', Resolvable::TYPE_CONTROLLER_GET);
// Or explicitly using a custom type
$engine->callController('some_form', 'delegated_function');
// Custom types are specified via the type parameter
$engine->path('some_article', 'markdown');
// `import` can be used to get values returned from arbitrary PHP scripts
$form_handler_object = $engine->import('some_form', type:'form_handler_object');
```

## License

Expand Down
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "league/plates",
"name": "dmjohnson/contemplate",
"description": "Plates, the native PHP template system that's fast, easy to use and easy to extend.",
"keywords": [
"league",
Expand All @@ -8,9 +8,13 @@
"templates",
"views"
],
"homepage": "https://platesphp.com",
"homepage": "https://github.com/dmjohnsson23/contemplate",
"license": "MIT",
"authors" : [
{
"name": "Dominick Johnson",
"role": "Developer"
},
{
"name": "Jonathan Reinink",
"email": "[email protected]",
Expand All @@ -32,12 +36,12 @@
},
"autoload": {
"psr-4": {
"League\\Plates\\": "src"
"DMJohnson\\Contemplate\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"League\\Plates\\Tests\\": "tests"
"DMJohnson\\Contemplate\\Tests\\": "tests"
}
},
"extra": {
Expand Down
14 changes: 7 additions & 7 deletions doc/content/engine/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ parent = "engine"
weight = 5
+++

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]({{< relref "engine/functions.md" >}}) within a `register()` method.
Creating extensions couldn't be easier, and can really make Plates sing for your specific project. Start by creating a class that implements `\DMJohnson\Contemplate\Extension\ExtensionInterface`. Next, register your template [functions]({{< relref "engine/functions.md" >}}) within a `register()` method.

## Simple extensions example

~~~ php
use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use DMJohnson\Contemplate\Engine;
use DMJohnson\Contemplate\Extension\ExtensionInterface;

class ChangeCase implements ExtensionInterface
{
Expand Down Expand Up @@ -51,8 +51,8 @@ They can also be used in a [batch]({{< relref "templates/functions.md#batch-func
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;
use DMJohnson\Contemplate\Engine;
use DMJohnson\Contemplate\Extension\ExtensionInterface;

class ChangeCase implements ExtensionInterface
{
Expand Down Expand Up @@ -97,8 +97,8 @@ $engine->loadExtension(new ChangeCase());
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;
use DMJohnson\Contemplate\Engine;
use DMJohnson\Contemplate\Extension\ExtensionInterface;

class MyExtension implements ExtensionInterface
{
Expand Down
2 changes: 1 addition & 1 deletion doc/content/engine/file-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Plates does not enforce a specific template file extension. By default it assume

~~~ php
// Create new engine and set the default file extension to ".tpl"
$template = new League\Plates\Engine('/path/to/templates', 'tpl');
$template = new DMJohnson\Contemplate\Engine('/path/to/templates', 'tpl');
~~~

## Setter method
Expand Down
4 changes: 2 additions & 2 deletions doc/content/engine/folders.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To create folders, use the `addFolder()` method:

~~~ php
// Create new Plates instance
$templates = new League\Plates\Engine();
$templates = new DMJohnson\Contemplate\Engine();

// Add folders
$templates->addFolder('admin', '/path/to/admin/templates');
Expand All @@ -41,7 +41,7 @@ When enabled, if a folder template is missing, Plates will automatically fallbac

~~~ php
// Create new Plates engine
$templates = new \League\Plates\Engine('/path/to/default/theme');
$templates = new \DMJohnson\Contemplate\Engine('/path/to/default/theme');

// Add themes
$templates->addFolder('theme1', '/path/to/theme/1', true);
Expand Down
2 changes: 1 addition & 1 deletion doc/content/engine/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ While [extensions]({{< relref "engine/extensions.md" >}}) are awesome for adding

~~~ php
// Create new Plates engine
$templates = new \League\Plates\Engine('/path/to/templates');
$templates = new \DMJohnson\Contemplate\Engine('/path/to/templates');

// Register a one-off function
$templates->registerFunction('uppercase', function ($string) {
Expand Down
6 changes: 3 additions & 3 deletions doc/content/engine/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Plates uses a central object called the `Engine`, which is used to store the env

~~~ php
// Create new Plates engine
$templates = new League\Plates\Engine('/path/to/templates');
$templates = new DMJohnson\Contemplate\Engine('/path/to/templates');

// Add any additional folders
$templates->addFolder('emails', '/path/to/emails');

// Load any additional extensions
$templates->loadExtension(new League\Plates\Extension\Asset('/path/to/public'));
$templates->loadExtension(new DMJohnson\Contemplate\Extension\Asset('/path/to/public'));

// Create a new template
$template = $templates->make('emails::welcome');
Expand All @@ -34,7 +34,7 @@ class Controller
{
private $templates;

public function __construct(League\Plates\Engine $templates)
public function __construct(DMJohnson\Contemplate\Engine $templates)
{
$this->templates = $templates;
}
Expand Down
4 changes: 2 additions & 2 deletions doc/content/engine/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Themes provide an alternative to template path resolution that allow for a holis
Given an engine configuration like:

```php
use League\Plates\{Engine, Template\Theme};
use DMJohnson\Contemplate\{Engine, Template\Theme};

$plates = Engine::fromTheme(Theme::hierarchy([
Theme::new('/templates/main', 'Main'), // parent
Expand Down Expand Up @@ -59,7 +59,7 @@ The fallback functionality is a bit different however since with folders, it's *

## Additional Customization

This functionality is powered by the `League\Plates\Template\ResolveTemplatePath` interface. If you'd prefer a more complex or specific path resolution, you can just implement your own and assign it to the engine instance with:
This functionality is powered by the `DMJohnson\Contemplate\Template\ResolveTemplatePath` interface. If you'd prefer a more complex or specific path resolution, you can just implement your own and assign it to the engine instance with:

```php
$plates = Engine::withResolveTemplatePath(new MyCustomResolveTemplatePath());
Expand Down
2 changes: 1 addition & 1 deletion doc/content/extensions/asset.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The asset extension comes packaged with Plates but is not enabled by default, as

~~~ php
// Load asset extension
$engine->loadExtension(new League\Plates\Extension\Asset('/path/to/public/assets/', true));
$engine->loadExtension(new DMJohnson\Contemplate\Extension\Asset('/path/to/public/assets/', true));
~~~

The first constructor parameter is the file system path of the assets directory. The second is an optional `boolean` parameter that if set to true uses the filename caching method (ie. `file.1373577602.css`) instead of the default query string method (ie. `file.css?v=1373577602`).
Expand Down
4 changes: 2 additions & 2 deletions doc/content/extensions/uri.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ The URI extension comes packaged with Plates but is not enabled by default, as i

~~~ php
// Load URI extension using global variable
$engine->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO']));
$engine->loadExtension(new DMJohnson\Contemplate\Extension\URI($_SERVER['PATH_INFO']));

// Load URI extension using a HttpFoundation's request object
$engine->loadExtension(new League\Plates\Extension\URI($request->getPathInfo()));
$engine->loadExtension(new DMJohnson\Contemplate\Extension\URI($request->getPathInfo()));
~~~

## URI example
Expand Down
2 changes: 1 addition & 1 deletion doc/content/getting-started/simple-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Here is a simple example of how to use Plates. We will assume the following dire

~~~ php
// Create new Plates instance
$templates = new League\Plates\Engine('/path/to/templates');
$templates = new DMJohnson\Contemplate\Engine('/path/to/templates');

// Render a template
echo $templates->render('profile', ['name' => 'Jonathan']);
Expand Down
2 changes: 1 addition & 1 deletion doc/content/templates/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Assigning data is done from within your application code, such as a controller.

~~~ php
// Create new Plates instance
$templates = new League\Plates\Engine('/path/to/templates');
$templates = new DMJohnson\Contemplate\Engine('/path/to/templates');

// Assign via the engine's render method
echo $templates->render('profile', ['name' => 'Jonathan']);
Expand Down
6 changes: 3 additions & 3 deletions doc/content/templates/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Plates templates are very simple PHP objects. Generally you'll want to create th

~~~ php
// Create new Plates instance
$templates = new League\Plates\Engine('/path/to/templates');
$templates = new DMJohnson\Contemplate\Engine('/path/to/templates');

// Render a template in a subdirectory
echo $templates->render('partials/header');
Expand All @@ -27,10 +27,10 @@ It's also possible to create templates manually. The only dependency they requir

~~~ php
// Create new Plates instance
$templates = new League\Plates\Engine('/path/to/templates');
$templates = new DMJohnson\Contemplate\Engine('/path/to/templates');

// Create a new template
$template = new League\Plates\Template\Template($templates, 'profile');
$template = new DMJohnson\Contemplate\Template\Template($templates, 'profile');

// Render the template
echo $template->render(['name' => 'Jonathan']);
Expand Down
2 changes: 1 addition & 1 deletion example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
include '../vendor/autoload.php';

// Create new Plates instance
$templates = new League\Plates\Engine('templates');
$templates = new DMJohnson\Contemplate\Engine('templates');

// Preassign data to the layout
$templates->addData(['company' => 'The Company Name'], 'layout');
Expand Down
Loading

0 comments on commit 4a10f87

Please sign in to comment.