odepax/pasap

此包已被 弃用 且不再维护。未建议替代包。

PHP 作为预处理器 --- 另一个服务器端自定义标签库。

2.0.0-beta 2016-09-03 16:43 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:59:36 UTC


README

This composer package is no longer maintained

Pasap

Pasap代表 Php AS A Preprocessor。这是一个提供服务器端自定义标签的库。

自定义标签用于提高代码可读性并使HTML更具意义。

记住,这个 README 仅作为备忘录。有关库功能的详细信息,请查阅wiki

安装

Pasap 作为 Composer 包 提供。您可以通过CLI安装它

composer require odepax/pasap

... 或者将其粘贴到您的项目 composer.json 文件中

"require": {
    "odepax/pasap": "^2.0"
}

快速入门

步骤 1. 创建一个经典的PHP文件。此文件应生成HTML,唯一不同的是生成的HTML代码中有自定义标签。

<document title="I'm Using Custom Tags!">
	<news title="Lorem Ipsum" author="Me, of course">
		<p>Lorem ipsum <em>dolor</em> sit amet...</p>
	</news>
</document>

注意:<html> -- <head> -- <body> 结构不是必须的:您可以使用自定义标签并用作文档的根元素,而不是 <html>

步骤 2. 为您使用的每个自定义标签创建一个定义文件。这里,您将创建一个 custom-tags/document.php 文件

<html lang="en">
	<head>
		<meta charset="UTF-8"/>
		<title><?= $this->attr("title") ?></title>
	</head>
	<body>
		<?= $this->children() ?>
	</body>
</html>

... 以及一个 custom-tags/news.php 文件

<article class="news">
	<h1 class="news_title"><?= $this->attr('title') ?></h1>
	<em class="news_author"><?= $this->attr('author') ?></em>
	<div class="news_content">
		<?= $this->children() ?>
	</div>
</article>

步骤 3. 现在让我们配置和运行Pasap

<?php

require './vendor/autoload.php';

// Get generated HTML with custom tags as a string.
ob_start();
include 'our-super-pasap-document.php';
$htmlWithCustomTags = ob_get_clean();

// Convert custom tags into valid HTML using the definition files.
echo \Pasap\Pasap::parse($htmlWithCustomTags, 'custom-tags');

步骤 4. 享受输出

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8"/>
		<title>I'm Using Custom Tags!</title>
	</head>
	<body>
		<article class="news">
			<h1 class="news_title">Lorem Ipsum</h1>
			<em class="news_author">Me, of course</em>
			<div class="news_content">
				<p>Lorem ipsum <em>dolor</em> sit amet...</p>
			</div>
		</article>
	</body>
</html>

用法

在定义文件中,$this 代表自定义元素。以下是您可以调用的方法

$this->tag(); // string

$this->is('tag'); // boolean (compares with $this->tag())
$this->is('#text'); // boolean
$this->is('#comment'); // boolean
$this->is('#cdata'); // boolean
$this->is('#element'); // boolean

$this->parent(); // Element

$this->children(); // ElementCollection
$this->children('link'); // Shorcut for $this->children()->only('link')

$this->attr(); // AttrCollection
$this->attr('name'); // string | null
$this->attr('name', 'fallback'); // string

$this->data('key'); // mixed | null
$this->data('key', 'fallback'); // mixed

$this->scope('key'); // mixed | null
$this->scope('key', 'fallback'); // mixed

更多示例请参考 测试文件夹

Pasap 支持一些配置选项。它们由 Configure 静态类处理

Configure::namespaceSource('', './element');
Configure::namespaceSource('intuitive-form', './vendor/odepax/pasap-intuitive-form/element');

Configure::nativeNamespace('html', [ 'br', 'hr', 'img', 'meta' ]);
Configure::nativeNamespace('svg', [ 'path' ]);

Configure::output(Configure::LEFT_AS_THIS | Configure::PRETTIFY | Configure::MINIFY);

Configure::doctype(Configure::LEFT_AS_THIS | Configure::ALWAYS_HTML5);

限制

限制 1. 您不能在定义文件中使用自定义标签。然而,您仍然可以使用自定义标签作为另一个自定义标签的子标签。

限制 2. 这是XML,而不是HTML。这意味着即使这是有效的 HTML5 代码

<head>
    <meta charset="UTF-8">
    <title>...</title>
</head>

... 这不是有效的 XML 代码,因为XML对自闭合标签更为严格:它们必须以 /> 结尾,因此您需要将 <meta><input> 转换为 <meta/><input/>

即使您不再受 <html> -- <head> -- <body> 结构的限制,请记住您只能有一个 根元素

限制 3. 由于自定义标签是在服务器端管理的,因此您的CSS和JavaScript不会意识到它们。我正在努力解决这个问题。