使用 php 减少/sass
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4986334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
less/sass with php
提问by Adi
I am a front end developer and have recently considered using SASS or LESS for CSS development.
我是一名前端开发人员,最近考虑使用 SASS 或 LESS 进行 CSS 开发。
However I do not use Ruby and I don't want to rely on users having JavaScript active. Does anyone have any tips for using SASS or LESS using PHP projects?
但是,我不使用 Ruby,也不想依赖使用 JavaScript 的用户。有没有人有使用 SASS 或 LESS 使用 PHP 项目的任何提示?
采纳答案by Quentin
Install Ruby. Install SASS. Use SASS.
安装红宝石。安装SASS。使用SASS。
It outputs static files, so you just upload them like any other CSS as part of your build/publish process.
它输出静态文件,因此您只需像任何其他 CSS 一样上传它们,作为构建/发布过程的一部分。
回答by Lulu the hero
Download the latest version of lessphphere.
在这里下载最新版本的lessphp。
Here is an example on how I tried it:
这是我如何尝试的示例:
<?php
require 'lessc.inc.php';
$less = new lessc('test.less');
file_put_contents('test.less.css', $less->parse());
?>
<html>
<head>
<title>Less CSS</title>
<link rel="stylesheet" href="test.less.css" type="text/css" />
</head>
<body>
<h1>This isn't supposed to be black!</h1>
</body>
</html>
And my test.less file:
还有我的 test.less 文件:
@color : #33ddff;
.colorful(@textcolor : red){
color : @textcolor;
}
h1{
.colorful(@color);
}
It works for me, and it's php!
它对我有用,而且是 php!
回答by leafo
You now have two options written by me:
你现在有我写的两个选项:
- LESS: http://leafo.net/lessphp/
- SCSS: http://leafo.net/scssphp/(Only has SCSS syntax, not the indentation based SASS syntax)
- 少:http: //leafo.net/lessphp/
- SCSS:http: //leafo.net/scssphp/(只有 SCSS 语法,没有基于缩进的 SASS 语法)
Both are well documented. I encourage you to try them out and tell me if you have any trouble.
两者都有很好的记录。我鼓励你尝试一下,如果你有任何问题,请告诉我。
回答by Manuel Pedrera
Take a look at this tutorial: http://net.tutsplus.com/tutorials/php/how-to-squeeze-the-most-out-of-less/
看看这个教程:http: //net.tutsplus.com/tutorials/php/how-to-squeeze-the-most-out-of-less/
(4th entry when googling 'less php')
(谷歌搜索“less php”时的第 4 个条目)
回答by V-Light
LESS PHP takes a lot of resources...okay lot is relative, but anyway, you should chache the output css. If you're on Mac, use LESSapp. If you're on Windows, use dotLEss(actually it's a library, which could be integrated in several .NET project, but it has a small command-line compiler, wich outputs a valid CSS file)
LESS PHP 占用大量资源......好吧,很多是相对的,但无论如何,你应该查查输出的 css。如果您使用的是 Mac,请使用LESSapp。如果您使用的是 Windows,请使用dotLEss(实际上它是一个库,可以集成到多个 .NET 项目中,但它有一个小的命令行编译器,可输出有效的 CSS 文件)
回答by Jonathan Czitkovics
As part of my job I do use sass with php.
作为我工作的一部分,我确实将 sass 与 php 一起使用。
You may try PhamlPas this is what I use. PhamlP
is a port of Haml
and Sass
to PHP
.
您可以尝试PhamlP,因为这是我使用的。PhamlP
是的端口Haml
和Sass
到PHP
。
You may have the sass parser run every page load or you can have it cache the css
it generates.
您可以让 sass 解析器在每个页面加载时运行,或者您可以让它缓存css
它生成的内容。
Here is the same question question being asked.if you want to see more options
这是被问到的同样的问题。如果您想查看更多选项
回答by Andy
You can use PhpLessDemandBridge for that: https://github.com/andyhausmann/PhpLessDemandBridge
您可以使用 PhpLessDemandBridge:https: //github.com/andyhausmann/PhpLessDemandBridge
You can simply use it in your Templates, like:
您可以简单地在模板中使用它,例如:
<link rel="stylesheet" type="text/css" media="all" href="css/engine/css.php?file=bootstrap.less" />
The Engine can be configured via config file, where you define your path the the less files and cache files.
引擎可以通过配置文件进行配置,您可以在其中定义路径,文件和缓存文件越少。
By defining "css.php?file=bootstrap.less", you point the Engine to the bootstrap file, which import other less files e.g. Twitter Bootstrap or such.
通过定义“css.php?file=bootstrap.less”,您将引擎指向引导文件,该文件导入其他较少的文件,例如 Twitter Bootstrap 等。
I am using this for Magento, TYPO3 CMS and many more.
我将它用于 Magento、TYPO3 CMS 等等。
In the first line, i am including needed parts of Twitter Bootstrap - after that i am including my own theme and overrides.
在第一行中,我包含了 Twitter Bootstrap 所需的部分 - 之后我包含了我自己的主题和覆盖。
回答by shea
If you're working with WordPress, you should definitely check out WP LESS. I All you need to do is specify a .less
file with wp_register_style
or wp_enqueue_style
or even add_editor_style
, and it compiles your LESS code into CSS code for you. It also caches the resulting CSS so you don't have the overhead of the LESS compiling on every page load.
如果您使用 WordPress,则绝对应该查看WP LESS。I 所有你需要做的就是.less
用wp_register_style
或wp_enqueue_style
甚至指定一个文件add_editor_style
,它会为你将你的 LESS 代码编译成 CSS 代码。它还缓存生成的 CSS,因此您不会在每个页面加载时产生 LESS 编译的开销。
回答by ethanpil
I have http://leafo.net/scssphp/on a few projects. It works well for me, here is what I do.
我有几个项目的http://leafo.net/scssphp/。它对我很有效,这就是我所做的。
Install scssphp into lib/scssphp/
将 scssphp 安装到 lib/scssphp/
In .htaccess
在.htaccess中
#Sass Parser: anything /css/FILENAME.css -> FILENAME.scss
RewriteRule ^css/(.*).css?$ style.php/.scss [NC,L]
In root folder, I have style.php:
在根文件夹中,我有 style.php:
<?php
require "lib/scssphp/scss.inc.php";
$scss = new scssc();
$scss->setFormatter("scss_formatter");
$server = new scss_server("ui", null, $scss);
$server->serve();
?>
In my HTML, I use:
在我的 HTML 中,我使用:
<link rel="stylesheet" href="/css/style.css">
In /ui I have my file of actual SCSS code: /ui/style.scss
在 /ui 我有我的实际 SCSS 代码文件:/ui/style.scss
And everything just works. SCSSPHP just checks if the file needs to be recompiled and does it transparently, otherwise sends the cached version.
一切正常。SCSSPHP 只检查文件是否需要重新编译并透明地执行,否则发送缓存版本。