@@ -317,19 +317,10 @@ def compile!(view)
317
317
end
318
318
end
319
319
320
- # Among other things, this method is responsible for properly setting
321
- # the encoding of the compiled template.
322
- #
323
- # If the template engine handles encodings, we send the encoded
324
- # String to the engine without further processing. This allows
325
- # the template engine to support additional mechanisms for
326
- # specifying the encoding. For instance, ERB supports <%# encoding: %>
327
- #
328
- # Otherwise, after we figure out the correct encoding, we then
329
- # encode the source into <tt>Encoding.default_internal</tt>.
330
- # In general, this means that templates will be UTF-8 inside of Rails,
331
- # regardless of the original source encoding.
332
- def compile ( mod )
320
+ # This method compiles the source of the template. The compilation of templates
321
+ # involves setting strict_locals! if applicable, encoding the template, and setting
322
+ # frozen string literal.
323
+ def compiled_source
333
324
strict_locals!
334
325
source = encode!
335
326
code = @handler . call ( self , source )
@@ -343,7 +334,7 @@ def compile(mod)
343
334
344
335
# Make sure that the resulting String to be eval'd is in the
345
336
# encoding of the code
346
- original_source = source
337
+ @ original_source = source
347
338
source = +<<-end_src
348
339
def #{ method_name } (#{ method_arguments } )
349
340
@virtual_path = #{ @virtual_path . inspect } ;#{ locals_code } ;#{ code }
@@ -364,17 +355,33 @@ def #{method_name}(#{method_arguments})
364
355
raise WrongEncodingError . new ( source , Encoding . default_internal )
365
356
end
366
357
358
+ if Template . frozen_string_literal
359
+ "# frozen_string_literal: true\n #{ source } "
360
+ else
361
+ source
362
+ end
363
+ end
364
+
365
+ # Among other things, this method is responsible for properly setting
366
+ # the encoding of the compiled template.
367
+ #
368
+ # If the template engine handles encodings, we send the encoded
369
+ # String to the engine without further processing. This allows
370
+ # the template engine to support additional mechanisms for
371
+ # specifying the encoding. For instance, ERB supports <%# encoding: %>
372
+ #
373
+ # Otherwise, after we figure out the correct encoding, we then
374
+ # encode the source into <tt>Encoding.default_internal</tt>.
375
+ # In general, this means that templates will be UTF-8 inside of Rails,
376
+ # regardless of the original source encoding.
377
+ def compile ( mod )
367
378
begin
368
- if Template . frozen_string_literal
369
- mod . module_eval ( "# frozen_string_literal: true\n #{ source } " , identifier , -1 )
370
- else
371
- mod . module_eval ( source , identifier , 0 )
372
- end
379
+ mod . module_eval ( compiled_source , identifier , offset )
373
380
rescue SyntaxError
374
381
# Account for when code in the template is not syntactically valid; e.g. if we're using
375
382
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate
376
383
# the result into the template, but missing an end parenthesis.
377
- raise SyntaxErrorInTemplate . new ( self , original_source )
384
+ raise SyntaxErrorInTemplate . new ( self , @ original_source)
378
385
end
379
386
380
387
return unless @strict_locals
@@ -398,6 +405,14 @@ def #{method_name}(#{method_arguments})
398
405
)
399
406
end
400
407
408
+ def offset
409
+ if Template . frozen_string_literal
410
+ -1
411
+ else
412
+ 0
413
+ end
414
+ end
415
+
401
416
def handle_render_error ( view , e )
402
417
if e . is_a? ( Template ::Error )
403
418
e . sub_template_of ( self )
0 commit comments