{"id":20309559,"url":"https://github.com/pollen-solutions/partial","last_synced_at":"2025-03-04T07:47:15.871Z","repository":{"id":57043419,"uuid":"397193661","full_name":"pollen-solutions/partial","owner":"pollen-solutions","description":"Pollen Solutions - Partial Component - Layer and tools for creating reusable web user interfaces","archived":false,"fork":false,"pushed_at":"2021-08-18T07:21:33.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-14T11:58:17.629Z","etag":null,"topics":["partial","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pollen-solutions.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-17T09:37:28.000Z","updated_at":"2021-08-18T07:21:35.000Z","dependencies_parsed_at":"2022-08-24T04:50:44.911Z","dependency_job_id":null,"html_url":"https://github.com/pollen-solutions/partial","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollen-solutions%2Fpartial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollen-solutions%2Fpartial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollen-solutions%2Fpartial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollen-solutions%2Fpartial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pollen-solutions","download_url":"https://codeload.github.com/pollen-solutions/partial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241809603,"owners_count":20023786,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["partial","php"],"created_at":"2024-11-14T17:27:48.580Z","updated_at":"2025-03-04T07:47:15.850Z","avatar_url":"https://github.com/pollen-solutions.png","language":"PHP","readme":"# Partial Component\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/pollen-solutions/partial.svg?style=for-the-badge)](https://packagist.org/packages/pollen-solutions/partial)\n[![MIT Licensed](https://img.shields.io/badge/license-MIT-green?style=for-the-badge)](LICENSE.md)\n[![PHP Supported Versions](https://img.shields.io/badge/PHP-\u003e=7.4-8892BF?style=for-the-badge\u0026logo=php)](https://www.php.net/supported-versions.php)\n\nPollen Solutions **Partial** Component provides layer and tools for creating reusable web user interfaces.\n\n## Installation\n\n```bash\ncomposer require pollen-solutions/partial\n```\n\n## Basic Usage\n\n### From a callable\n\n```php\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello', function () {\n    return 'Hello World !';\n});\n\necho $partial-\u003eget('hello');\n```\n\n### From the default partial tag driver\n\n```php\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello');\n\necho $partial-\u003eget('hello', ['content' =\u003e 'Hello World !']);\n```\n\n### From a custom driver\n\n```php\nuse Pollen\\Partial\\PartialDriver;\nuse Pollen\\Partial\\PartialManager;\n\nclass HelloPartial extends PartialDriver\n{\n    public function render() : string{\n        return 'Hello World !';\n    }\n}\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello', HelloPartial::class);\n\necho $partial-\u003eget('hello');\n```\n\n### Through a PSR-11 depency injection container\n\n```php\nuse Pollen\\Container\\Container;\nuse Pollen\\Partial\\PartialDriver;\nuse Pollen\\Partial\\PartialManager;\n\n$container = new Container();\n\n$partial = new PartialManager();\n$partial-\u003esetContainer($container);\n\nclass HelloPartial extends PartialDriver\n{\n    public function render() : string{\n        return 'Hello World !';\n    }\n}\n\n$container-\u003eadd('helloPartialService', HelloPartial::class);\n\n$partial-\u003eregister('hello', 'helloPartialService');\n\necho $partial-\u003eget('hello');\n```\n\n### Shows a partial driver instance with custom parameters\n\n```php\nuse Pollen\\Partial\\PartialDriverInterface;\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello', function (PartialDriverInterface $driver) {\n    return 'Hello '. $driver-\u003eget('name') .' !';\n});\n\necho $partial-\u003eget('hello', ['name' =\u003e 'John Doe']);\n```\n\n### Recalls the same partial driver instance with keeped custom parameters\n\n```php\nuse Pollen\\Partial\\PartialDriverInterface;\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello', function (PartialDriverInterface $driver) {\n    return 'Hello '. $driver-\u003eget('name') .' !\u003cbr\u003e';\n});\n\necho $partial-\u003eget('hello', 'HelloJohn', ['name' =\u003e 'John Doe']);\necho $partial-\u003eget('hello', 'HelloJane', ['name' =\u003e 'Jane Doe']);\necho $partial-\u003eget('hello', 'HelloJohn');\n```\n\n## Partial driver API\n\n### Partial driver parameters of call\n\n```php\nuse Pollen\\Partial\\PartialDriverInterface;\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$tag = $partial-\u003eget('tag', [\n    /** \n     * Common driver parameters.\n     * -------------------------------------------------------------------------- \n     */\n    /**\n     * Main container HTML tag attributes.\n     * @var array $attrs\n     */\n    'attrs'   =\u003e [\n        'class' =\u003e '%s MyAppendedClass'\n    ],\n    /**\n     * Content displayed after the main container.\n     * @var string|callable $after\n     */\n    'after'   =\u003e 'content show after',\n    /**\n     * Content displayed before the main container.\n     * @var string|callable $before\n     */\n    'before'  =\u003e function (PartialDriverInterface $driver) {\n        return 'content show before'\n    },\n    /**\n     * List of parameters of the template view|View instance.\n     * {@internal See below in the View API usage section.}  \n     * @var array|ViewInterface $view\n     */\n    'view'  =\u003e [],\n    \n    /** \n     * Tag partial driver parameters\n     * -------------------------------------------------------------------------- \n     */\n    /**\n     * HTML tag.\n     * @var string $tag div|span|a|... default div.\n     */\n    'tag'       =\u003e 'div',\n    /**\n     * HTML tag content.\n     * @var string|callable $content\n     */\n    'content'   =\u003e '',\n    /**\n     * Enable tag as singleton.\n     * {@internal Auto-resolve if null based on list of known singleton tags.}\n     * @var bool|null $singleton\n     */\n    'singleton' =\u003e null,\n]);\n\necho $tag;\n```\n\n### Partial driver instance methods\n\n```php\nuse Pollen\\Field\\FieldManager;\n\n$field = new FieldManager();\n\n$field-\u003eregister('hello', function () {\n    return 'Hello World';\n});\n\nif ($hello = $partial-\u003eget('hello')) {\n    // Gets alias identifier.\n    printf('alias: %s \u003cbr/\u003e', $hello-\u003egetAlias());\n\n    // Gets the base prefix of HTML class.\n    printf('base HTML class: %s \u003cbr/\u003e', $hello-\u003egetBaseClass());\n\n    // Gets the unique identifier.\n    printf('identifier: %s \u003cbr/\u003e', $hello-\u003egetId());\n\n    // Gets the index in related partial manager.\n    printf('index: %s \u003cbr/\u003e', $hello-\u003egetIndex());\n}\n```\n\n### View API usage\n\n#### Plates view engine\n\nPartial driver used Plates as default template engine.\n\n1. Creates a view file for the partial driver.\n\n```php\n// /var/www/html/views/partial/hello.plates.php file\n/**\n * \\Pollen\\Partial\\PartialTemplateInterface $this\n */\necho 'Hello World !';\n```\n\n2. Creates and call a partial driver with this above file directory as view directory.\n\n```php\nuse Pollen\\Partial\\PartialDriver;\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello', new class extends PartialDriver{});\n\necho $partial-\u003eget('hello', ['view' =\u003e [\n    /**\n     * View directory absolute path (required).\n     * @var string\n     */\n    'directory' =\u003e '/var/www/html/views/partial/',\n    /**\n     * View override directory absolute path.\n     * @var string|null\n     */\n    'override_dir' =\u003e null,\n    /**\n     * View render main template name. index is used by default if its null.\n     * @var string|null\n     */\n    'template_name' =\u003e 'hello'\n]]);\n```\n\n#### Uses another view engine\n\nYour are free to used your own instance of Pollen\\View\\ViewInterface as the partial driver view parameter if needed. In\nthis example Twig engine is used instead Plates.\n\n1. Creates a view file for the partial driver.\n\n```html\n\u003c!-- /var/www/html/views/partial/hello/index.html.twig file --\u003e\nHello World !\n```\n\n2. Creates and call a partial driver with this above file directory as view directory.\n\n```php\nuse Pollen\\View\\ViewManager;\nuse Pollen\\Partial\\PartialDriver;\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello', new class extends PartialDriver{});\n\n$viewEngine = (new ViewManager())-\u003ecreateView('twig')-\u003esetDirectory('/var/www/html/views/partial/hello/');\n\necho $partial-\u003eget('hello', ['view' =\u003e $viewEngine]);\n```\n\n### Routing API usage\n\nIn some cases, partial driver should be able to send a response through a controller, for example to respond from a rest\napi call.\n\nFortunately, all partial driver instance are related to a route stack and have a reponseController method to do that.\nThe partial driver route stack is created for all known HTTP methods (GET, POST, PATH, OPTIONS, DELETE) and for a\nparticular api method that works with XHR HTTP request.\n\n1. Creates a partial driver and gets its route url for the get http method.\n\n```php\nuse Pollen\\Http\\Response;\nuse Pollen\\Http\\ResponseInterface;\nuse Pollen\\Partial\\PartialDriver;\nuse Pollen\\Partial\\PartialManager;\n\n$partial = new PartialManager();\n\n$partial-\u003eregister('hello', new class extends PartialDriver {\n    public function responseController(...$args) : ResponseInterface {\n        return new Response('Hello World !');\n    }\n});\n\n// Gets the route url for the get HTTP method\necho $partial-\u003egetRouteUrl('hello', null, [], 'get');\n\n```\n\n2. Now you can call the route url in your browser.\n\n[Get the partial response](/_partial/hello/responseController)\n\nObviously, you are free to use your own routing stack and them controller methods instead.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpollen-solutions%2Fpartial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpollen-solutions%2Fpartial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpollen-solutions%2Fpartial/lists"}