{"id":13622329,"url":"https://github.com/slimphp/PHP-View","last_synced_at":"2025-04-15T05:34:38.271Z","repository":{"id":41983724,"uuid":"43382409","full_name":"slimphp/PHP-View","owner":"slimphp","description":"A Simple PHP Renderer for Slim 3 \u0026 4 (or any other PSR-7 project)","archived":false,"fork":false,"pushed_at":"2024-08-29T16:26:30.000Z","size":634,"stargazers_count":263,"open_issues_count":0,"forks_count":60,"subscribers_count":15,"default_branch":"3.x","last_synced_at":"2024-09-08T02:09:23.331Z","etag":null,"topics":[],"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/slimphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-29T17:18:41.000Z","updated_at":"2024-09-02T08:06:57.000Z","dependencies_parsed_at":"2024-02-01T18:14:27.422Z","dependency_job_id":"83c520a2-6984-4e40-90b9-decd86c23a35","html_url":"https://github.com/slimphp/PHP-View","commit_stats":{"total_commits":116,"total_committers":23,"mean_commits":5.043478260869565,"dds":0.6810344827586207,"last_synced_commit":"ed5e37e8041959ec4e46b4bf6df5b94fed49684a"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FPHP-View","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FPHP-View/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FPHP-View/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slimphp%2FPHP-View/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slimphp","download_url":"https://codeload.github.com/slimphp/PHP-View/tar.gz/refs/heads/3.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223661553,"owners_count":17181689,"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":[],"created_at":"2024-08-01T21:01:17.711Z","updated_at":"2024-11-08T09:31:29.122Z","avatar_url":"https://github.com/slimphp.png","language":"PHP","readme":"\n[![Latest Version on Packagist](https://img.shields.io/github/release/slimphp/php-view.svg)](https://packagist.org/packages/slim/PHP-View)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)\n[![Build Status](https://github.com/slimphp/PHP-View/actions/workflows/tests.yml/badge.svg?branch=3.x)](https://github.com/slimphp/PHP-View/actions)\n[![Total Downloads](https://img.shields.io/packagist/dt/slim/PHP-View.svg)](https://packagist.org/packages/slim/PHP-View/stats)\n\n\n## PHP Renderer\n\nThis is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 4.\n\n\n### Cross-site scripting (XSS) risks\n\nNote that PHP-View has no built-in mitigation from XSS attacks. \nIt is the developer's responsibility to use `htmlspecialchars()` \nor a component like [laminas-escaper](https://github.com/laminas/laminas-escaper). Alternatively, consider  [Twig-View](https://github.com/slimphp/Twig-View).\n\n## Installation\n\n```\ncomposer require slim/php-view\n```\n\n## Usage with any PSR-7 Project\n\n```php\n//Construct the View\n$renderer = new PhpRenderer('path/to/templates');\n\n$viewData = [\n    'key1' =\u003e 'value1',\n    'key2' =\u003e 'value2',\n];\n\n// Render a template\n$response = $renderer-\u003erender(new Response(), 'hello.php', $viewData);\n```\n\n## Usage with Slim 4\n\n```php\nuse Slim\\AppFactory;\nuse Slim\\Views\\PhpRenderer;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n$app = AppFactory::create();\n\n$app-\u003eget('/hello', function ($request, $response) {\n    $renderer = new PhpRenderer('path/to/templates');\n    \n    $viewData = [\n        'name' =\u003e 'John',\n    ];\n    \n    return $renderer-\u003erender($response, 'hello.php', $viewData);\n});\n\n$app-\u003erun();\n```\n\n## DI Container Setup\n\nYou can place the `PhpRenderer` instantiation within your DI Container.\n\n```php\n\u003c?php\n\nuse Psr\\Container\\ContainerInterface;\nuse Slim\\Views\\PhpRenderer;\n// ...\n\nreturn [\n    PhpRenderer::class =\u003e function (ContainerInterface $container) {\n        $renderer = new PhpRenderer('path/to/templates');\n\n        return $renderer;\n    },\n];\n\n```\n\n## Template Variables\n\nYou can now add variables to your renderer that will be available to all templates you render.\n\n```php\n// Via the constructor\n$globalViewData = [\n    'title' =\u003e 'Title'\n];\n\n$renderer = new PhpRenderer('path/to/templates', $globalViewData);\n\n// or setter\n$viewData = [\n    'key1' =\u003e 'value1',\n    'key2' =\u003e 'value2',\n];\n$renderer-\u003esetAttributes($viewData);\n\n// or individually\n$renderer-\u003eaddAttribute($key, $value);\n```\n\nData passed in via the `render()` method takes precedence over attributes.\n\n```php\n$viewData = [\n    'title' =\u003e 'Title'\n];\n$renderer = new PhpRenderer('path/to/templates', $viewData);\n\n//...\n\n$response = $renderer-\u003erender($response, $template, [\n    'title' =\u003e 'My Title'\n]);\n\n// In the view above, the $title will be \"My Title\" and not \"Title\"\n```\n\n## Sub-templates\n\nInside your templates you may use `$this` to refer to the PhpRenderer object to render sub-templates. \nIf using a layout the `fetch()` method can be used instead of `render()` to avoid applying the layout to the sub-template.\n\n```php\n\u003c?=$this-\u003efetch('./path/to/partial.phtml', ['name' =\u003e 'John'])?\u003e\n```\n\n## Rendering in Layouts\n\nYou can now render view in another views called layouts, \nthis allows you to compose modular view templates\nand help keep your views DRY.\n\nCreate your layout `path/to/templates/layout.php`\n\n```php\n\u003chtml\u003e\u003chead\u003e\u003ctitle\u003e\u003c?=$title?\u003e\u003c/title\u003e\u003c/head\u003e\u003cbody\u003e\u003c?=$content?\u003e\u003c/body\u003e\u003c/html\u003e\n```\n\nCreate your view template `path/to/templates/hello.php`\n\n```php\nHello \u003c?=$name?\u003e!\n```\n\nRendering in your code.\n\n```php\n$renderer = new PhpRenderer('path/to/templates', ['title' =\u003e 'My App']);\n$renderer-\u003esetLayout('layout.php');\n\n$viewData = [\n    'title' =\u003e 'Hello - My App',\n    'name' =\u003e 'John',\n];\n\n//...\n\n$response = $renderer-\u003erender($response, 'hello.php', $viewData);\n```\n\nResponse will be\n\n```html\n\u003chtml\u003e\u003chead\u003e\u003ctitle\u003eHello - My App\u003c/title\u003e\u003c/head\u003e\u003cbody\u003eHello John!\u003c/body\u003e\u003c/html\u003e\n```\n\nPlease note, the `$content` is special variable used inside layouts \nto render the wrapped view and should not be set in your view parameters.\n\n## Escaping values\n\nIt's essential to ensure that the HTML output is secure to \nprevent common web vulnerabilities like Cross-Site Scripting (XSS).\nThis package has no built-in mitigation from XSS attacks.\n\nThe following function uses the `htmlspecialchars` function \nwith specific flags to ensure proper encoding:\n\n```php\nfunction html(?string $text = null): string\n{\n    return htmlspecialchars($text ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');\n}\n```\n\nYou could consider setting it up as a global function in [composer.json](https://getcomposer.org/doc/04-schema.md#files).\n\n**Usage**\n\n```php\nHello \u003c?= html($name) ?\u003e\n```\n\n## Exceptions\n\n* `\\Slim\\Views\\Exception\\PhpTemplateNotFoundException` - If template layout does not exist\n* `\\Slim\\Views\\Exception\\PhpTemplateNotFoundException` - If template does not exist\n* `\\RuntimeException` - If the template output could not be fetched\n* `\\InvalidArgumentException` - If $data contains 'template'\n","funding_links":[],"categories":["Table of Contents","PHP","目录","Packages and Middleware","Templating"],"sub_categories":["Micro Framework Extras","微观框架-扩展 micro-framework-extras","Videos"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslimphp%2FPHP-View","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslimphp%2FPHP-View","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslimphp%2FPHP-View/lists"}