Download the PHP package migears/template without Composer

On this page you can find all versions of the php package migears/template. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package template

migears/template

Version

Minimalist PHP template engine — native PHP with optional ## ## syntax sugar.

PHP itself is already a template language. miGears Template adds just a few things on top: auto-escaping, layout inheritance, view components, and optional ## ## syntax sugar that compiles to pure PHP.

Background: miGears is the open-source successor of TinyGears, a self-developed PHP framework. It was renamed and open-sourced recently because the name TinyGears is already taken in the open-source community.

Features

Boundaries

In scope

Not in scope (by design)

Installation

Requires: PHP 8.1+.

Quick Start

Basic Template (Native PHP — .php files)

Use .php extension for native PHP templates:

Syntax Sugar (.tpl.php files)

Use .tpl.php extension for the ## ## syntax — auto-compiled to pure PHP at runtime:

Syntax rules:

Syntax Compiles to Description
## $expr ## <?= $this->e($expr) ?> Escaped output (auto HTML-escaping)
### $expr ### <?= $this->raw($expr) ?> Raw output (no escaping, for trusted HTML)
## section('name') ## <?= $this->section('name') ?> Output a section — not escaped: a section holds markup a template already wrote, so escaping it here would escape it twice
\## … \## literal ## Escaped hashes — a backslash before a run of two or more hashes keeps it literal

Control structures (if, foreach, for, while) use native PHP tags — this preserves IDE syntax highlighting, auto-completion, and error checking.

A ## section(...) ## block is recognised only as section('name') with a quoted literal name. Any other shape — a second argument, an empty or multi-line name, or a compound expression like section('x') . 'y' — is a compile error naming the template and the line, instead of reaching render time as a call to a global section(); write $this->section(...) inside ## ## for any other form.

How it works:

  1. .tpl.php files are compiled to pure PHP cache files on first render
  2. Cache is regenerated only when the source file changes (mtime check, at second granularity — a source edited within the same second as the cache file is not counted as newer)
  3. .php files run directly — zero compilation overhead
  4. Both can coexist in the same project

Manual compilation (for debugging):

Auto-Escaping

$this->e() handles strings, numbers, null (returns empty string), and arrays/objects (JSON-encoded then escaped).

Escaping is a property of the syntax you write, not a runtime switch: ## $expr ## compiles to $this->e($expr), ### $expr ### to $this->raw($expr), and a native <?= $expr ?> outputs exactly what you hand it. A global toggle is not offered — it would have to change what ## ## means per render, which the compiled cache (keyed by source path alone) cannot express.

Literal hashes are written with a backslash: \## compiles to a literal ##, \### to ###. Without it, a ## in the source is read as an output expression — the pass is text-level and does not distinguish markup from PHP code.

One trap follows: ## $this->raw($expr) ## is escaped anyway, because the sugar wraps whatever sits inside in $this->e(), so the call is a silent no-op. For raw output write ### $expr ### or native <?= $this->raw($expr) ?>.

Layout Inheritance

Layouts are single-level: one render applies exactly one layout, the one the page itself asks for. A layout must not call extends() — if it does, that call is silently ignored (no warning) and the page is not wrapped a second time, so what renders is that layout's own output. The page front ends built on this engine (migears-pages and its XML/YAML siblings) follow the same one-level rule: each page emits a single extends().

A render is isolated from the renders around it. A template may call $this->render('partial') to nest one render inside another, or inside a section it is currently capturing: the nested call neither reads the outer render's sections and layout nor leaves its own behind, and the outer state is put back even when the nested template throws. That is what keeps a failure deep inside a partial from taking the page's own sections down with it.

View Components

A component is self-contained: it is a fragment, not a page. It neither reads the sections captured around it nor applies a layout — an extends() call inside a component is silently ignored (no warning), and the component's own sections are isolated from its caller's. To render a full page use render('...'), not component('...').

Multiple Paths (Theme Support)

Templates are searched in reverse order of addPath() calls. First match wins. A name resolves inside those roots and nowhere else: .., . and empty path segments (and a NUL byte) are refused rather than looked up, so a name never reaches a file beside them.

API Reference

Method Description
render(string $template, array $data = []): string Render a template (.php or .tpl.php)
exists(string $template): bool Check if template exists
e(mixed $value, string $encoding = 'UTF-8'): string Escape for HTML output. Arrays and objects are JSON-encoded first; $encoding is passed to htmlspecialchars()
raw(mixed $value): string Output without escaping (trust required). Takes what e() takes, so ### $expr ### and ## $expr ## differ only in the escaping
extends(string $layout): void Set layout template
start(string $name): void Start capturing a section
end(): void End current section
section(string $name, string $default = ''): string Output section content
component(string $name, array $data = []): string Render a component
addPath(string $path): self Add template directory
setCacheDir(string $dir): self Set cache directory for compiled .tpl.php
getCompiler(): TemplateCompiler Get the compiler instance

Design Philosophy

PHP is already a template engine. miGears Template provides just the essential tools that every template engine needs:

  1. Safety — auto-escaping prevents XSS
  2. Reuse — layout inheritance and components reduce duplication
  3. Simplicity — you already know the syntax

## ## syntax sugar is optional — it compiles to pure PHP, so you can always see what's happening. Use .tpl.php for cleaner output syntax, or .php for native PHP. Both work seamlessly together.

What we don't do:

Integration with miGears Web

License

MIT


migears/template

Version

极简 PHP 模板引擎 — 原生 PHP + 可选 ## ## 语法糖。

