久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 PHP 自動將引用的 LESS 文件編譯為 CSS

Compile a referenced LESS file into CSS with PHP automatically(使用 PHP 自動將引用的 LESS 文件編譯為 CSS)
本文介紹了使用 PHP 自動將引用的 LESS 文件編譯為 CSS的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望發生以下事情:

  1. 使流程自動化服務器端.

  1. Have the process automated server side.

只需能夠像在代碼中引用 CSS 文件一樣引用 LESS 文件.

Simply be able to reference the LESS file as I would a CSS file in my code.

返回給用戶的是縮小的 CSS 而不是 LESS 文件 - 已緩存,因此編譯器不需要運行,除非 LESS 文件已更新.

The user is returned minified CSS instead of the LESS file - cached so the compiler doesn't need to run unless the LESS file has been updated.

為了使其與任何在我的域內任何位置引用的 LESS 文件一起使用.

For this to work with any LESS file that is referenced anywhere within my domain.

我發現了 Lessphp,但文檔不是很清楚,也沒有解釋如何動態獲取任何 LESS 文件.我想我會發布我是如何讓它全部工作的,因為我還沒有看到如何使用 PHP 實現這一目標.

I spotted Lessphp, but the documentation isn't very clear, nor does it explain how to dynamically get any LESS file to it. I thought I would post up how I got it all working as I haven't seen a run through on how to achieve this with PHP.

推薦答案

THIS ASSUMES LESSPHP v0.3.8+ 不確定早期版本,但如果沒有,您將了解其工作原理不是直接開箱即用的.

THIS ASSUMES LESSPHP v0.3.8+ Unsure about earlier versions, but you'll get the gist of how it works if it doesn't straight out of the box.

<link rel="stylesheet" type="text/css" href="styles/main.less"/>

如果您使用的是less.js 來編譯客戶端,請確保將rel="stylesheet/less" 更改為rel="stylesheet"

If you were using less.js to compile client side, make sure you change rel="stylesheet/less" to rel="stylesheet"

1) 抓取 Lessphp 我把這些文件放在 /www/compilers/lessphp/代碼>用于此演示的上下文

1) Grab Lessphp I placed these files in /www/compilers/lessphp/ for the context of this demo

2) 制作一個 PHP 腳本,我們可以在其中拋出 LESS 文件.這將處理緩存、編譯為 CSS 并將 CSS 作為響應返回.我已將此文件放在 /www/compilers/ 并命名為 lessphp.php

2) Make a PHP script that we can throw out LESS files at. This will deal with caching, compiling to CSS and returning the CSS as a response. I have placed this file at /www/compilers/ and called it lessphp.php

此代碼的大部分內容都在 Lessphp 站點上,只是其中存在錯誤,我在最后添加了響應.

Most of this code was on the Lessphp site, except there were errors in it, and I have added the response at the end.

<?php
require "lessphp/lessc.inc.php";
$file = $_GET["file"];
function autoCompileLess($inputFile, $outputFile) {
    // load the cache
    $cacheFile = $inputFile.".cache";
    if (file_exists($cacheFile)) {
        $cache = unserialize(file_get_contents($cacheFile));
    } else {
        $cache = $inputFile;
    }
    $less = new lessc;
    $less->setFormatter("compressed");
    $newCache = $less->cachedCompile($cache);
    if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
        file_put_contents($cacheFile, serialize($newCache));
        file_put_contents($outputFile, $newCache['compiled']);
    }
}
autoCompileLess('../' . $file, '../' . $file . '.css');
header('Content-type: text/css');
readfile('../' . $file . '.css');
?>

這會將 LESS 文件(例如 styles/main.less)編譯為緩存文件和 CSS 文件(例如 styles/main.less.css).

This will compile the LESS file (eg, styles/main.less) to a cache file and a CSS file (eg, styles/main.less.css).

3) 添加 mod_rewrite 規則,以便用戶請求的任何 LESS 文件都重定向到我們的編譯器,并為其提供路徑.這被放置在根 .htaccess 文件中.

3) Add a mod_rewrite rule so that any LESS files a user requests are redirected to our compiler, giving it its path. This was placed in the root .htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^.]*.less)$ compilers/lessphp.php?file=$1 [R,QSA,L]
</ifModule>

如果您使用的是 WordPress,則此規則需要跟在它之后 - 即使 WordPress 位于子目錄中,它似乎也會覆蓋這些規則,并且對于存在于下面的引用文件(目錄明智)不會發生 LESS 編譯WordPress 的 .htaccess 規則.

If you are using WordPress, this rule will need to come after it - even if WordPress is in a sub directory, it seems to overwrite these rules, and LESS compilation will not occur for referenced files which exist below (directory wise) WordPress's .htaccess rules.

4) 您的 LESS 代碼應該相對于編譯器位置進行引用.此外,如果存在空屬性,Lessphp 編譯器將失敗,例如.背景色:;

4) Your LESS code should be relatively referenced in relation to the compilers location. Additionally, Lessphp compiler will fail if there are empty attributes, eg. background-color: ;

如果一切正常,應該會發生以下情況:

If all is working well, the following should occur:

  1. 直接瀏覽你的 LESS 文件 http://domain.com/styles/main.less

被自動重定向到http://domain.com/compilers/lessphp?file=styles/main.less

以縮小的 CSS 呈現

Be presented with minified CSS

main.less.cssmain.less.cache 現在應該與 LESS 文件位于同一目錄中

main.less.css and main.less.cache should now exist in the same directory as your LESS file

上次修改日期不應更改,除非您更新 LESS 文件

The last modified dates shouldn’t change unless you update your LESS file

這篇關于使用 PHP 自動將引用的 LESS 文件編譯為 CSS的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 凹凸日日摸日日碰夜夜 | 日本黄色免费视频 | 日本免费在线看 | 欧美性网 | 超碰97人人人人人蜜桃 | 国产成人久久av免费高清密臂 | 涩爱av一区二区三区 | 免费一级毛片 | 久久亚洲国产 | 欧美综合久久 | 日韩欧美国产精品一区 | 免费在线观看av网站 | 久久久久一区 | 天堂成人国产精品一区 | 欧美日韩在线一区二区 | 岛国午夜 | 中文字字幕一区二区三区四区五区 | 亚州精品天堂中文字幕 | 男女免费观看在线爽爽爽视频 | 久久国产精品久久久久久久久久 | 日韩av一区二区在线观看 | www.色婷婷 | 99热这里都是精品 | 97超碰成人| 亚洲天堂999| 精品福利在线 | 午夜影视在线观看 | 亚洲国产精品久久久 | 国产黄色麻豆视频 | 有码在线 | 亚洲91精品 | 亚洲天堂日韩精品 | 日韩视频一区在线观看 | 久久日韩精品 | 中文字幕在线二区 | www久| 国产人久久人人人人爽 | 欧美1页| 亚洲精品在线免费看 | 久久av一区二区三区 | 羞羞视频在线观看网站 |