2

I am using a very simple code to test phpsass

$sass = new SassParser();
$c = file_get_contents('/_www/_projects/cdn/public_html/test/test.sass');
$css = $sass->toCss($c);

The sass file is very basic:

$blue: #3bbfce;
$margin: 16px;

table.hl {
  margin: 2em 0;
  td.ln {
    text-align: right;
  }
}

li {
  font: {
    family: serif;
    weight: bold;
    size: 1.2em;
  }
}

.content-navigation {
  border-color: $blue;
  color:
    darken($blue, 9%);
}

.border {
  padding: $margin / 2;
  margin: $margin / 2;
  border-color: $blue;
}

And I get the following error:

Fatal error: Uncaught exception 'SassNumberException' with message ' in D:\_www\_git\phpsass\script\literals\SassNumber.php on line 201

SassNumberException: Number must be a number: Array::26 Source: padding: $margin / 2; in D:\_www\_git\phpsass\script\literals\SassNumber.php on line 201

If it can help, there's also the following notice thrown before (I tried to change the error_reporting level but having the notice disappear of course didn't solve the problem):

Notice: Array to string conversion in D:\_www\_git\phpsass\SassException.php on line 25

If I use the same sass code against their online PHP compiler at http://www.phpsass.com/try/ it compiles correctly... I don't get it

Community
  • 1
  • 1
Nabab
  • 2,608
  • 1
  • 19
  • 32

3 Answers3

3

Take a look at the SassParser class. The function toCss() has a second parameter called $isFile which by default is true:

public function toCss($source, $isFile = true) {
    return $this->parse($source, $isFile)->render();
}

If you notice, you're passing in the source not the filename. You can either pass in the filename like so:

$sass = new SassParser();
$c = '/_www/_projects/cdn/public_html/test/test.sass';
$css = $sass->toCss($c);

Or do what you're trying to do but with an additional parameter on toCss():

$sass = new SassParser();
$c = file_get_contents('/_www/_projects/cdn/public_html/test/test.sass');
$css = $sass->toCss($c, false);
SeanWM
  • 16,789
  • 7
  • 51
  • 83
  • Yes this parameter exists, I hadn't seen it, but it still falls back on the string method when the file isn't found, so adding a false parameter didn't change the problem. However I found something which did (see update) – Nabab Mar 08 '13 at 04:45
2

Your sass file is not really sass at all; this would be the correct syntax, basically take out all the trailing semi-colons:

$blue: #3bbfce
$margin: 16px

table.hl {
  margin: 2em 0
  td.ln {
    text-align: right
  }
}

li {
  font: {
    family: serif
    weight: bold
    size: 1.2em
  }
}

.content-navigation {
  border-color: $blue
  color
    darken($blue, 9%)
}

.border {
  padding: $margin / 2
  margin: $margin / 2
  border-color: $blue
}

Or, like you suggested:

$sass = new SassParser(['syntax' => SassFile::SCSS]);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Yes, I know, I've been confused by the website http://sass-lang.com/ (their frontpage is just scss and they're Google number 1 on sass, I just didn't look enough ;) – Nabab Mar 14 '13 at 16:24
1

In fact that was a pure syntaxic problem. I tried to remove the variables from the sass code, and realized it worked - not well (margin: 2em 0 became margin: 20) but there was no error anymore.

The sass snippets that I took from http://sass-lang.com/ were not sass but scss format. The SassParser class has a parameter called syntax, which is set to 'sass' by default and needs to be set to scss when calling $scss = new SassParser(['syntax' => 'scss']).

However I needed to parse a string, not a file, but if you pass a file with a scss extension you don't need this extra parameter.

Nabab
  • 2,608
  • 1
  • 19
  • 32