Giter Club home page Giter Club logo

php-webapp-render-utils's Introduction

Webapp Renderer

This library allows to create PSR-7 ResponseInterface objects for given content.

Supported renderers are:

  • Redirect
  • String
  • JSON
  • CSV
  • File
  • cURL response
  • View

Contents

View Renderer

The most complex and powerful renderer is View, which allows to catch outputs from a PHP file into a PSR-7 ResponseInterface object.

Catches any type of output like generated PDFs, Spreadsheets or HTML documents.

Rendering a view

$renderer->renderView('/home/index', [
    'title' => 'Hello World!',
    'paragraph' => 'Hello everyone.'
]);

View file /home/index.php:

<html>
    <head>
        <title><?=$title?></title>
    </head>
    <body>
        <h1><?=$title?></h1>
        <p><?=$paragraph?></p>
    </body>
</html>

Renderer result:

<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <p>Hello everyone.</p>
    </body>
</html>

Including subviews

Include file /includes/head.php.

<head>
    <title><?=$title?></title>
</head>

View file /home/index.php

<html>
    <?=$view->include('includes/head')=?>
    <body>
        <h1><?=$title?></h1>
        <p><?=$paragraph?></p>
    </body>
</html>

Using layouts

Layout file /layouts/main.php:

<html>
    <head>
        <title><?=$title?></title>
    </head>
    <body>
        <?=$layout->section('content')?>
    </body>
</html>

View file /home/index.php:

<?=$layout = $view->loadLayout('/layouts/main')?>
<?=$layout->startSection('content')?>
<h1><?=$title?></h1>
<p><?=$paragraph?</p>
<?=$layout->endSection()?>

NOTE
When using Layouts, everything outside a section will be ignored, therefore will not be renderered.

Library API

Class Renderer

class Renderer
{
    public function __construct(
        ResponseFactoryInterface $responseFactory,
        ?StreamFactoryInterface $streamFactory = null
    );

    public function renderRedirect(
        $location,
        int $code = StatusCodeInterface::STATUS_FOUND,
        ?ResponseInterface $response = null
    ): ResponseInterface;

    public function render(
        $content,
        $contentType = 'text/plain',
        ?ResponseInterface $response = null
    ): ResponseInterface;

    public function renderJson(
        $data,
        ?ResponseInterface $response = null
    ): ResponseInterface;

    public function renderCsv(
        array $data,
        $filename = 'file.csv',
        ?CsvOptions $options = null,
        ?ResponseInterface $response = null
    ): ResponseInterface;

    public function renderFile(
        string $filepath,
        ?string $filename = null,
        bool $attachment = false,
        ?ResponseInterface $response = null
    ): ResponseInterface;

    public function renderCurlResponse(
        CurlHandle $curl,
        string $responseBody,
        ?ResponseInterface $response = null
    ): ResponseInterface;

    public function setViewsBasePath(?string $basepath);

    public function renderView(
        string $viewpath,
        array $data = [],
        ?ResponseInterface $response = null
    ): ResponseInterface;
}

Class View

An instance of this class is automatically created when $renderer->renderView() method is invoked. Instance is passed into views with name $view.

class View
{
    /**
     * Renders a header directly into the view.
     * 
     * @param string $header Defines the header name or name and content.
     * @param string|string[] $content Defines the content of given header.
     */
    public function header(string $header, string|string[] $content = []);

    /**
     * Embeds another view inside the current view.
     * 
     * All `$view` variables are passed automatically into included view.
     * 
     * @param string $path Path to embed view.
     * @param mixed[] $vars Additional vars to send to embed view.
     */
    public function include(string $path, array $vars = []);

    /**
     * Loads a layout to current view.
     * 
     * By loading a layout only content inside a section will be renderer in
     * final output.
     * 
     * All `$view` variables are passsed automatically into layout.
     * 
     * @param string $path Path to layout view.
     * @param mixed[] $vars Additional vars to send to layout.
     * 
     * @return Layout
     */
    public function loadLayout(string $path, array $vars = []);
}

Class Layout

An object of this class is created when $view->loadLayout() method is invoked.

This class contains methods to allow main view file to define sections.

class Layout
{
    /**
     * This method starts a section block.
     */
    public function startSection(string $sectionName);

    /**
     * This method ends a section block.
     * 
     * This methods ends section by the last started.
     */
    public function endSection();
}

Class LayoutView

An object of this classe is passed into Layout Views as $layout.

class LayoutView
{
    /**
     * Embeds section into the layout.
     */
    public function section(string $sectionName);
}

php-webapp-render-utils's People

Contributors

francerz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.