https://github.com/shuber/philt
https://github.com/shuber/philt
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/shuber/philt
- Owner: shuber
- License: other
- Created: 2012-01-28T02:45:21.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2012-02-01T01:02:46.000Z (over 14 years ago)
- Last Synced: 2025-02-25T21:42:22.263Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 99.6 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# Philt
Generic interface to multiple PHP template engines
## Usage
### Initialization
require 'philt/philt.php';
$philt = new Philt;
### Basic
// => /tmp/template.php
Hello = $name ?>!
$template = $philt->template('/tmp/template.php');
get_class($template); // Philt\PhpTemplate
$template->render(array('name' => 'John')); // Hello John!
$template->render(array('name' => 'Jane')); // Hello Jane!
### Nested templates
// => /tmp/template.haml.php
.= $class ?>= $name
$template = $philt->template('/tmp/template.haml.php');
get_class($template); // Philt\NestedTemplate
$template->render(array('class' => 'person', 'name' => 'John')); //
John
$template->render(array('class' => 'person', 'name' => 'Jane')); // Jane
### The `binding` object
#### Properties
$template->binding->class = 'person';
$template->binding->locals; // array('class' => 'person')
$template->render(array('name' => 'John')); //
John
$template->render(array('name' => 'Jane')); // Jane
#### Methods
class AssetHelpers {
function asset_path($path) {
return '/assets/'.$path;
}
}
$template->binding->extend('AssetHelpers');
$template->binding->asset_path('test.png'); // /assets/test.png
#### Overriding methods
class S3AssetHelpers {
function asset_path($path) {
return 'http://example.s3.amazonaws.com'.$this->super($path);
}
}
$template->binding->extend('S3AssetHelpers');
$template->binding->ancestors; // array('S3AssetHelpers', 'AssetHelpers')
$template->binding->asset_path('test.png'); // http://example.s3.amazonaws.com/assets/test.png
#### Missing/wildcard methods
$template->binding->get_color(); // BadMethodCallException - Undefined method Philt\Binding::get_color()
class Getters {
function method_missing($method, $arguments) {
if (preg_match('/^get_(.+)$/', $method, $matches) && isset($this->{$matches[1]})) {
return $this->{$matches[1]};
} else {
return $this->super($method, $arguments);
}
}
}
$template->binding->extend('Getters');
$template->binding->color = 'Brown';
$template->binding->get_color(); // Brown
$template->binding->get_size(); // BadMethodCallException - Undefined method Philt\Binding::get_size()
$template->binding->undefined(); // BadMethodCallException - Undefined method Philt\Binding::undefined()
### Using individual template engines
$template = new Philt\PhpTemplate('
');
$template->render(array('image' => 'test.png')); // 