class Haml::Options

This class encapsulates all of the configuration options that Haml understands. Please see the {file:REFERENCE.md#options Haml Reference} to learn how to set the options.

Attributes

attr_wrapper[R]

The character that should wrap element attributes. This defaults to `'` (an apostrophe). Characters of this type within the attributes will be escaped (e.g. by replacing them with `'`) if the character is an apostrophe or a quotation mark.

autoclose[RW]

A list of tag names that should be automatically self-closed if they have no content. This can also contain regular expressions that match tag names (or any object which responds to `#===`). Defaults to `['meta', 'img', 'link', 'br', 'hr', 'input', 'area', 'param', 'col', 'base']`.

cdata[RW]

Whether to include CDATA sections around javascript and css blocks when using the `:javascript` or `:css` filters.

This option also affects the `:sass`, `:scss`, `:less` and `:coffeescript` filters.

Defaults to `false` for html, `true` for xhtml. Cannot be changed when using xhtml.

compiler_class[RW]

The compiler class to use. Defaults to Haml::Compiler.

encoding[R]

The encoding to use for the HTML output. Only available on Ruby 1.9 or higher. This can be a string or an `Encoding` Object. Note that Haml **does not** automatically re-encode Ruby values; any strings coming from outside the application should be converted before being passed into the Haml template. Defaults to `Encoding.default_internal`; if that's not set, defaults to the encoding of the Haml template; if that's `US-ASCII`, defaults to `“UTF-8”`.

escape_attrs[RW]

Sets whether or not to escape HTML-sensitive characters in attributes. If this is true, all HTML-sensitive characters in attributes are escaped. If it's set to false, no HTML-sensitive characters in attributes are escaped. If it's set to `:once`, existing HTML escape sequences are preserved, but other HTML-sensitive characters are escaped.

Defaults to `true`.

escape_html[RW]

Sets whether or not to escape HTML-sensitive characters in script. If this is true, `=` behaves like {file:REFERENCE.md#escaping_html `&=`}; otherwise, it behaves like {file:REFERENCE.md#unescaping_html `!=`}. Note that if this is set, `!=` should be used for yielding to subtemplates and rendering partials. See also {file:REFERENCE.md#escaping_html Escaping HTML} and {file:REFERENCE.md#unescaping_html Unescaping HTML}.

Defaults to false.

filename[RW]

The name of the Haml file being parsed. This is only used as information when exceptions are raised. This is automatically assigned when working through ActionView, so it's really only useful for the user to assign when dealing with Haml programatically.

format[R]

Determines the output format. The default is `:html5`. The other options are `:html4` and `:xhtml`. If the output is set to XHTML, then Haml automatically generates self-closing tags and wraps the output of the Javascript and CSS-like filters inside CDATA. When the output is set to `:html5` or `:html4`, XML prologs are ignored. In all cases, an appropriate doctype is generated from `!!!`.

If the #mime_type of the template being rendered is `text/xml` then a format of `:xhtml` will be used even if the global output format is set to `:html4` or `:html5`.

hyphenate_data_attrs[RW]

If set to `true`, Haml will convert underscores to hyphens in all {file:REFERENCE.md#html5_custom_data_attributes Custom Data Attributes} As of Haml 4.0, this defaults to `true`.

line[RW]

The line offset of the Haml template being parsed. This is useful for inline templates, similar to the last argument to `Kernel#eval`.

mime_type[RW]

The mime type that the rendered document will be served with. If this is set to `text/xml` then the format will be overridden to `:xhtml` even if it has set to `:html4` or `:html5`.

parser_class[RW]

The parser class to use. Defaults to Haml::Parser.

preserve[RW]

A list of tag names that should automatically have their newlines preserved using the {Haml::Helpers#preserve} helper. This means that any content given on the same line as the tag will be preserved. For example, `%textarea= “FoonBar”` compiles to `<textarea>Foo&#x000A;Bar</textarea>`. Defaults to `['textarea', 'pre']`. See also {file:REFERENCE.md#whitespace_preservation Whitespace Preservation}.

remove_whitespace[R]

If set to `true`, all tags are treated as if both {file:REFERENCE.md#whitespace_removal__and_ whitespace removal} options were present. Use with caution as this may cause whitespace-related formatting errors.

Defaults to `false`.

suppress_eval[RW]

Whether or not attribute hashes and Ruby scripts designated by `=` or `~` should be evaluated. If this is `true`, said scripts are rendered as empty strings.

Defaults to `false`.

ugly[RW]

If set to `true`, Haml makes no attempt to properly indent or format the HTML output. This significantly improves rendering performance but makes viewing the source unpleasant.

Defaults to `true` in Rails production mode, and `false` everywhere else.

Public Class Methods

buffer_option_keys() click to toggle source

An array of keys that will be used to provide a hash of options to {Haml::Buffer}. @return Hash

# File lib/haml/options.rb, line 49
def self.buffer_option_keys
  @buffer_option_keys
end
defaults() click to toggle source

The default option values. @return Hash

# File lib/haml/options.rb, line 36
def self.defaults
  @defaults
end
new(values = {}) { || ... } click to toggle source
# File lib/haml/options.rb, line 172
def initialize(values = {}, &block)
  defaults.each {|k, v| instance_variable_set :"@#{k}", v}
  values.reject {|k, v| !defaults.has_key?(k) || v.nil?}.each {|k, v| send("#{k}=", v)}
  yield if block_given?
end
valid_formats() click to toggle source

An array of valid values for the `:format` option. @return Array

# File lib/haml/options.rb, line 42
def self.valid_formats
  @valid_formats
end

Public Instance Methods

[](key) click to toggle source

Retrieve an option value. @param key The value to retrieve.

# File lib/haml/options.rb, line 180
def [](key)
  send key
end
[]=(key, value) click to toggle source

Set an option value. @param key The key to set. @param value The value to set for the key.

# File lib/haml/options.rb, line 187
def []=(key, value)
  send "#{key}=", value
end
attr_wrapper=(value) click to toggle source
# File lib/haml/options.rb, line 220
def attr_wrapper=(value)
  @attr_wrapper = value || self.class.defaults[:attr_wrapper]
end
encoding=(value) click to toggle source
# File lib/haml/options.rb, line 251
def encoding=(value)
  return unless value
  @encoding = value.is_a?(Encoding) ? value.name : value.to_s
  @encoding = "UTF-8" if @encoding.upcase == "US-ASCII"
end
for_buffer() click to toggle source

Returns a subset of options: those that {Haml::Buffer} cares about. All of the values here are such that when `#inspect` is called on the hash, it can be `Kernel#eval`ed to get the same result back.

See {file:REFERENCE.md#options the Haml options documentation}.

@return [{Symbol => Object}] The options hash

# File lib/haml/options.rb, line 265
def for_buffer
  self.class.buffer_option_keys.inject({}) do |hash, key|
    hash[key] = send(key)
    hash
  end
end
format=(value) click to toggle source
# File lib/haml/options.rb, line 231
def format=(value)
  unless self.class.valid_formats.include?(value)
    raise Haml::Error, "Invalid output format #{value.inspect}"
  end
  @format = value
end
html4?() click to toggle source

@return [Boolean] Whether or not the format is HTML4.

# File lib/haml/options.rb, line 211
def html4?
  format == :html4
end
html5?() click to toggle source

@return [Boolean] Whether or not the format is HTML5.

# File lib/haml/options.rb, line 216
def html5?
  format == :html5
end
html?() click to toggle source

@return [Boolean] Whether or not the format is any flavor of HTML.

# File lib/haml/options.rb, line 206
def html?
  html4? or html5?
end
remove_whitespace=(value) click to toggle source
# File lib/haml/options.rb, line 243
def remove_whitespace=(value)
  @ugly = true if value
  @remove_whitespace = value
end
xhtml?() click to toggle source

@return [Boolean] Whether or not the format is XHTML.

# File lib/haml/options.rb, line 201
def xhtml?
  not html?
end

Private Instance Methods

defaults() click to toggle source
# File lib/haml/options.rb, line 274
def defaults
  self.class.defaults
end