سؤال

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!

هل كانت مفيدة؟

المحلول

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]

نصائح أخرى

Does post(trim()) help you ?

Some examples I use:

[template public bodyOperation(g : Getter) ? post (trim())]

[template public typeKind(t : Type) post (replaceAll('\n', '').trim())]

It seems that I've found a hack:

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

[/if][var/][/for]
[/template]

But it's a little bit ugly...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top