chulakov/yii2-typograph

为 Yii2 的排版组件

1.0.5 2024-07-04 10:35 UTC

This package is auto-updated.

Last update: 2024-09-04 11:01:12 UTC


README

为 Yii2 开发的排版组件

该包允许将排版功能嵌入到 Yii2 框架中。该包由行为和排版组件组成。

安装

要安装此包,需要在 composer.json 文件中添加以下行:

"require": {
    "chulakov/yii2-typograph": "^1.0.0",
}

或者输入以下命令

composer require chulakov/yii2-typograph

组件连接方式

在 common/config/main 文件中添加组件。组件必须实现 chulakov/ch-php-typograph 包中的 TypografInterface 接口。

return [
	'components' => [
		...
		'typograph' => [
	    	'class' => 'Chulakov\Typograph\TypographComponent',
	    ],
	]
];

在组件中指定默认的排版类 TypographFacade

可以在组件内部覆盖排版规则。为此,在组件属性 additionalRulesPath 和 changedRulesPath 中放置包含新排版规则和/或修改旧规则文件的路劲。以下是一些示例文件路径。

注意1:新规则是需要添加到旧规则中的规则
注意2:配置规则或修改规则的示例(第3-4点)

return [
	'components' => [
		...
		'typograph' => [
			'class' => 'Chulakov\Typograph\TypographComponent',
			'additionalRulesPath' => '@common/components/typograph/config/additionalRules.php',
			'changedRulesPath' => '@common/components/typograph/config/changedRules.php',
		],
	]
];

在 TypographComponent 组件中实现了 process 函数,用于排版文本

$typographComponent->process('до н. э.');

行为连接方式

可以将行为附加到表单上,并指定需要排版的字段

public function behaviors()
{
	return [
		[
		    'class' => TypographBehaviour::class,
		    'attributes' => ['title']
		],
	];
}

如果字段是复合的,例如字段是 MultipleInput 对象,则需要以数组的形式指定

public function behaviors()
{
	return [
		[
			'class' => TypographerBehaviour::class,
			'attributes' => [
			    'items' => [
			        'properties' => ['title', 'description']
			    ]
			]
		],
	];
}

行为构造函数需要传入 TypografInterface 接口的实现
在 Yii2 中,可以通过依赖注入传递实现 TypografInterface 接口的排版组件
可以通过在 BootstrapInterface 接口的实现中编写代码来实现这一点

\Yii::$container->setSingleton('Chulakov\PhpTypograph\TypografInterface', function() use ($app) {
    return $app->get('typograph');
});