|
189 | 189 | # |
190 | 190 | # When you call method #result, |
191 | 191 | # the method executes the code and removes the entire execution tag |
192 | | -# (generating no text in the result). |
| 192 | +# (generating no text in the result): |
| 193 | +# |
| 194 | +# ``` |
| 195 | +# ERB.new('foo <% Dir.chdir("C:/") %> bar').result # => "foo bar" |
| 196 | +# ``` |
| 197 | +# |
| 198 | +# Whitespace before and after the embedded code is optional: |
| 199 | +# |
| 200 | +# ``` |
| 201 | +# ERB.new('foo <%Dir.chdir("C:/")%> bar').result # => "foo bar" |
| 202 | +# ``` |
193 | 203 | # |
194 | 204 | # You can interleave text with execution tags to form a control structure |
195 | 205 | # such as a conditional, a loop, or a `case` statements. |
|
261 | 271 | # |
262 | 272 | # #### Shorthand Format for Execution Tags |
263 | 273 | # |
264 | | -# You can give `trim_mode: '%'` to enable a shorthand format for execution tags; |
| 274 | +# You can use keyword argument `trim_mode: '%'` to enable a shorthand format for execution tags; |
265 | 275 | # this example uses the shorthand format `% _code_` instead of `<% _code_ %>`: |
266 | 276 | # |
267 | 277 | # ``` |
|
283 | 293 | # Note that in the shorthand format, the character `'%'` must be the first character in the code line |
284 | 294 | # (no leading whitespace). |
285 | 295 | # |
| 296 | +# #### Suppressing Unwanted Blank Lines |
| 297 | +# |
| 298 | +# With keyword argument `trim_mode` not given, |
| 299 | +# all blank lines go into the result: |
| 300 | +# |
| 301 | +# ``` |
| 302 | +# s = <<EOT |
| 303 | +# <% if true %> |
| 304 | +# <%= RUBY_VERSION %> |
| 305 | +# <% end %> |
| 306 | +# EOT |
| 307 | +# ERB.new(s).result.lines.each {|line| puts line.inspect } |
| 308 | +# "\n" |
| 309 | +# "3.4.5\n" |
| 310 | +# "\n" |
| 311 | +# ``` |
| 312 | +# |
| 313 | +# You can give `trim_mode: '-'`, you can suppress each blank line |
| 314 | +# whose source line ends with `-%>` (instead of `%>`): |
| 315 | +# |
| 316 | +# ``` |
| 317 | +# s = <<EOT |
| 318 | +# <% if true -%> |
| 319 | +# <%= RUBY_VERSION %> |
| 320 | +# <% end -%> |
| 321 | +# EOT |
| 322 | +# ERB.new(s, trim_mode: '-').result.lines.each {|line| puts line.inspect } |
| 323 | +# "3.4.5\n" |
| 324 | +# ``` |
| 325 | +# |
| 326 | +# It is an error to use the trailing `'-%>'` notation without `trim_mode: '-'`: |
| 327 | +# |
| 328 | +# ``` |
| 329 | +# ERB.new(s).result.lines.each {|line| puts line.inspect } # Raises SyntaxError. |
| 330 | +# ``` |
| 331 | +# |
| 332 | +# #### Suppressing Unwanted Newlines |
| 333 | +# |
| 334 | +# Consider this input string: |
| 335 | +# |
| 336 | +# ``` |
| 337 | +# s = <<EOT |
| 338 | +# <% RUBY_VERSION %> |
| 339 | +# <%= RUBY_VERSION %> |
| 340 | +# foo <% RUBY_VERSION %> |
| 341 | +# foo <%= RUBY_VERSION %> |
| 342 | +# EOT |
| 343 | +# ``` |
| 344 | +# |
| 345 | +# With keyword argument `trim_mode` not given, all newlines go into the result: |
| 346 | +# |
| 347 | +# ``` |
| 348 | +# ERB.new(s).result.lines.each {|line| puts line.inspect } |
| 349 | +# "\n" |
| 350 | +# "3.4.5\n" |
| 351 | +# "foo \n" |
| 352 | +# "foo 3.4.5\n" |
| 353 | +# ``` |
| 354 | +# |
| 355 | +# You can give `trim_mode: '>'` to suppress the trailing newline |
| 356 | +# for each line that ends with `'%<'` (regardless of its beginning): |
| 357 | +# |
| 358 | +# ``` |
| 359 | +# ERB.new(s, trim_mode: '>').result.lines.each {|line| puts line.inspect } |
| 360 | +# "3.4.5foo foo 3.4.5" |
| 361 | +# ``` |
| 362 | +# |
| 363 | +# You can give `trim_mode: '<>'` to suppress the trailing newline |
| 364 | +# for each line that both begins with `'<%'` and ends with `'%<'`: |
| 365 | +# |
| 366 | +# ``` |
| 367 | +# ERB.new(s, trim_mode: '<>').result.lines.each {|line| puts line.inspect } |
| 368 | +# "3.4.5foo \n" |
| 369 | +# "foo 3.4.5\n" |
| 370 | +# ``` |
| 371 | +# |
| 372 | +# #### Combining Trim Modes |
| 373 | +# |
| 374 | +# You can combine certain trim modes: |
| 375 | +# |
| 376 | +# - `'%-'`: Enable shorthand and omit each blank line ending with `'%>'`. |
| 377 | +# - `'%>'`: Enable shorthand and omit newline for each line ending with `'%>'`. |
| 378 | +# - `'%<>'`: Enable shorthand and omit newline for each line starting with `'<%'` and ending with `'%>'`. |
| 379 | +# |
286 | 380 | # ### Comment Tags |
287 | 381 | # |
288 | 382 | # You can embed a comment in a template using a *comment tag*; |
@@ -537,19 +631,19 @@ def self.version |
537 | 631 | # |
538 | 632 | # **Keyword Argument `trim_mode`** |
539 | 633 | # |
540 | | - # When keyword argument `trim_mode` has a string value, |
541 | | - # that value may be one of: |
| 634 | + # You can use keyword argument `trim_mode: '%'` |
| 635 | + # to enable the [shorthand format][shorthand format] for execution tags. |
| 636 | + # |
| 637 | + # This value allows [blank line control][blank line control]: |
542 | 638 | # |
543 | | - # - `'%'`: Enable [shorthand format][shorthand format] for execution tags. |
544 | 639 | # - `'-'`: Omit each blank line ending with `'%>'`. |
| 640 | + # |
| 641 | + # Other values allow [newline control][newline control]: |
| 642 | + # |
545 | 643 | # - `'>'`: Omit newline for each line ending with `'%>'`. |
546 | 644 | # - `'<>'`: Omit newline for each line starting with `'<%'` and ending with `'%>'`. |
547 | 645 | # |
548 | | - # The value may also be certain combinations of the above. |
549 | | - # |
550 | | - # - `'%-'`: Enable shorthand and omit each blank line ending with `'%>'`. |
551 | | - # - `'%>'`: Enable shorthand and omit newline for each line ending with `'%>'`. |
552 | | - # - `'%<>'`: Enable shorthand and omit newline for each line starting with `'<%'` and ending with `'%>'`. |
| 646 | + # You can also [combine trim modes][combine trim modes]. |
553 | 647 | # |
554 | 648 | # **Keyword Argument `eoutvar`** |
555 | 649 | # |
@@ -578,9 +672,12 @@ def self.version |
578 | 672 | # However, their values, if given, are handled thus: |
579 | 673 | # |
580 | 674 | # - `safe_level`: ignored. |
581 | | - # - `legacy_trim_mode: overrides keyword argument `trim_mode`. |
582 | | - # - `legacy_eoutvar: overrides keyword argument `eoutvar`. |
| 675 | + # - `legacy_trim_mode`: overrides keyword argument `trim_mode`. |
| 676 | + # - `legacy_eoutvar`: overrides keyword argument `eoutvar`. |
583 | 677 | # |
| 678 | + # [blank line control]: rdoc-ref:ERB@Suppressing+Unwanted+Blank+Lines |
| 679 | + # [combine trim modes]: rdoc-ref:ERB@Combining+Trim+Modes |
| 680 | + # [newline control]: rdoc-ref:ERB@Suppressing+Unwanted+Newlines |
584 | 681 | # [shorthand format]: rdoc-ref:ERB@Shorthand+Format+for+Execution+Tags |
585 | 682 | # |
586 | 683 | def initialize(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout') |
|
0 commit comments