popphp/pop-view
Composer 安装命令:
composer require popphp/pop-view
包简介
Pop View Component for Pop PHP Framework
README 文档
README
Overview
pop-view is the view template component that can be used as the "V" in an MVC stack or
independently as well. It supports using both PHP-file based templates and stream templates.
Within the stream templates, there is basic support for logic and iteration for dynamic
control over the view template.
pop-view is a component of the Pop PHP Framework.
Install
Install pop-view using Composer.
composer require popphp/pop-view
Or, require it in your composer.json file
"require": {
"popphp/pop-view" : "^4.0.4"
}
Quickstart
Consider a phtml template file like this:
<html>
<body>
<h1><?=$title; ?></h1>
</body>
</html>
You can set up a view object and populate data like this:
use Pop\View\View; use Pop\View\Template\File; $view = new View(new File('hello.phtml')); $view->title = 'Hello World!'; echo $view;
which will produce:
<html> <body> <h1>Hello World!</h1> </body> </html>
File Template
A file template simply uses PHP variables to deliver the data and content to template to be rendered. With a file template, you have full access to the PHP environment to write any additional code or helper scripts. However, in using this, you must make sure to adhere to the best practices and standards regarding the security of the application.
hello.phtml
<!DOCTYPE html> <html> <head> <title><?=$title; ?></title> </head> <body> <h1><?=$title; ?></h1> <p><?=$content; ?></p> </body> </html>
You can set up the view object like this:
use Pop\View\View; use Pop\View\Template\File; $view = new View(new File('hello.phtml')); $view->title = 'Hello World!'; $view->content = 'This is a test!'; echo $view;
Stream Template
A stream template uses a formatted string placeholder to deliver the data and content to template to be rendered:
hello.html
<!DOCTYPE html> <html> <head> <title>[{title}]</title> </head> <body> <h1>[{title}]</h1> <p>[{content}]</p> </body> </html>
You can set up the view object in a similar way and it will render the exact same as the file template example.
use Pop\View\View; use Pop\View\Template\Stream; $view = new View(new Stream('hello.html')); $view->title = 'Hello World!'; $view->content = 'This is a test!'; echo $view;
Includes
Stream templates support includes to allow you to include other templates within them.
header.html
<!-- header.html //--> <!DOCTYPE html> <html> <head> <title>[{title}]</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <header>This is the header</header>
footer.html
<!-- footer.html //--> <footer>This is the footer</footer> </body> </html>
index.html
<!-- index.html //--> {{@include header.html}} <h1>[{title}]</h1> <p>[{content}]</p> {{@include footer.html}}
You can set up the view object like before:
use Pop\View\View; use Pop\View\Template\Stream; $view = new View(new Stream('index.html')); $view->title = 'Hello World!'; $view->content = 'This is a test!'; echo $view;
Inheritance
Stream templates support inheritance to allow you to extend other templates.
parent.html
<!-- parent.html //--> <!DOCTYPE html> <html> <head> {{header}} <title>[{title}]</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> {{/header}} </head> <body> <h1>[{title}]</h1> [{content}] </body> </html>
child.html
<!-- child.html //--> {{@extends parent.html}} {{header}} {{parent}} <style> body { margin: 0; padding: 0; color: #bbb;} </style> {{/header}}
You can set up the view object like before:
use Pop\View\View; use Pop\View\Template\Stream; $view = new View(new Stream('child.html')); $view->title = 'Hello World!'; $view->content = 'This is a test!'; echo $view;
Iteration
Iteration is possible in stream templates when working with arrays and array-like objects.
<!-- index.html //--> <!DOCTYPE html> <html> <head> <title>[{title}]</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> [{items}] <div><strong>[{key}]</strong>: [{value}]</div> [{/items}] </body> </html>
use Pop\View\View; use Pop\View\Template\Stream; $data = [ 'items' => [ 'hello' => 'world', 'foo' => 'bar', 'baz' => 123 ] ]; $view = new View(new Stream('index.html'), $data); echo $view;
Conditionals
Conditional logic is possible within a stream template as well.
<!-- index.html //--> <!DOCTYPE html> <html> <head> <title>[{title}]</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> [{if(foo)}] <p>The variable 'foo' is set to [{foo}].</p> [{else}] <p>The variable 'foo' is not set.</p> [{/if}] </body> </html>
use Pop\View\View; use Pop\View\Template\Stream; $data = ['foo' => 'bar']; $view = new View(new Stream('index.html'), $data); echo $view;
popphp/pop-view 适用场景与选型建议
popphp/pop-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.43k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2014 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「template」 「view」 「pop」 「pop php」 「view template」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 popphp/pop-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 popphp/pop-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 popphp/pop-view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Illuminate View for Morningmedley.
Blade template for Kirby
Simple ASCII output of array data
View5 is a version of Blade Templates for the Mvc5 Framework
E2E Studios PHP Framework adaptation of leafsphp/blade package
统计信息
- 总下载量: 9.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 7
- 依赖项目数: 5
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-05