Search code examples
phplesslessphp

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


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.


Solution

  • 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) );