maplephp/swiftrender

PHP SwiftRender 是一个纯净且高度可移植的 PHP 模板库。

v1.2.2 2024-02-04 09:37 UTC

This package is auto-updated.

Last update: 2024-09-04 11:04:50 UTC


README

PHP SwiftRender 是一个纯 PHP 高级模板库。它为需要创建和渲染 PHP 应用程序模板的开发者提供了几个优势。首先,它可以提高性能,因为不需要额外的语言或引擎来处理模板。这可能导致渲染时间更快,开销更少。此外,纯净的 PHP 模板库非常可移植,因为它可以在几乎任何 PHP 应用程序中使用,无论其底层平台或框架。

PHP SwiftRender 提供了更大的灵活性和对渲染过程的控制。了解 PHP 的开发者可以以更适合他们特定需求的方式组织和设计模板,从而创建更高效、更有效的模板。

PHP 是一种广泛使用的语言,大多数开发者已经熟悉其语法和约定。这使得学习和使用纯净的 PHP 模板库比学习全新的模板语言或引擎要容易。

使用方法

初始化

通过应用程序使用的一次性设置。

use MaplePHP\Output\SwiftRender;
use MaplePHP\DTO\Format;

$swift = new SwiftRender();

$swift->setIndexDir(dirname(__FILE__)."/resources/") // Set index directory
->setViewDir(dirname(__FILE__)."/resources/views/")  // Set view directory
->setPartialDir(dirname(__FILE__)."/resources/partials/"); // Set partials directory

// Prepare/bind "/resources/index.php"
$swift->setIndex("index"); 

// Prepare/bind "/resources/views/main.php"
$swift->setView("main");

// Prepare/bind "/resources/partials/article.php"
$swift->setPartial("article", [
	"date" => "2023-02-30 15:33:22",
    "name" => "This is an article",
    "content" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
    "feed" => [
        [
            "headline" => "test 1", 
            "description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, architecto."
        ],
        [
            "headline" => "test 2", 
            "description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, architecto."
        ]
    ]
]);
// Keep in mind that the data usally comes from the database and that it should/might be called from you controller.
// E.g. $swift->setPartial("article", $mysqli->fetch_objects());

模板化

索引

在初始化部分上方已绑定 $swift->setIndex("index") 的文件 /resources/index.php。文件如下所示

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<!-- Used to create dynamic HTML tags, explained bellow under the section "Easy DOM manipulation" -->
	<?php echo \MaplePHP\Output\Dom\Document::dom("head")->execute(); ?>
</head>
<body>
	<?php echo $this->partial("navigation")->get(); ?>
	<main>
		<?php echo $this->view()->get(); ?>
	</main>
</body>
</html>

视图

在初始化部分上方已绑定 $swift->setView("main") 的文件 /resources/views/main.php。文件如下所示

<div id="wrapper">
	<?php echo $this->partial("article")->get(); ?>
</div>

部分

在初始化部分上方已绑定 $swift->setPartial("article", ...) 的文件 /resources/partials/article.php。文件如下所示

<article>
	<header>
		<h2><?php echo $obj->name; ?></h2>
		<h6><?php echo $obj->date("DateTime")->format("Y/m/d"); ?></h6>
		<p><?php echo $obj->content("Str")->excerpt(20)->get(); ?></p>
	</header>
	<?php if($obj->feed()->count() > 0): ?>
	<ul>
		<?php foreach($obj->feed()->fetch()->get() as $row): ?>
		<li>
			<strong><?php echo $row->headline("Str")->ucfirst()->get(); ?></strong><br>
			<?php echo $row->description; ?>
		</li>
		<?php endforeach; ?>
	</ul>
	<?php endif; ?>
</article>

部分功能

所有部分参数都将自动转换为具有大量扩展功能的对象。以下是一些例子

echo $obj->date; // 2023-02-30 15:33:22
echo $obj->date("DateTime")->format("Y/m/d"); // 2023/02/30
// Will strip all html tags, replace regular line breaks with "<br>" and uppercase the first letter
echo $obj->content("Str")->stripTags()->nl2br()->ucfirst()->get();

// Loop through an array
if($obj->feed()->count() > 0) foreach($obj->feed()->fetch()->get() as $row) {
	echo $row->headline("Str")->ucfirst()->get()."<br>";
}

运行模板引擎

您可以在稍后的空文件、发射器或路由分发器中运行模板引擎。这完全取决于您的设置。

echo $swift->index()->get();

动态视图

您还可以创建一个动态视图,如果调用,将覆盖当前视图。这对于例如显示 404 页面非常出色。

在此示例中,当前视图 /resources/views/main 将在响应状态码为(403、404 等)时替换为视图 /resources/views/httpStatus.php

// MaplePHP framework (PSR response). Just using this in this example to handle status response codes
use MaplePHP\Http\Response;

$swift->bindToBody(
    "httpStatus",
    Format\Arr::value(Response::PHRASE)->unset(200, 201, 202)->arrayKeys()->get()
    // This method will load all HTTP Request status codes (like 403, 404 e.g.) except for (200, 201, 202)
);

$swift->findBind($response->getStatusCode());

简单的 DOM 操作

高级 DOM 创建,并且与元数据等东西配合得很好,因为您可以在控制器中稍后更改值和属性。

// Advance DOM creation and works great with stuff like the Meta data 
$dom = MaplePHP\Output\Dom\Document::dom("head");
$dom->bindTag("title", "title")->setValue("Meta title");
$dom->bindTag("meta", "description")->attr("name", "Description")->attr("content", "Lorem ipsum dolor sit amet.");

// Then later in controller you can change the meta title and description
$head = MaplePHP\Output\Dom\Document::dom("head");
$head->getElement("title")->setValue("New meta title");
$head->getElement("description")->attr("content", "New meta description...");