Coder Social home page Coder Social logo

view's Introduction

Yii View Rendering Library


Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Quality Score Mutation testing badge static analysis type-coverage

This library provides templates rendering abstraction supporting layout-view-subview hierarchy, custom renderers with PHP-based as default and more. It is used in Yii Framework but is supposed to be usable separately.

Requirements

  • PHP 7.4 or higher.

Installation

The package could be installed via composer:

composer require yiisoft/view --prefer-dist

General usage

The package provides two use cases for managing view templates:

State of View and WebView services

While being immutable and, by itself, stateless, both View and WebView services have sets of stateful and mutable data.

View service:

  • parameters,
  • blocks,
  • theme,
  • locale.

WebView service:

  • parameters,
  • blocks,
  • theme,
  • locale,
  • title,
  • meta and link tags,
  • JS/CSS strings,
  • JS/CSS files.

The state of View and WebView is not cloned when the services are cloned. So when using with*(), both new and old instances are sharing the same set of stateful mutable data. It allows, for example, to get WebView via type-hinting in a controller and change context path:

final class BlogController {
    private WebView $view;
    public function __construct (WebView $view) {
        $this->view = $view->withContextPath(__DIR__.'/views');
    }
}

... and then register CSS in a widget:

final class LastPosts extends Widget 
{    
    private WebView $view;
    public function __construct (WebView $view) {
        $this->view = $view;
    }
    protected function run(): string
    {
        ...
        $this->view->registerCss('.lastPosts { background: #f1f1f1; }');
        ...
    }
}

Locale state

You can change the locale by using setLocale(), which will be applied to all other instances that used current state including existing ones. If you need to change the locale only for a single instance, you can use the immutable withLocale() method. Locale will be applied to all views rendered within views with render() calls.

Example with mutable method:

final class LocaleMiddleware implements MiddlewareInterface
{    
    ...
    private WebView $view;
    ...
    public function __construct (
        ...
        WebView $view
        ...
    ) {
        $this->view = $view;
    }
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        ...
        $this->view->setLocale($locale);
        ...
    }
}

Example with immutable method:

final class BlogController {
    private WebView $view;
    public function __construct (WebView $view) {
        $this->view = $view;
    }
    public function index() {
        return $this->view->withLocale('es')->render('index');
    }
}

Reset state

To get a deep cloned View or WebView use withClearedState():

$view = $view->withClearedState();

Extensions

  • yiisoft/yii-view - a wrapper that is used in Yii Framework. Adds additional functionality for a WEB environment and compatibility with PSR-7 interfaces.
  • yiisoft/view-twig - an extension that provides a view renderer that will allow you to use the Twig view template engine, instead of the default PHP renderer.

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii View Rendering Library is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

view's People

Contributors

samdark avatar terabytesoftw avatar vjik avatar devanych avatar machour avatar dependabot[bot] avatar xepozz avatar thenotsoft avatar roxblnfk avatar mister-42 avatar desure85 avatar mj4444ru avatar kamarton avatar razonyang avatar fantom409 avatar hiqsol avatar sanmai avatar dplusg avatar kaznovac avatar rustamwin avatar sankaest avatar viktorprogger avatar dependabot-preview[bot] avatar skugarev avatar yiiliveext 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.