I'm using Less in a project now and came across a situation where I needed to provide the path to a processed less file. It would be helpful for the module to provide something like this. Below is the helper function from my project. My guess is that you'd simply want to return the URI instead.

/**
 * Helper function that returns the path of a processed Less file.
 *
 * @param $type The type of the item (i.e. theme, theme_engine, module, profile).
 * @param $name The name of the item for which the path is requested.
 * @param $path The path of the less file within your module or theme.
 *
 * @return Relative path to the resulting CSS file generated by Less.
 */
function less_get_path($type, $name, $path) {
  // Work out the URI for the processed file.
  $uri = file_default_scheme() . '://less/' . drupal_get_path($type, $name) . '/' . $path;
  // file_create_url() turns a URI into a full URL. We want a relative URL so
  // filter out the base URL.
  $relative_path = str_replace($GLOBALS['base_url'], '', file_create_url($uri));
  return $relative_path;
}

Comments

rickvug’s picture

I forgot that .less needs to be stripped off of the path provided:

  if (drupal_substr($path, -5) == '.less') {
    $path = drupal_substr($path, 0, -5);
  }
corey.aufang’s picture

You can do something like this to have less return the paths to compiled files. It will also compile the file if it doesn't exist yet.

    $styles = array(
      '#items' => array(),
    );
    
    foreach ($stylesheets as $css_file) {
      $styles['#items'][$css_file] = array(
        'data' => trim($css_file, '/'),
      );
    }
    
    $styles = _less_pre_render($styles);

corey.aufang’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)