PHPackages                             digphp/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. digphp/template

ActiveLibrary

digphp/template
===============

php template engine!

1.0.0(4y ago)0152GPL-3.0PHP

Since Mar 10Pushed 4y ago1 watchersCompare

[ Source](https://github.com/digphp/template)[ Packagist](https://packagist.org/packages/digphp/template)[ RSS](/packages/digphp-template/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (2)

PHP模板引擎
=======

[](#php模板引擎)

一个功能强大的模板引擎，非常灵活

安装
--

[](#安装)

```
composer require digphp/template
```

用例
--

[](#用例)

```
$template = new \DigPHP\Template\Template('可以传入一个 Psr\SimpleCache\CacheInterface 实例，传入后会开启模板缓存，提高效率');

// 添加分区
$template->addPath('book','/www/template/book');
$template->addPath('cms','/www/template/cms', 1);
$template->addPath('cms','/www/template/cms2', 2);

// 声明变量
$template->assign('name','小刘');
$template->assign('sex','男');
$template->assign([
    'name'=>'小刘',
    'sex'=>'男'
]);

// 渲染
$template->renderFromFile('content@cms'); // 渲染cms分区下的content模板 系统会根据权重顺序在添加的分区目录中依次寻找content模板
$template->renderFromFile('index@book'); // 渲染book分区下的index模板

// 直接渲染模板字符串
$template->renderFromString('hello {$name}', ['name'=>'隔壁小王']); // 直接渲染字符串模板

// 语法扩展
// 定一个{datetime}，渲染成当前时间
$template->extend('/\{datetime\}/Ui', function ($matchs) {
    return '';
});
$template->renderFromString('当前时间是：{datetime}');
```

支持的模板语法
-------

[](#支持的模板语法)

### 变量输出

[](#变量输出)

所有的变量输出都进行了htmlspecialchars编码

若要原始输出，可使用{echo $name} {echo $vo\['title'\]} {php echo $vo\['title'\]}

```
{$name}
{$vo.title}
{$vo['title']}

// 原始输出
{echo $name}
{echo $vo['title']}
{php echo $vo['title']}
```

### 使用函数

[](#使用函数)

```
{:mb_substr($title, 0, 80)}
{:date('Y-m-d', $time)}

// 原始输出
{echo date('Y-m-d', time())}
{php echo date('Y-m-d', time())}
{php print_r($list)}
```

### function 定义函数

[](#function-定义函数)

```
// 定义函数
{function somefunc($a, $b)}
{$a}与{$b}{$b}求和，其值是{echo $a+$b}
{/function}

// 执行函数
{echo somefunc(1,2)}
```

### php 原始代码

[](#php-原始代码)

```
// 方式一

// 方式二
{php echo $title}

// 方式三
{php}
echo $title;
{/php}
```

### include 引入模板

[](#include-引入模板)

```
{include common/header@cms}
{include common/footer@cms}
```

### foreach 循环

[](#foreach-循环)

```
{foreach $list as $key=>$vo}
{$key}:{$vo}
{/foreach}
```

### for 循环

[](#for-循环)

```
{for $i=0; $i < 10; $i++}
{$i}
{/for}
```

### if else 条件判断

[](#if-else-条件判断)

```
{if isset($a)}
// somecode
{elseif $b==1}
// somecode
{else}
// somecode
{/if}
```

### switch 条件

[](#switch-条件)

```
{switch $a}

{case 1}
// some..
{/case}

{case 2}
// some..
{/case}

{default}
// some..
{/default}

{/switch}
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1531d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f462934f8ed14974526240d3f3732144c90bef87038d8ede35a70dc315610696?d=identicon)[digphp](/maintainers/digphp)

---

Top Contributors

[![wseng](https://avatars.githubusercontent.com/u/6572161?v=4)](https://github.com/wseng "wseng (1 commits)")

### Embed Badge

![Health badge](/badges/digphp-template/health.svg)

```
[![Health](https://phpackages.com/badges/digphp-template/health.svg)](https://phpackages.com/packages/digphp-template)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.7k509.9M17.0k](/packages/laravel-framework)[phpoffice/phpspreadsheet

PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine

13.9k293.5M1.3k](/packages/phpoffice-phpspreadsheet)[maatwebsite/excel

Supercharged Excel exports and imports in Laravel

12.7k144.3M712](/packages/maatwebsite-excel)[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[illuminate/contracts

The Illuminate Contracts package.

705122.9M10.1k](/packages/illuminate-contracts)[algolia/algoliasearch-client-php

API powering the features of Algolia.

69433.0M114](/packages/algolia-algoliasearch-client-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
