instance method compiled_source

Ruby on Rails 7.2.3

Since v7.1.6 Private

Available in: v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

compiled_source()

This method compiles the source of the template. The compilation of templates involves setting strict_locals! if applicable, encoding the template, and setting frozen string literal.

Source
# File actionview/lib/action_view/template.rb, line 444
      def compiled_source
        set_strict_locals = strict_locals!
        source = encode!
        code = @handler.call(self, source)

        method_arguments =
          if set_strict_locals
            if set_strict_locals.include?("&")
              "local_assigns, output_buffer, #{set_strict_locals}"
            else
              "local_assigns, output_buffer, #{set_strict_locals}, &_"
            end
          else
            "local_assigns, output_buffer, &_"
          end

        # Make sure that the resulting String to be eval'd is in the
        # encoding of the code
        source = +<<-end_src
          def #{method_name}(#{method_arguments})
            @virtual_path = #{@virtual_path.inspect};#{locals_code};#{code}
          end
        end_src

        # Make sure the source is in the encoding of the returned code
        source.force_encoding(code.encoding)

        # In case we get back a String from a handler that is not in
        # BINARY or the default_internal, encode it to the default_internal
        source.encode!

        # Now, validate that the source we got back from the template
        # handler is valid in the default_internal. This is for handlers
        # that handle encoding but screw up
        unless source.valid_encoding?
          raise WrongEncodingError.new(source, Encoding.default_internal)
        end

        if Template.frozen_string_literal
          "# frozen_string_literal: true\n#{source}"
        else
          source
        end
      end

Defined in actionview/lib/action_view/template.rb line 444 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Template

Type at least 2 characters to search.

↑↓ navigate · open · esc close