2

I've got the following less code. I've read around that there is a limitation in less.js when using variables in url() declaration and so i used two suggested workarounds, but the real problem is that the variable comes from PHP and it seem it's not passed correctly, as it's never interpreted.

I'm using lessphp

Maybe I'm doing something wrong.

PHP code

$this->parsed_css = $this->lessc->parse( $this->parsed_css, array( 'fontdir', 'myfontdir' ) );

Less code

@font-face {
    font-family: 'FontAwesome';
    src: url(%("%s/fontawesome-webfont.eot", @fontdir));
    src: url('@{fontdir}/fontawesome-webfont.eot?#iefix') format('embedded-opentype'),
         url('@{fontdir}/fontawesome-webfont.woff') format('woff'),
         url('@{fontdir}/fontawesome-webfont.ttf') format('truetype'),
         url('@{fontdir}/fontawesome-webfont.svgz#FontAwesomeRegular') format('svg'),
         url('@{fontdir}/fontawesome-webfont.svg#FontAwesomeRegular') format('svg');
    font-weight: @fontdir;
    font-style: normal;
}

CSS output

@font-face {
  font-family:'FontAwesome';
  src:url("/fontawesome-webfont.eot");
  src:url('/fontawesome-webfont.eot?#iefix') format('embedded-opentype'), url('/fontawesome-webfont.woff') format('woff'), url('/fontawesome-webfont.ttf') format('truetype'), url('/fontawesome-webfont.svgz#FontAwesomeRegular') format('svg'), url('/fontawesome-webfont.svg#FontAwesomeRegular') format('svg');
  font-weight:;
  font-style:normal;
}

As you can see the variable is never interpreted.

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192

2 Answers2

1

Per your original question (before you removed it in an edit), the variable's name is $font_dir, but you're using fontdir (no underscore) in all references.

Try assigning the fontdir element a value with array('fontdir' => $font_dir), such as:

$this->parsed_css = $this->lessc->parse( $this->parsed_css, array( 'fontdir' => $font_dir, 'myfontdir' => $my_font_dir) );
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • No i'm not even passing the variable, `$this->parsed_css = $this->lessc->parse( $this->parsed_css, array( 'fontdir', 'myfontdir' ) );`, i will later on pass a variable, as of now i hardcoded it – Nicola Peluchetti Aug 01 '12 at 13:42
  • Yep yopu are right and i'm an idiot, `array( 'fontdir', 'myfontdir' )` should be `array( 'fontdir' => 'myfontdir' )`. Thanks! :) – Nicola Peluchetti Aug 01 '12 at 13:55
  • No need for name calling =P; it's a common mistake (and I've made it plenty of times) - some systems do key/value pairing with index 0 vs 1 (like you originally had) and some do them with PHP's 'key'=>'value' method. – newfurniturey Aug 01 '12 at 13:58
0

I've just downloaded less and tested it, the array thing doesn't seem to be the only reason your code isn't working.

This isn't valid less code (At least according to the documentation)

src: url('@{fontdir}/fontawesome-webfont.eot?#iefix') format('embedded-opentype')
//         ^       ^    Remove these

//Right
echo $less->parse("color: @color",
    array(
        'color' => 'red'
)); //color:red;

//Wrong
echo $less->parse("color: @{color}",
        array(
            'color' => 'red'
    )); // (no output)
Adi
  • 5,089
  • 6
  • 33
  • 47
  • No the sintax is correct ( and i think it's required since i'm concatenating into url(), outside it shouldn't be used ), i had to do some extra work to have everything working, like escaping the variable for less since it had a : inside, but the problem here was with the array – Nicola Peluchetti Aug 01 '12 at 14:37
  • @NicolaPeluchetti, very strange! Because I even tested the exact same `less` code you posted, didn't work. Well, happy you solved it! – Adi Aug 01 '12 at 14:53