{"id":22477555,"url":"https://github.com/maplephp/swiftrender","last_synced_at":"2025-10-09T17:21:09.720Z","repository":{"id":209843896,"uuid":"620792304","full_name":"MaplePHP/SwiftRender","owner":"MaplePHP","description":"Advanced PHP template engine built for MaplePHP","archived":false,"fork":false,"pushed_at":"2025-03-25T19:08:16.000Z","size":102,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-27T20:14:27.144Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MaplePHP.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-03-29T11:37:16.000Z","updated_at":"2025-03-25T19:07:47.000Z","dependencies_parsed_at":"2024-12-06T14:11:39.287Z","dependency_job_id":"75a043ad-60a9-4981-9f45-c026c91a5f61","html_url":"https://github.com/MaplePHP/SwiftRender","commit_stats":null,"previous_names":["maplephp/swiftrender"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/MaplePHP/SwiftRender","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FSwiftRender","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FSwiftRender/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FSwiftRender/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FSwiftRender/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MaplePHP","download_url":"https://codeload.github.com/MaplePHP/SwiftRender/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaplePHP%2FSwiftRender/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279001879,"owners_count":26083197,"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","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-12-06T14:11:31.260Z","updated_at":"2025-10-09T17:21:09.677Z","avatar_url":"https://github.com/MaplePHP.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftRender\n\nPHP SwiftRender is a pure PHP advanced template library. It offers several advantages for developers who need to create and render templates in PHP applications. Firstly, it can improve performance, as there is no need for an additional language or engine to process the template. This can result in faster rendering times and reduced overhead. Additionally, a pure PHP template library is highly portable, as it can be used in almost any PHP application, regardless of the underlying platform or framework.\n\nPHP SwiftRender offers greater flexibility and control over the rendering process. Developers that knows PHP can structure and design their templates in ways that better suit their specific needs, resulting in more efficient and effective templates. \n\nPHP is a widely-used language, most developers are already familiar with its syntax and conventions. This makes it easier to learn and use a pure PHP template library than an entirely new templating language or engine.\n\n\n## Usage\n\n\n### Initialisation\nOne time setup to use through the application.\n\n```php\n\nuse MaplePHP\\Output\\SwiftRender;\nuse MaplePHP\\DTO\\Format;\n\n$swift = new SwiftRender();\n\n$swift-\u003esetIndexDir(dirname(__FILE__).\"/resources/\") // Set index directory\n-\u003esetViewDir(dirname(__FILE__).\"/resources/views/\")  // Set view directory\n-\u003esetPartialDir(dirname(__FILE__).\"/resources/partials/\"); // Set partials directory\n\n// Prepare/bind \"/resources/index.php\"\n$swift-\u003esetIndex(\"index\"); \n\n// Prepare/bind \"/resources/views/main.php\"\n$swift-\u003esetView(\"main\");\n\n// Prepare/bind \"/resources/partials/article.php\"\n$swift-\u003esetPartial(\"article\", [\n\t\"date\" =\u003e \"2023-02-30 15:33:22\",\n    \"name\" =\u003e \"This is an article\",\n    \"content\" =\u003e \"Lorem ipsum dolor sit amet, consectetur adipisicing elit.\",\n    \"feed\" =\u003e [\n        [\n            \"headline\" =\u003e \"test 1\", \n            \"description\" =\u003e \"Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, architecto.\"\n        ],\n        [\n            \"headline\" =\u003e \"test 2\", \n            \"description\" =\u003e \"Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, architecto.\"\n        ]\n    ]\n]);\n// Keep in mind that the data usually comes from the database and that it should/might be called from you controller.\n// E.g. $swift-\u003esetPartial(\"article\", $mysqli-\u003efetch_objects());\n\n```\n\n### Templating\n\n#### Index\n\nThe file **/resources/index.php** has already been bounded under the initialisation section above **$swift-\u003esetIndex(\"index\")**. The file looks like this:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n\t\u003cmeta charset=\"UTF-8\"\u003e\n\t\u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n\t\u003c!-- Used to create dynamic HTML tags, the below will create the metadata\n\texplained bellow under the section \"Easy DOM manipulation\" --\u003e\n\t\u003c?php echo \\MaplePHP\\Output\\Dom\\Document::dom(\"head\")-\u003eexecute(); ?\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\t\u003c?php echo $this-\u003epartial(\"navigation\")-\u003eget(); ?\u003e\n\t\u003cmain\u003e\n\t\t\u003c?php echo $this-\u003eview()-\u003eget(); ?\u003e\n\t\u003c/main\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n#### View\nThe file **/resources/views/main.php** has already been bounded under the initialisation section above **$swift-\u003esetView(\"main\")**. The file looks like this:\n\n```html\n\u003cdiv id=\"wrapper\"\u003e\n\t\u003c?php echo $this-\u003epartial(\"article\")-\u003eget(); ?\u003e\n\u003c/div\u003e\n```\n\n#### Partial\nThe file **/resources/partials/article.php** has already been bounded under the initialisation section above **$swift-\u003esetPartial(\"article\", ...)**. The file looks like this:\n\n```html\n\u003carticle\u003e\n\t\u003cheader\u003e\n\t\t\u003ch2\u003e\u003c?php echo $name; ?\u003e\u003c/h2\u003e\n\t\t\u003ch6\u003e\u003c?php echo $date-\u003eclockFormat(\"Y/m/d\"); ?\u003e\u003c/h6\u003e\n\t\t\u003cp\u003e\u003c?php echo $content-\u003estExcerpt(20); ?\u003e\u003c/p\u003e\n\t\u003c/header\u003e\n\t\u003c?php if($feed-\u003ecount() \u003e 0): ?\u003e\n\t\u003cul\u003e\n\t\t\u003c?php foreach($feed-\u003efetch() as $row): ?\u003e\n\t\t\u003cli\u003e\n\t\t\t\u003cstrong\u003e\u003c?php echo $row-\u003eheadline-\u003estrUcFirst(); ?\u003e\u003c/strong\u003e\u003cbr\u003e\n\t\t\t\u003c?php echo $row-\u003edescription; ?\u003e\n\t\t\u003c/li\u003e\n\t\t\u003c?php endforeach; ?\u003e\n\t\u003c/ul\u003e\n\t\u003c?php endif; ?\u003e\n\u003c/article\u003e\n```\n\n#### Partial functionality\nThe partials all arguments will automatically be converted to an object with a lot of extended functionality. Here is some:\n\n```php\necho $date; // 2023-02-30 15:33:22\necho $date-\u003eclockFormat(\"Y/m/d\"); // 2023/02/30\n// Will strip all html tags, replace regular line breaks with \"\u003cbr\u003e\" and uppercase the first letter\necho $content-\u003estrStripTags()-\u003estrNl2br()-\u003estrUcfirst();\n\n// Loop through an array\nif($feed-\u003ecount() \u003e 0) foreach($feed-\u003efetch() as $row) {\n\techo $row-\u003eheadline-\u003estrUcFirst() . \"\u003cbr\u003e\";\n}\n```\n\n#### Run the template engine\nYou can run the template engine later in an empty file, emitter or router dispatcher. It all depends on your setup.\n```php\necho $swift-\u003eindex()-\u003eget();\n\n```\n\n#### Dynamic views\nYou can also create a dynamic view that will overwrite the current view if called. This is great for e.g. showing a 404 page. \n\nIn this example the current view which is **/resources/views/main** will be replaced with the view **/resources/views/httpStatus.php** when response status code is (403, 404 e.g.).\n\n```php\n// MaplePHP framework (PSR response). Just using this in this example to handle status response codes\nuse MaplePHP\\Http\\Response;\n\n$swift-\u003ebindToBody(\n    \"httpStatus\",\n    Format\\Arr::value(Response::PHRASE)-\u003eunset(200, 201, 202)-\u003earrayKeys()-\u003eget()\n    // This method will load all HTTP Request status codes (like 403, 404 e.g.) except for (200, 201, 202)\n);\n\n$swift-\u003efindBind($response-\u003egetStatusCode());\n```\n\n#### Easy DOM manipulation\nAdvance DOM creation and works great with stuff like the Metadata because you can later in change values and attributes in the controller. \n\n```php\n// Advance DOM creation and works great with stuff like the Metadata \n$dom = MaplePHP\\Output\\Dom\\Document::dom(\"head\");\n$dom-\u003ebindTag(\"title\", \"title\")-\u003esetValue(\"Meta title\");\n$dom-\u003ebindTag(\"meta\", \"description\")-\u003eattr(\"name\", \"Description\")-\u003eattr(\"content\", \"Lorem ipsum dolor sit amet.\");\n\n// Then later in controller you can change the meta title and description\n$head = MaplePHP\\Output\\Dom\\Document::dom(\"head\");\n$head-\u003egetElement(\"title\")-\u003esetValue(\"New meta title\");\n$head-\u003egetElement(\"description\")-\u003eattr(\"content\", \"New meta description...\");\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaplephp%2Fswiftrender","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaplephp%2Fswiftrender","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaplephp%2Fswiftrender/lists"}