krowinski / laravel-xslt
为 Laravel 设计的 XSLT 模板引擎
3.0.0
2017-12-31 00:08 UTC
Requires
- php: >=7.0
- ext-xsl: *
- illuminate/console: 5.5.*
- illuminate/events: 5.5.*
- illuminate/queue: 5.5.*
- illuminate/support: 5.5.*
- illuminate/view: 5.5.*
Suggests
- barryvdh/laravel-debugbar: Allows displaying XML tree content for debug
README
为 Laravel 设计的 XSLT 模板引擎
安装
- 使用 composer 在您的 Laravel 项目中安装
composer require krowinski/laravel-xslt
- 将此行添加到 app.php 文件的 'providers' 数组末尾(在文件 config/app.php 中)
Krowinski\LaravelXSLT\XSLTServiceProvider::class,
- 在 resources/views 中创建 welcome.xsl
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" xmlns:str="http://exslt.org/strings" xmlns:php="https://php.ac.cn/xsl" exclude-result-prefixes="exslt str php"> <xsl:output encoding="UTF-8" method="xml" omit-xml-declaration="yes" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" cdata-section-elements="script"/> <xsl:template match="/"> <head> <title>Laravel</title> <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css"/> <style> html, body { height: 100%; } body { margin: 0; padding: 0; width: 100%; display: table; font-weight: 100; font-family: 'Lato'; } .container { text-align: center; display: table-cell; vertical-align: middle; } .content { text-align: center; display: inline-block; } .title { font-size: 96px; } </style> </head> <body> <div class="container"> <div class="content"> <div class="title">Laravel 5 XSLT engine template</div> </div> </div> </body> </xsl:template> </xsl:stylesheet>
- 使用 simple xml 函数添加数据到 xml
/** * Show the application welcome screen to the user. * * @return Response */ public function index() { // adds to main xml /App attributte name template with value = hello \View::addAttribute('name_template ', 'hello'); // create child template to /App with value hello and add aaa and zzz atribute to template. \View::addChild('template', 'hello', false)->addAttribute('aaaa', 'zzz'); // creates parent example and adds childs foo and bar to it \View::addArrayToXmlByChild(['foo', 'bar'], 'example', false); // add to parent App child bar and zzz \View::addArrayToXml(['bar', 'zzz'], false); return view('welcome'); }
将 xml 添加到 debugBar(https://github.com/barryvdh/laravel-debugbar)
添加到 EventServiceProvider.php
use Krowinski\LaravelXSLT\Engines\XSLTEngine;
并到 protected $listen 数组中
XSLTEngine::EVENT_NAME => [ 'App\Listeners\XSLTDebugBar' ],
创建文件 Listeners\XSLTDebugBar.php
php artisan make:listener XSLTDebugBar --event XSLTEngineEvent
事件内容
<?php namespace App\Listeners; use DebugBar\DataCollector\MessagesCollector; use DebugBar\DebugBar; use Illuminate\Support\Facades\App; use Krowinski\LaravelXSLT\Events\XSLTEngineEvent; /** * Class XSLTDebugBar * @package App\Listeners */ class XSLTDebugBar { /** * @param XSLTEngineEvent $event */ public function handle(XSLTEngineEvent $event) { $dom = new \DOMDocument; $dom->preserveWhiteSpace = false; $dom->loadXML($event->getExtendedSimpleXMLElement()->saveXML()); $dom->formatOutput = true; $xmlString = $dom->saveXML(); /** @var DebugBar $debugBar */ $debugBar = App::make('debugbar'); if (!$debugBar->hasCollector('XML')) { $debugBar->addCollector(new MessagesCollector('XML')); } /** @var MessagesCollector $collector */ $collector = $debugBar->getCollector('XML'); $collector->addMessage($xmlString, 'info', false); } }