Giter VIP home page Giter VIP logo

widget's Introduction

Yii

Yii Widget


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

Widgets are reusable building blocks used to create complex and configurable user interface elements in an object-oriented fashion.

This package provides an abstract class and a factory for creating widgets, ready-made widgets are provided in the yiisoft/yii-widgets package.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with Composer:

composer require yiisoft/widget

General usage

In order to implement your own widget, you need to create a class that extends the abstract class Yiisoft\Widget\Widget. In most cases it is enough to implement render() method.

final class MyWidget extends \Yiisoft\Widget\Widget
{
    public function render(): string
    {
        return 'My first widget.'.
    }
}

To get the string "My first widget." in the view, call the widget() method. Inside which the Yiisoft\Widget\WidgetFactory will create an instance of the MyWidget, and when converting the object to a string, the declared render() method will be called.

<?= MyWidget::widget() ?>

The Yiisoft\Widget\WidgetFactory factory uses a Factory instance to create widget objects, so you can require dependencies by listing them in your widget's constructor and set default values when initializing the factory. To initialize the widget factory call WidgetFactory::initialize() once before using widgets:

/**
 * @var \Psr\Container\ContainerInterface $container
 */
 
$widgetDefaults = [
    MyWidget::class => [
        'withNumber()' => [42],
    ],
];

\Yiisoft\Widget\WidgetFactory::initialize($container, $widgetDefaults);

It is a good idea to do that for the whole application. See Yii example in the configuration file of this package config/bootstrap.php.

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Widget 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

widget's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar devanych avatar fantom409 avatar luizcmarin avatar roxblnfk avatar rustamwin avatar samdark avatar sankaest avatar terabytesoftw avatar viktorprogger avatar vjik avatar xepozz avatar yiiliveext avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

widget's Issues

Remove yiisoft/di dependency

This is not yii-* package, but him have dependecy from yiisoft/di.
I think this is not correct and should be fixed.

Installation failed

What steps will reproduce the problem?

Installation

What do you get instead?

Problem 1
- Can only install one of: psr/container[1.1.1, 1.0.0].
- Can only install one of: psr/container[1.0.0, 1.1.1].
- Can only install one of: psr/container[1.0.0, 1.1.1].
- yiisoft/widget 3.0.x-dev requires psr/container 1.0.0 -> satisfiable by psr/container[1.0.0].
- Installation request for yiisoft/widget ^3.0@dev -> satisfiable by yiisoft/widget[3.0.x-dev].
- Installation request for psr/container (locked at 1.1.1) -> satisfiable by psr/container[1.1.1].

Additional info

Q A
PHP version 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )
Operating system ubuntu 20.04
Yii Version version 2.0.41.1.

Make begin and end non-static

<?php $myWidget = MyWidget::widget()->begin() ?>
  test
<?= $myWidget->end() ?>

That would make code simpler w/o making templates syntax worse.

Unable to install when using composer 2.0. Conflicting psr/container requirements

What steps will reproduce the problem?

Upgrade to composer 2.0
Try to require yiisoft/widget into an existing project

What is the expected result?

yiisoft/widget should install

What do you get instead?

composer require --prefer-dist yiisoft/widget
Using version 3.0.x-dev for yiisoft/widget
./composer.json has been updated
Running composer update yiisoft/widget
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - yiisoft/widget dev-master requires psr/container ^1.0 -> found psr/container[1.0.0, 1.1.0, 1.1.1, 1.1.x-dev] but it conflicts with your root composer.json require (^2.0).     
    - yiisoft/widget 3.0.x-dev is an alias of yiisoft/widget dev-master and thus requires it to be installed too.
    - Root composer.json requires yiisoft/widget 3.0.x-dev -> satisfiable by yiisoft/widget[3.0.x-dev (alias of dev-master)].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Additional info

composer 2 loads psr/container ^2 but yiisoft/widgets requires psr/container ^1
problem can be worked around by downgrading psr/container and updating with all depdendancies

Q A
Version 3.0.x-dev
PHP version 7.4
Operating system

The `Widget` class don't work directly without initialization

What steps will reproduce the problem?

AnyWidget::widget(), where AnyWidget's ancestor is Yiisoft\Widget\Widget

What is the expected result?

Widget instantiating.

What do you get instead?

TypeError
Argument 1 passed to Yiisoft\Widget\Widget::__construct() must implement interface Psr\EventDispatcher\EventDispatcherInterface, null given, called in view/src/Widget/Widget.php on line 109

Additional info

There is $this->container->get(Widget::class); in view/tests/TestCase.php. After this initialization AnyWidget::widget() works properly. Reason: \Yiisoft\Widget\Widget::__construct sets dependencies to static fields.

Q A
Version master [8d9f5ea2770835c05b49281a6ee53bbf24428a1d]
PHP version 7.3
Operating system *nix

Widgets and DI question

Good day. I have are question with using widgets and dependency injection - how to set/change base options using DI? For example i want change options for all Yiisoft\Yii\DataView\GridView on pages. I wrote that config/web/widgets.php

<?php

declare(strict_types=1);

use Yiisoft\Yii\DataView\GridView;

return [
    GridView::class => [
        '$options' => [
            'class' => 'table-responsive'
        ],
        'tableOptions()' => [
            'class' => 'table table-sm table-striped table-hover'
        ]
    ]
];

If i remove () or $ - i have error metadata "***" is not allowed, but with this it whatever not working. Thanks

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.