lroot/simple-view
Composer 安装命令:
composer require lroot/simple-view
包简介
A single class file implementing the core features of a view system including layouts, partials and placeholders
README 文档
README
A single class implementing the core features of a view system including layouts, partials, placeholders and caching.
Configuration
You can configure SimpleView to work with your directory structure:
// Setup Configuration SimpleView::setConfigProperties(array( SimpleView::CONFIG_VIEW_DIR => YOUR_APP_PATH.'/app/views/', SimpleView::CONFIG_LAYOUT_DIR => YOUR_APP_PATH.'/app/views/_layouts/', SimpleView::CONFIG_PARTS_DIR => YOUR_APP_PATH.'/app/views/_partials/' ));
Rendering
Rendering a view is simple:
// Output your view script. Parameters: view script, data & layout echo SimpleView::render('your-view-script', array('name'=>'Amanda'), 'your-layout');
You specify your view script, pass an associative array of data and indicate which layout you would like to wrap the view script in. You can access this passed in data as standard variables within the view script. For example array('name'=>'amanda') will become $view_name.
Views
View scripts are the primary template file. They contain the core layout for the view you are rendering. Within the layout you can include Partials content and define placeholder content for use within the enclosing layout.
<?php SimpleView::placeholderCaptureStart(SimpleView::PLACEHOLDER_HEAD_CONTENT); ?> <style> /* CSS content... */ /* consider outputting this placeholder content in the head of your layout */ </style> <?php SimpleView::placeholderCaptureEnd(); ?> <?php echo SimpleView::partial('title.php',array('name'=>$view_name)); ?> <p>Example conetent lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <?php echo SimpleView::placeholderCaptureStart(SimpleView::PLACEHOLDER_INLINE_SCRIPTS);?> <script> // ... mind blowing js code ... // consider outputting this placeholder content at the bottom of your layout </script> <?php SimpleView::placeholderCaptureEnd(); ?>
Placeholders
Placeholders allow you to associate content with a name that you can reference later.
SimpleView::placeholderSetContent('placeholder_name','placeholder content');
You can also "capture" placeholder content.
<?php SimpleView::placeholderCaptureStart('placeholder_name'); ?> All content will be captured until we call placeholderCaptureEnd() <?php SimpleView::placeholderCaptureEnd(); ?>
Getting placeholder content is simple.
echo SimpleView::placeholderGetContent('placeholder_name');
Naming things is hard so there is a set of common placeholder names if you wish.
<?php SimpleView::placeholderCaptureStart(SimpleView::PLACEHOLDER_INLINE_SCRIPTS);?> <script> // ... mind blowing js code ... // consider outputting this at the bottom of your layout </script> <?php SimpleView::placeholderCaptureEnd(); ?>
Partials
Partials are like small, self contained view scripts. You can pass an associative array of data to partial templates too.
// Execute a partial script and echo its content echo SimpleView::partial('title.php',array('name'=>'Amanda'));
Partial templates are simple php files. The array of data will be available as variables within the partial template.
<!--Super simple partial template--> <h1>Hello <?=$parts_name?></h1>
Layouts
Layouts are your "master" templates pulling together all your content.
<!DOCTYPE html> <html> <head> <?php // This content could be set from anywhere including the view script or a // partial included by the view script echo SimpleView::placeholderGetContent(SimpleView::PLACEHOLDER_HEAD_CONTENT); ?> </head> <body> <?php // This placeholder name is special. It will be populated with the contents // of your rendered layout. echo SimpleView::placeholderGetContent(SimpleView::PLACEHOLDER_TMPL_CONTENT); ?> <?php // This content could be set from anywhere as well echo SimpleView::placeholderGetContent(SimpleView::PLACEHOLDER_FOOTER_CONTENT); ?> </body> </html>
Further Documentation
SimpleView has extensive code level documentation and examples. To learn more about how SimpleView works just read through the code.
Todo
- Add tests
- Add example dir with functional layout, placeholders and partials
- make specifying partial and view scripts the same (fix extension requirement for partials)
- Show how to handle namespace use in templates
lroot/simple-view 适用场景与选型建议
lroot/simple-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.32k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「template」 「view」 「renderer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lroot/simple-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lroot/simple-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lroot/simple-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
统计信息
- 总下载量: 1.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-10