Skip to content

Commit

Permalink
update: migrate template logic and tests from inhere/kite
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 11, 2021
1 parent a73c224 commit fc06482
Show file tree
Hide file tree
Showing 26 changed files with 2,181 additions and 28 deletions.
49 changes: 43 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

- it's simple and fastly
- simple echo syntax. eg: `{{= $var }}` `{{ $var }}` `{{ echo $var }}`
- chained access array value syntax. eg: `{{ $arr.0 }}` `{{ $map.name }}` `{{ $map.user.name }}`
- support php builtin string function as filters. eg: `{{ $var | ucfirst }}`
- support all control syntax. such as `if,elseif,else;foreach;for;switch`
- support add custom filters.
- support add custom directive. eg: `include` - `{{ include('other.tpl') }}`
- support all syntax:`if,elseif,else;foreach;for;switch...`
- support add custom directive.

## Install

Expand All @@ -33,17 +34,32 @@ $t = new EasyTemplate();
$t->renderString($tplCode);
```

## Custom directive
## Custom directives

You can use the directives implement some special logic.

```php
$tpl = EasyTemplate::new();
$tpl->addDirective(
'include',
function (string $body, string $name) {
/** will call {@see EasyTemplate::include()} */
return '$this->' . $name . $body;
}
);
```

In template can use:

```php

{{ include('part/header.tpl', ['title' => 'My world']) }}

```

## Using the filters
### Filters

### Using the filters

You can use the filters in any of your templates.

Expand Down Expand Up @@ -72,8 +88,11 @@ You can use the filters in any of your templates.
**Passing variables as filter parameters**:

```php
$currency = 'HUF'
{{ '12.75' | currency:$currency }} // HUF 12.75
{{
$suffix = '¥';
}}

{{ '12.75' | add_suffix:$suffix }} // 12.75¥
```

**Built-in functionality**:
Expand All @@ -84,6 +103,24 @@ $currency = 'HUF'
{{ 'foo_bar' | studly }} // FooBar
```

### Custom filters

```php
use PhpPkg\EasyTpl\EasyTemplate;

$tpl = EasyTemplate::new();
// use php built function
$tpl->addFilter('upper', 'strtoupper');

// add more
$tpl->addFilters([
'last3chars' => function (string $str): string {
return substr($str, -3);
},
]);

```

## License

[MIT](LICENSE)
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"authors": [
{
"name": "inhere",
"email": "in.798@qq.com"
"email": "in.798@qq.com",
"homepage": "https://github.com/inhere"
}
],
"require": {
Expand Down
13 changes: 0 additions & 13 deletions example/demo.php

This file was deleted.

Empty file removed src/.keep
Empty file.
243 changes: 243 additions & 0 deletions src/AbstractTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php declare(strict_types=1);

namespace PhpPkg\EasyTpl;

use PhpPkg\EasyTpl\Contract\TemplateInterface;
use InvalidArgumentException;
use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj;
use function is_file;
use function strpos;

/**
* Class AbstractTemplate
*
* @author inhere
* @package PhpPkg\EasyTpl
*/
abstract class AbstractTemplate implements TemplateInterface
{
/**
* @var array
*/
protected array $globalVars = [];

/**
* allow template file ext list. should start with '.'
*
* @var string[]
*/
protected array $allowExt = ['.php', '.tpl'];

/**
* manual set view files
*
* @var array
*/
protected array $tplFiles = [];

/**
* @var string
*/
public string $tplDir = '';

/**
* custom path resolve
*
* @var callable(string): string
*/
public $pathResolver;

/**
* @param array $config
*
* @return static
*/
public static function new(array $config = []): self
{
return new static($config);
}

/**
* Class constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
Obj::init($this, $config);
}

/**
* @param callable $fn
*
* @return $this
*/
public function configThis(callable $fn): self
{
$fn($this);
return $this;
}

/**
* @param string $tplFile
* @param array $tplVars
*/
public function render(string $tplFile, array $tplVars = []): void
{
$tplFile = $this->findTplFile($tplFile);

echo $this->renderFile($tplFile, $tplVars);
}

/**
* @param string $tplName
*
* @return string
*/
protected function findTplFile(string $tplName): string
{
$tplName = $this->resolvePath($tplName);
if (is_file($tplName)) {
return $tplName;
}

if (isset($this->tplFiles[$tplName])) {
return $this->tplFiles[$tplName];
}

if (!$this->tplDir) {
throw new InvalidArgumentException("no such template file: $tplName");
}

$suffix = '';
$tplFile = $this->tplDir . '/' . $tplName;
if (strpos($tplName, '.') > 0) {
$suffix = File::getExtension($tplName);
}

// is an exists file
if ($suffix) {
if (is_file($tplFile)) {
return $tplFile;
}
} else {
foreach ($this->allowExt as $ext) {
$filename = $tplFile . $ext;
if (is_file($filename)) {
return $filename;
}
}
}

throw new InvalidArgumentException("tplDir - no such template file: $tplName");
}

/**
* @param string $filePath
*
* @return string
*/
public function resolvePath(string $filePath): string
{
if ($fn = $this->pathResolver) {
return $fn($filePath);
}

return $filePath;
}

/**
* @param string $tplName
* @param string $filePath
*/
public function addTplFile(string $tplName, string $filePath): void
{
$this->tplFiles[$tplName] = $filePath;
}

/**
* @return array
*/
public function getTplFiles(): array
{
return $this->tplFiles;
}

/**
* @param array $tplFiles
*/
public function setTplFiles(array $tplFiles): void
{
$this->tplFiles = $tplFiles;
}

/**
* @return string
*/
public function getTplDir(): string
{
return $this->tplDir;
}

/**
* @param string $tplDir
*/
public function setTplDir(string $tplDir): void
{
$this->tplDir = $tplDir;
}

/**
* @return string[]
*/
public function getAllowExt(): array
{
return $this->allowExt;
}

/**
* @param string[] $allowExt
*/
public function addAllowExt(array $allowExt): void
{
foreach ($allowExt as $ext) {
$this->allowExt[] = $ext;
}
}

/**
* @param string[] $allowExt
*/
public function setAllowExt(array $allowExt): void
{
$this->allowExt = $allowExt;
}

/**
* @return array
*/
public function getGlobalVars(): array
{
return $this->globalVars;
}

/**
* @param array $globalVars
*/
public function setGlobalVars(array $globalVars): void
{
$this->globalVars = $globalVars;
}

/**
* @param callable $pathResolver
*
* @return AbstractTemplate
*/
public function setPathResolver(callable $pathResolver): self
{
$this->pathResolver = $pathResolver;
return $this;
}
}
Loading

0 comments on commit fc06482

Please sign in to comment.