Search code examples
eclipse-emfacceleo

How to remove extra new line after template with loop in Acceleo?


Here is a simple MOF Model to Text-script:

[comment encoding = UTF-8 /]
[module test('http://www.eclipse.org/uml2/2.1.0/UML')/]

[template public test(element : Model)]
[comment @main /]
[file ('test.txt', false, 'UTF-8')]
start
  [loop(element)/]
  [loop(element)/]
end
[/file]
[/template]

[template public loop(element : Model)]
[for (var : Integer | Sequence{1..3})]
[var/]
[/for]
[/template]

It generates the following text:

start
  1
  2
  3

  1
  2
  3

end

How to remove extra new lines after 3? Thanks!


Solution

  • You can use separator in the for:

    [template public loop(element : Model)]
    [for (var : Integer | Sequence{1..3}) separator('\n')]
    [var/][/for]
    [/template]
    

    or you can trim the result of template "loop":

    [template public loop(element : Model) post(trim())]
    [for (var : Integer | Sequence{1..3})]
    [var/]
    [/for]
    [/template]