instance method
compile
Ruby on Rails 7.1.6
Since v3.0.20 PrivateSignature
compile(mod)
Among other things, this method is responsible for properly setting the encoding of the compiled template.
If the template engine handles encodings, we send the encoded String to the engine without further processing. This allows the template engine to support additional mechanisms for specifying the encoding. For instance, ERB supports <%# encoding: %>
Otherwise, after we figure out the correct encoding, we then encode the source into Encoding.default_internal. In general, this means that templates will be UTF-8 inside of Rails, regardless of the original source encoding.
Parameters
-
modreq
Source
# File actionview/lib/action_view/template.rb, line 477
def compile(mod)
begin
mod.module_eval(compiled_source, identifier, offset)
rescue SyntaxError
# Account for when code in the template is not syntactically valid; e.g. if we're using
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate
# the result into the template, but missing an end parenthesis.
raise SyntaxErrorInTemplate.new(self, encode!)
end
return unless strict_locals?
parameters = mod.instance_method(method_name).parameters - [[:req, :output_buffer]]
# Check compiled method parameters to ensure that only kwargs
# were provided as strict locals, preventing `locals: (foo, *foo)` etc
# and allowing `locals: (foo:)`.
non_kwarg_parameters = parameters.select do |parameter|
![:keyreq, :key, :keyrest, :nokey].include?(parameter[0])
end
non_kwarg_parameters.pop if non_kwarg_parameters.last == %i(block _)
unless non_kwarg_parameters.empty?
mod.undef_method(method_name)
raise ArgumentError.new(
"#{non_kwarg_parameters.map { |_, name| "`#{name}`" }.to_sentence} set as non-keyword " \
"#{'argument'.pluralize(non_kwarg_parameters.length)} for #{short_identifier}. " \
"Locals can only be set as keyword arguments."
)
end
unless parameters.any? { |type, _| type == :keyrest }
parameters.map!(&:last)
parameters.sort!
@strict_local_keys = parameters.freeze
end
end
Defined in actionview/lib/action_view/template.rb line 477
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Template