northys/css-inliner

将CSS文件转换为HTML内联样式的PHP库。

v1.0-beta 2015-09-29 00:10 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:50:11 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

CSS-Inliner是一个简单的PHP工具,它可以将文件中的CSS插入到HTML的样式属性中。不多也不少。

我尽量让它尽可能快,但由于第三方库的限制,我无法做得更多。

无论如何,这个工具不是为了内联数百万行代码的CSS文件(嘿,Bootstrap - 当然你可以内联它,但你会足够的时间去泡一杯咖啡),而是为了将样式内联到电子邮件中。所以,我希望它能为你工作,帮助你创建新闻通讯、发送通知邮件等。

使用方法

使用composer安装

将northys/css-inliner添加到你的composer.json中。必须使用composer安装此工具。否则,你需要手动下载其他库。

$ composer require northys/css-inliner

要求

示例代码

$inliner = new Northys\CSSInliner;
$inliner->addCSS(__DIR__ . '/example.css');
$inliner->render(file_get_contents(__DIR__ . '/example.html'));

方法 addCSS() 接受CSS文件的路径,而 render() 接受HTML内容。如果你想了解原因,这里就是原因 - 有大量的模板系统,如 Nette\LatteSmarty,有时你需要在那些库生成的代码上使用这个工具。

输入

<h1>Hello, world!</h1>
<a href="http://google.com" class="google">Google</a>
<a href="http://Facebook.com" class="facebook">Facebook</a>
<a href="http://Outlook.com" id="outlook">Outlook</a>
h1{color:#27ae60;font-size:200px;margin:10px 50px 80px 30px;}
a{color:#2c3e50;}
a#outlook{color:#2980b9;position:absolute;top:30px;left:500px;padding:50px;}
a.facebook{color:#8e44ad;margin:300px;}
a.google{color:#c0392b;font-weight:700;font-family:Verdana, 'Open Sans';font-size:30px;}

输出

<h1 style="color: #27ae60; margin: 10px 50px 80px 30px; font-size: 200px;">Hello, world!</h1>
<a href="http://google.com" class="google" style='color: #c0392b; font-weight: 700; font-family: "Verdana","Open Sans"; font-size: 30px;'>Google</a>
<a href="http://Facebook.com" class="facebook" style="color: #8e44ad; margin: 300px;">Facebook</a>
<a href="http://Outlook.com" id="outlook" style="color: #2980b9; padding: 50; position: absolute; top: 30px; left: 500px;">Outlook</a>

或者用node运行它

  1. 安装依赖
	npm install
  1. 运行
	node index example myResult

在templates/example目录中会生成一个名为myResult.html的文件。

你的.html和.css文件必须与模板路径的名称相同。请参见https://github.com/alisonmonteiro/CSS-Inliner/tree/master/templates中的示例。