定制 germania-kg/renderer 二次开发

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

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

germania-kg/renderer

Composer 安装命令:

composer require germania-kg/renderer

包简介

Render Callables for PHP files, Twig, Smarty and Markdown

README 文档

README

Render Callables for PHP files, Twig, Smarty and Markdown

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation with Composer

$ composer require germania-kg/renderer
"require": {
    "germania-kg/renderer":"^1.0|^2.0|^3.0"
}

The v3 release of germania-kg/renderer supports Twig v2 and v3.

Usage

All classes PhpRenderer, TwigRenderer, RenderedMarkdownRenderer, and SmartyRenderer extend \Germania\Renderer\RendererAbstract and implement the RendererInterface:

interface RendererInterface {
    /**
     * @param  string   $template   The template file
     * @param  array    $context    Associative template variables array
     * @return string   Template output
     */	
     public function render( $template, array $context = array()) : string
  
    /**
     * Callable alias for render()
     */	
     public function __invoke( $template, array $context = array())
}

PhpRenderer

This RendererInterface implentation will include a PHP file, using output buffering. Passed context variables are extracted to __invoke method scope und thus are locally available inside the PHP include file.

<?php
use Germania\Renderer\PhpRenderer;

// Base path and PSR-3 Logger are optional.
// Base path defaults to PHP's getcwd()
$php = new PhpRenderer;
$php = new PhpRenderer( '/path/to/includes' );
$php = new PhpRenderer( array('/path/to/includes', '/another/path') );
$php = new PhpRenderer( '/path/to/includes', $logger );

// Pass file name and variable context:
echo $php('myinc.php', [
	'foo'  => 'bar',
	'user' => $container->get('var')
]);

PSR-7 HTTP Messages

If the include file itselfs returns a PSR-7 ResponseInterface, the PhpRenderer will return this ResponseInterface instance.

<?php
// myinc.php
return $response;
<?php
use Psr\Http\Message\ResponseInterface;

$render = new PhpRenderer;

$result = $render('myinc.php', [
	'response' => new GuzzleHttp\Psr7\Response
]);

echo $result instanceOf ResponseInterface
? $result->getBody()
: $result;

TwigRenderer

<?php
use Germania\Renderer\TwigRenderer;

// Have your Twig_Environment at hand;
// Logger is optional.
$render_twig = new TwigRenderer( $twig, $logger ) ;

// Pass file name and variable context:
echo $render_twig('mytwig.tpl', [
	'foo'  => 'bar',
	'user' => $container->get('var')
]);

SmartyRenderer

<?php
use Germania\Renderer\SmartyRenderer;

// Have your Smarty3 at hand;
// Logger is optional.
$render_smarty = new SmartyRenderer( $smarty ) ;
$render_smarty = new SmartyRenderer( $smarty, $logger ) ;

// Pass file name and variable context:
echo $render_smarty('mysmarty.tpl', [
	'foo'  => 'bar',
	'user' => $container->get('var')
]);

RenderedMarkdownRenderer

Sometimes it is useful to 'process' a markdown source code first with a real template engine before markdown-parsing. RenderedMarkdownRenderer takes a RendererInterface instance and any of Carsten Brandt's cebe/markdown flavours.

<?php
use Germania\Renderer\TwigRenderer;
use Germania\Renderer\RenderedMarkdownRenderer;
use cebe\markdown\Markdown;

// Have a RendererInterface instance at hand,
// as well as Carsten Brandt's Markdown Parser.

$twig_render = new TwigRenderer( $twig, $logger );
$markdown = new Markdown;

// Pass them to constructor:
$rendered_markdown_renderer = new RenderedMarkdownRenderer($twig_render, $markdown);

// Pass file name and variable context:
echo $rendered_markdown_renderer('twigged_markdown.md', [
	'foo'  => 'bar',
	'user' => 'Joe'
]);

Issues

…As always, some documentation missing here and there. Stay up to date on issues list.

Development

$ git clone https://github.com/GermaniaKG/Renderer.git
$ cd Renderer
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固