PHP 本身就是模板语言。miGears Template 只在之上加了几件事:自动转义、布局继承、视图组件,以及可选的 ## ## 语法糖(自动编译为纯 PHP)。

特性

边界

范围内

范围外(刻意不做)

安装

要求:PHP 8.1+。

快速开始

基础模板(原生 PHP — .php 文件)

使用 .php 扩展名编写原生 PHP 模板:

语法糖(.tpl.php 文件)

使用 .tpl.php 扩展名编写 ## ## 语法 — 运行时自动编译为纯 PHP:

语法规则:

语法 编译结果 说明
## $expr ## <?= $this->e($expr) ?> 转义输出(自动 HTML 转义)
### $expr ### <?= $this->raw($expr) ?> 原始输出(不转义,用于信任的 HTML)
## section('name') ## <?= $this->section('name') ?> 输出区块——不转义:区块装的是模板已经写出的标记,在这里转义等于转义两次
\## … \## 字面 ## 转义井号——反斜杠加两个及以上井号,保持字面

控制结构(if、foreach、for、while)使用原生 PHP 标签 — 保留 IDE 语法高亮、自动补全和错误检查。

## section(...) ## 块只认带引号字面量名字的 section('name')。其余任何形态——多一个参数、空名字或多行名字、或 section('x') . 'y' 这样的复合表达式——都是编译期错误并指名模板与行号,不会拖到渲染期变成一次对全局 section() 的调用;其它写法请在 ## ## 里写 $this->section(...)。

工作原理:

  1. .tpl.php 文件首次渲染时编译为纯 PHP 缓存文件
  2. 源文件修改后才重新编译(mtime 检查,粒度为秒——与缓存文件同一秒内改动的源文件不会被判定为更新)
  3. .php 文件直接运行 — 零编译开销
  4. 两种文件可以在同一项目中共存

手动编译(用于调试):

自动转义

$this->e() 支持字符串、数字、null(返回空字符串)、数组/对象(JSON 编码后转义)。

转义取决于你写下的语法,而不是运行期开关:## $expr ## 编译为 $this->e($expr),### $expr ### 编译为 $this->raw($expr),而原生 <?= $expr ?> 原样输出你给的东西。本引擎不提供全局开关——那会要求 ## ## 的含义随渲染实例变化,而编译缓存只以源文件路径为键,表达不了这种差异。

由此有一个坑:## $this->raw($expr) ## 仍会被转义,因为糖会把里面的表达式整体包进 $this->e(),那次调用等于静默失效。要原样输出请写 ### $expr ### 或原生 <?= $this->raw($expr) ?>。

需要字面井号时在前面加反斜杠:\## 编译为字面 ##,\### 为 ###。不加反斜杠的 ## 一律被当作输出表达式——这一层是按文本扫描的,不区分标记还是 PHP 代码。

布局继承

布局只有一层:一次渲染只应用一个布局,即页面自身要求的那个。布局不得调用 extends()——若调用了,该调用会被静默忽略(无提示),页面不会被再包一层,最终渲染的就是该布局自身的输出。构建在本引擎之上的页面前端(migears-pages 及其 XML/YAML 同族)遵循同样的一层规则:每个页面只发出一次 extends()。

一次渲染与它周围的渲染相互隔离。模板可以调用 $this->render('partial'),把一次渲染嵌进另一次渲染,或嵌进它此刻正在捕获的区块里:被嵌套的那次既不读取外层渲染的区块与布局,也不会把自己的留在外面;即使被嵌套的模板抛异常,外层状态也会归位。正是这一点,让片段深处的失败不会顺手带走页面自己的区块。

视图组件

组件是自包含的:它是片段,不是页面。它既不读取外层捕获的区块,也不应用布局——组件内部的 extends() 调用会被静默忽略(无提示),组件自身的区块与调用方相互隔离。要渲染完整页面请用 render('...'),而不是 component('...')。

多目录(主题支持)

模板按 addPath() 调用的逆序查找,先找到的优先使用。名字只在这些根内解析,不会到根外去找:..、.、空路径段(以及 NUL 字节)一律拒绝,名字因此触达不到根旁的文件。

API 参考

方法 说明
render(string $template, array $data = []): string 渲染模板(.php 或 .tpl.php)
exists(string $template): bool 检查模板是否存在
e(mixed $value, string $encoding = 'UTF-8'): string HTML 转义输出。数组与对象先 JSON 编码;$encoding 传给 htmlspecialchars()
raw(mixed $value): string 不转义输出(需信任内容)。参数与 e() 一致,因此 ### $expr ### 与 ## $expr ## 只差转义
extends(string $layout): void 设置布局模板
start(string $name): void 开始捕获区块
end(): void 结束当前区块
section(string $name, string $default = ''): string 输出区块内容
component(string $name, array $data = []): string 渲染组件
addPath(string $path): self 添加模板目录
setCacheDir(string $dir): self 设置 .tpl.php 的编译缓存目录
getCompiler(): TemplateCompiler 获取编译器实例

设计哲学

PHP 本身就是模板引擎。miGears Template 只提供每个模板引擎都需要的核心工具:

  1. 安全 — 自动转义防止 XSS
  2. 复用 — 布局继承和组件减少重复
  3. 简洁 — 你已经懂语法了

## ## 语法糖是可选的 — 它编译为纯 PHP,你随时可以看到实际运行的代码。用 .tpl.php 获得更简洁的输出语法,或用 .php 写原生 PHP。两种方式无缝共存。

我们不做的事:

与 miGears Web 集成

许可证

MIT


All versions of template with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package migears/template contains the following files

Loading the files please wait ...