Drupal 7 Theming: Generate styles to a file instead of inline In my field.tpl.php, I have used PHP to write a string of CSS. The content of the string varies according to the field options. 

I then add this string of css to my theme using drupal_add_css(). The style is then added to my theme, but it is added inline.

Is there a way to make the string of CSS to be written to an external stylesheet instead?
If you change the content of the CSS file every time the template file is invoked, then you could use code similar to the following one. The code is longer than it should be because I am using a custom function whenever the equivalent Drupal function would output a message error.

function mytheme_data_save($data, $destination, $replace = FILE_EXISTS_RENAME) {
  if (!file_valid_uri($destination)) {
    watchdog('file', 'The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', array('%destination' =&gt; $destination));
    return FALSE;
  }

  if ($uri = mytheme_unmanaged_data_save($data, $destination, $replace)) {
    // Create a file object.
    $file = new stdClass();
    $file-&gt;fid = NULL;
    $file-&gt;uri = $uri;
    $file-&gt;filename = drupal_basename($uri);
    $file-&gt;filemime = file_get_mimetype($file-&gt;uri);
    $file-&gt;uid      = $user-&gt;uid;
    $file-&gt;status   = FILE_STATUS_PERMANENT;
    // If we are replacing an existing file re-use its database record.
    if ($replace == FILE_EXISTS_REPLACE) {
      $existing_files = file_load_multiple(array(), array('uri' =&gt; $uri));
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $file-&gt;fid = $existing-&gt;fid;
        $file-&gt;filename = $existing-&gt;filename;
      }
    }
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
    elseif ($replace == FILE_EXISTS_RENAME &amp;&amp; is_file($destination)) {
      $file-&gt;filename = drupal_basename($destination);
    }

    return file_save($file);
  }

  return FALSE;
}

function mytheme_unmanaged_data_save($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  // Write the data to a temporary file.
  $temp_name = drupal_tempnam('temporary://', 'file');
  if (file_put_contents($temp_name, $data) === FALSE) {
    return FALSE;
  }

  // Move the file to its final destination.
  return mytheme_unmanaged_file_move($temp_name, $destination, $replace);
}

function mytheme_unmanaged_file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  $filepath = mytheme_unmanaged_file_copy($source, $destination, $replace);
  if ($filepath == FALSE || file_unmanaged_delete($source) == FALSE) {
    return FALSE;
  }
  return $filepath;
}

function mytheme_unmanaged_file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  $original_source = $source;
  $original_destination = $destination;

  // Assert that the source file actually exists.
  if (!file_exists($source)) {
    if (($realpath = drupal_realpath($original_source)) !== FALSE) {
      watchdog('file', 'File %file (%realpath) could not be copied because it does not exist.', array('%file' =&gt; $original_source, '%realpath' =&gt; $realpath));
    }
    else {
      watchdog('file', 'File %file could not be copied because it does not exist.', array('%file' =&gt; $original_source));
    }
    return FALSE;
  }

  // Build a destination URI if necessary.
  if (!isset($destination)) {
    $destination = file_build_uri(drupal_basename($source));
  }

  // Prepare the destination directory.
  if (file_prepare_directory($destination)) {
    // The destination is already a directory, so append the source basename.
    $destination = file_stream_wrapper_uri_normalize($destination . '/' . drupal_basename($source));
  }
  else {
    // Perhaps $destination is a dir/file?
    $dirname = drupal_dirname($destination);
    if (!file_prepare_directory($dirname)) {
      // The destination is not valid.
      watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' =&gt; $original_source, '%destination' =&gt; $dirname));
      return FALSE;
    }
  }

  // Determine whether we can perform this operation based on overwrite rules.
  $destination = file_destination($destination, $replace);
  if ($destination === FALSE) {
    watchdog('file', 'File %file could not be copied because a file by that name already exists in the destination directory (%directory)', array('%file' =&gt; $original_source, '%destination' =&gt; $destination));
    return FALSE;
  }

  // Assert that the source and destination filenames are not the same.
  $real_source = drupal_realpath($source);
  $real_destination = drupal_realpath($destination);
  if ($source == $destination || ($real_source !== FALSE) &amp;&amp; ($real_source == $real_destination)) {
    watchdog('file', 'File %file could not be copied because it would overwrite itself.', array('%file' =&gt; $source));
    return FALSE;
  }
  // Make sure the .htaccess files are present.
  file_ensure_htaccess();
  // Perform the copy operation.
  if (!@copy($source, $destination)) {
    // If the copy failed and realpaths exist, retry the operation using them
    // instead.
    if ($real_source === FALSE || $real_destination === FALSE || !@copy($real_source, $real_destination)) {
      watchdog('file', 'The specified file %file could not be copied to %destination.', array('%file' =&gt; $source, '%destination' =&gt; $destination), WATCHDOG_ERROR);
      return FALSE;
    }
  }

  // Set the permissions on the new file.
  drupal_chmod($destination);

  return $destination;
}


This is how you should use the code.

if (($file = mytheme_data_save($data, 'public://mytheme_css') !== FALSE) {
  if ($instance = file_stream_wrapper_get_instance_by_uri($file-&gt;uri)) {
    $url = $instance-&gt;getExternalUrl();
    drupal_add_css($url, array('type' =&gt; 'external'));
  }
}


With the code I am using, the file used to contain the CSS styles is deleted the first time the cron tasks are executed, if the file has been created 6 hours earlier.