定制 seanmorris/theme 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

seanmorris/theme

Composer 安装命令:

composer require seanmorris/theme

包简介

Simple, sane templating and theming for PHP

README 文档

README

Simple, sane templating and theming for PHP

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

The aim of the Theme library is to provide a start separation of presentation from logic. It has only two responsibilities:

  • Mapping objects to Views
  • Rendering those Views

Composer

Just run composer require seanmorris/theme in your project directory.

You can also add "seanmorris/theme": "^1.0.0" to the require array in your project's composer.json.

"require": {
  "seanmorris/theme": "^1.0.0"
}

Templating

Coupling the template with the View class is very simple. Simply subclass the provided View and append the template after a call to __halt_compiler(); (WITH THE CLOSING ?>) like so:

(note: Short tags are enabled for simple echo statements as of PHP 5.4, but are not required)

<?php
class FooView extends \SeanMorris\Theme\View
{
}
__halt_compiler(); ?>
<h1>FooView</h1>
<span class = "some_class"><?=$a;?></span>
<p><?=$b;?>. <b><?=$c;?></b></p>

Pass an associative array into the constructor to populate the variables in the template. The keys of the array will be translated to variable names.

<?php
$view = new FooView([
  'a' => 'value'
  , 'b' => 'value'
  , 'c' => 'value'
]);

echo $view;

Preprocessing

Preprocessing templates is simple. Just implement the preprocess method in your view class, and you'll get a chance to operate on the variables prior to rendering.

<?php
class FooView extends \SeanMorris\Theme\View
{
  public function preprocess(&$vars)
  {
    $vars['a'] = $vars['a'] . '...';
    $vars['b'] = $vars['b'] . '?';
    $vars['c'] = $vars['b'] . '!'
  }
}
__halt_compiler(); ?>
<h1>FooView</h1>
<span class = "some_class"><?=$a;?></span>
<p><?=$b;?>. <b><?=$c;?></b></p>

Usage:

<?php
$view = new FooView([
  'a' => 'value'
  , 'b' => 'value'
  , 'c' => 'value'
]);

echo $view;

Theming

Creating a theme is as simple as extending the theme class and providing a mapping from your object classes to their view classes, as shown:

<?php
class Theme extends \SeanMorris\Theme\Theme
{
  protected static
  $view = [
    'SeanMorris\Foo' => 'SeanMorris\Theme\FooView'
  ];
}

Usage:

<?php echo Theme::render(new Foo(...)); ?>

Wrapping

If you've got a default "trim" you'd like to use to wrap everything (i.e. the view that contains your , and structure), simply set the static property $wrap to an array listing your wrappers, innermost to outtermost.

<?php
class Theme extends \SeanMorris\Theme\Theme
{
  protected static
  $wrap = [
    'SeanMorris\Theme\Wrapper'
    , 'SeanMorris\Theme\HtmlDocument' 
  ];
}

Usage:

<?php
$bodyText = 'Lorem ipsum dolor sit amet...';
echo Theme::wrap($bodyText);

Advanced Stuff...

Although the library doesn't do much, its got some power under the hood.

Fallback Themes

If a theme cannot render an object, it can defer the rendering to other themes that can. This is done by specifying the $themes static property. The list will be check in order, until a theme is able to render a given object.

<?php
class Theme extends \SeanMorris\Theme\Theme
{
  protected static
  $themes = [
    'SeanMorris\SomeTheme\Theme'
    , 'SeanMorris\SomeOtherTheme\Theme'
  ];
}

Subclassing Views

You can subclass any view class and keep the template by ommitting the call to __halt_compiler(), and extending the existing view. You'll probably want to override the parent preprocessor as well.

<?php
class FoozleView extends FooView
{
  public function preprocess(&$vars)
  {
    parent::preprocess($vars);
    $vars['a'] = $vars['object']->a . 'DIFFERENT!!!';
  }
}

Contextualized Themeing

By defining mappings from classes to views in the $contextViews array, you can specify that a view should be rendered differently when the render call is made from certain classes.

In this example SeanMorris\Stuff\Foo will be rendered with the SeanMorris\Theme\FooAlternateView class when rendered inside of the SeanMorris\Stuff\RandomObject class, but outside, it will be rendered with SeanMorris\Theme\FooView.

<?php
class Theme extends \SeanMorris\Theme\Theme
{
  protected static
  $contextView = [
    'SeanMorris\Stuff\RandomObject' => [
      'SeanMorris\Stuff\Foo' => 'SeanMorris\Theme\FooAlternateView'
    ]
  ]
  , $view = [
    'SeanMorris\Stuff\Foo' => 'SeanMorris\Theme\FooView'
  ];
}

seanmorris/theme 适用场景与选型建议

seanmorris/theme 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.55k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 seanmorris/theme 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 seanmorris/theme 我们能提供哪些服务?
定制开发 / 二次开发

基于 seanmorris/theme 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2015-09-21