Unable to inject less variables when parsing string from PHP in lessphp

595 views Asked by At

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.

2

There are 2 answers

3
newfurniturey On BEST ANSWER

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) );
2
Adi On

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)