0

Here is an example of how to compile a file:

$lines = [];
exec("lessc $file", $lines, $status);
$output = implode("", $lines);

I would like to compile just a string ideally without creating a temporary file, writing to it, and then calling exec to compile that file. My unix skills aren't that great so I tried variations of this which doesn't work:

$lines = [];
exec("lessc < \"$string\"", $lines, $status);
$output = implode("", $lines);
fedorqui
  • 275,237
  • 103
  • 548
  • 598
rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • 1
    You should probably check this out: http://leafo.net/lessphp/ – pNre Aug 10 '14 at 10:45
  • 2
    You need to substitute the filename parameter then certainly, if not with `-` then `/dev/stdin`. Also either escape the $string properly, or use proc_open. – mario Aug 10 '14 at 10:46
  • @pNre I'd rather trust the official binary keeping up with the current version of LESS. LessPHP doesn't even indicate which version of LESS it supports, with their last commit 4 months ago, and 166 open issues. Good suggestion though, it would have been perfect if it was actively maintained. – rtheunissen Aug 10 '14 at 11:20

1 Answers1

2

If you run lessc --help in the console, you will that:

usage: lessc [option option=parameter ...] [destination]

If source is set to `-' (dash or hyphen-minus), input is read from stdin.

The above enable you to run for instance (see also: Send string to stdin):

echo "@color:red; p{color:@color;}" | lessc -

or

lessc - <<< "@color:red; p{color:@color;}"

And indeed less.php can:

$parser = new Less_Parser();
$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
$css = $parser->getCss();
Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224