jstewmc/get-style
Composer 安装命令:
composer require jstewmc/get-style
包简介
Get a css inline style
README 文档
README
Get a Cascading Style Sheet (CSS) inline style from a PHP array.
use Jstewmc\GetStyle; // define your styles $styles = [ // the asterisk is a special name for global style declarations '*' => [ 'color' => 'red', 'font-size' => 'small' ], 'foo' => [ 'color' => 'blue', 'font-size' => 'medium' ], 'bar' => [ 'color' => 'green' ] ]; // instantiate the get-style service $css = new Get($styles); // get the styles $css(); // returns "color: red; font-size: small;" $css('foo'); // returns "color: blue; font-size: medium;" $css('bar'); // returns "color: green; font-size: small;" $css('foo bar'); // returns "color: green; font-size: medium;"
This library is a (very) simple PHP Cascading Style Sheet (CSS) reader. It does not claim to be a full CSS parser! There are several of those already. This library is just a quick and easy way to produce inline CSS from an array of styles.
Names
This library has no concept of CSS selectors! Style names are treated as array keys. They can be any valid string. For example, p, a.foo, .foo, and foo are all valid style names.
Keep in mind, the asterisk (*) is a special style name that's treated as the global default style. If an asterisk style is defined, all styles will start with its declarations.
Declarations
This library merges style declarations like CSS. Given a space-separated string of style names (e.g., "foo bar baz"), it will merge the declarations with later declarations taking precedence over previous ones (i.e., "foo < bar < baz").
Keep in mind, this libary is not smart! It requires (very) simple style declarations.
Use valid declarations
This library makes no effort to validate your declarations. Make sure your CSS declarations are valid. Otherwise, you will have layout issues.
Avoid short-hand declarations
Short-hand CSS declarations like "margin" => "0 0 10px" cannot be merged intelligently with other declarations.
For example:
use Jstewmc\GetStyle; // this will not work! $styles1 = [ '*' => [ 'margin' => '0 0 10px' ], 'foo' => [ 'margin-top' => '15px' ] ]; // this will work! $styles2 = [ '*' => [ 'margin-top' => '0', 'margin-left' => '0', 'margin-right' => '0', 'margin-bottom' => '10px' ], 'foo' => [ 'margin-top' => '15px' ] ]; // create two css services $css1 = new Get($styles1); $css2 = new Get($styles2); $css1('foo'); // returns "margin: 0 0 10px; margin-top: 15px;" $css2('foo'); // returns "margin-top: 15px; margin-left: 0; ... "
Watch quotes
This library will not quote space-separated styles. Make sure you quote any space-separated styles in your declarations like "Courier New".
use Jstewmc\GetStyle; // this will not work! $styles1 = [ '*' => [ 'font-family' => 'Courier New' ] ]; // this will work! $styles2 = [ '*' => [ 'font-family' => '\'Courier New\'' ] ]; // create two css services $css1 = new Get($styles1); $css2 = new Get($styles2); $css1(); // returns "font-family: Courier New;" $css2(); // returns "font-family: 'Courier New';"
Keep in mind, if you are outputting your css as an inline style, make sure your CSS quotes don't collide with the quotes in your HTML source code.
For example:
use Jstewmc\GetStyle; $styles = [ '*' => [ 'font-family' => '"Courier New"' ] ]; $css = new Get($styles); echo '<p style="'. $css() .'">Hello world!</p>'
The example above will produce the following broken HTML:
<p style="font-family: "Courier New"">Hello world!</p>
Avoid space-separated declarations
Even if you quote space-separated declarations correctly, you can still run into issues.
An email's HTML body is aggressively line-wrapped at 80 characters or less by some email clients. Newlines in the source code are replaced by spaces, and newlines characters are inserted where needed.
If a newline is inserted at the space in a declaration name, the declaration will break, and the corresponding element will be un-styled. At a minimum, this behavior has been confirmed on OSX 10 running Apple Mail 8.
Author
License
Version
1.0.0, August 16, 2016
- Major release
- Update
composer.json - Update comments
0.1.0, August 7, 2016
- Initial release
jstewmc/get-style 适用场景与选型建议
jstewmc/get-style 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「css」 「inline」 「style」 「jstewmc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jstewmc/get-style 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jstewmc/get-style 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jstewmc/get-style 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Symfony bundle for using the CssToInlineStyles translator by tijsverkoyen
A Twig extension to insert css as inline styles with a tag
A Laravel Nova field for displaying morphTo relationship inline.
Caching and compression for Twig assets (JavaScript and CSS).
Utility functions to inline SVGs in PHP Projects
Inline test plugin for the Testo testing framework.
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-07