定制 dantleech/object-renderer 二次开发

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

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

dantleech/object-renderer

最新稳定版本:0.1.1

Composer 安装命令:

composer require dantleech/object-renderer

包简介

Render/pretty-print objects

README 文档

README

Build Status

Render / pretty print objects using Twig templates.

  • Templates are selected based on the FQN.
  • Templates are fallback based on class hierarchy.
  • Templates can render objects.

This library, or ones like it, could be suitable for:

  • Pretty print ReflectionClass and friends, (e.g. printing formatted documentation in a language server).
  • Building a CMS based on objects.
  • Other things.

Rendering an Object

Create a renderer and render an object:

$renderer = ObjectRendererBuilder::create()
    ->addTemplatePath('example/path')
    ->build();

$renderer->render(new \stdClass());

Will throw an exception:

Could not render object "stdClass" using templates "stdClass.twig"',

You can guess what you need to do, create stdClass.twig in the path given in the builder:

# stdClass.twig
Hello I am a stdClass

Object Properties and Recursive Rendering

The object is available as object in the template.

If the object contains other objects, you can recurisvely render them by calling render(object.anotherObject).

Ancestor Class Template Resolution

If a template for a given object's class is not found. The renderer will try and locate a template for each of the parent classes.

DOMDocument Example

{# DOMDocument.twig #}
DOMDocument:
{% for node in object.childNodes %}
    - {{ render(node) }}
{%- endfor -%}
{# DOMElement.twig #}
Element: "{{ object.nodeName }}"
{% for attribute in object.attributes %}
      {{ render(attribute) }}
{%- endfor -%}
{# DOMAttr.twig #}
{{ object.name }}: {{ object.value }}

Render them like:

$dom = new DOMDocument();
$child1 = $dom->createElement('child-1');
$child1->setAttribute('foo', 'bar');
$dom->appendChild($child1);
$child2 = $dom->createElement('child-2');
$child2->setAttribute('bar', 'foo');
$dom->appendChild($child2);

$renderer = ObjectRendererBuilder::create()
    ->addTemplatePath('example/path')
    ->build();

$renderer->render($dom);

Should return something like:

DOMDocument:
    - Element: "child-1"
      foo: bar
    - Element: "child-2"
      bar: foo

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-24