Search code examples
template-enginerythm

How to remove a newline from template?


This is a repost of my question in the Google Group. Hopefully I will get some response here.

Frequently I run into this problem. I want to generate a line of text if the text is not empty. If it is empty, do not generate the line. Illustration template:

namespace @classSpec.getNamespace()

@classSpec.getComment()
class @classSpec.getName() {
  ...
}

If @classSpec.getComment() returns meaningful comment text, the result looks like

namespace com.example

// this is comment
class MyClass {
  ...
}

But if there is no comment, it will be

namespace com.example


class MyClass {
  ...
}

Notice the extra empty line? I do not want it. Currently the solution is to write template as

namespace @classSpec.getNamespace()

@classSpec.getComment()class @classSpec.getName() {
  ...
}

and make sure the getComment() will append a "\n" to the return value. This makes the template much less readable. Also, imagine I need to generate a function with multiple parameters in a for loop. If each parameter requires complex logic of template code, I need to make them all written in one line as above. Otherwise, the result file will have function like

function myFunction(
  String stringParam,
  Integer intParam,
  Long longParam
)

The core problem is, the template file does not only contain scripts, but also raw text to be written in the output. For script part, we want newlines and indentations. We want the space to be trimmed just like what compilers usually do. But for raw text, we want the spaces to be exact as specified in the file. I feel we need a bit more raw text control mechanism to reconcile the two parts.

Specific to this case, is there some special symbol to treat multiple lines as single line in the output? For example, like if we can write

namespace @classSpec.getNamespace()

@classSpec.getComment()\\
class @classSpec.getName() {
  ...
}

Thanks!


Solution

  • This is just a known bug see

    Unfortunately there is no proper work-around for this yet. You might want to add your comments to the bugs above and reference your question.

    Take the example below which you can try out at http://fiddle.rythmengine.org/#/editor

    @def setXTimesY(int x,int y) { @{ int result=x*y;} @(result)}
    1
    2 a=@setXTimesY(2,3)
    3 b=@setXTimesY(3,5)
    4 c=@setXTimesY(4,7)
    5
    

    this will properly create the output:

    1
    2 a= 6
    3 b= 15
    4 c= 28
    5
    

    now try to beautify the @def setXTimesY ...

    @def setXTimesY(int x,int y) { 
    @{ 
      int result=x*y;
    }@(result)}
    1
    2 a=@setXTimesY(2,3)
    3 b=@setXTimesY(3,5)
    4 c=@setXTimesY(4,7)
    

    will give a wrong result

    1 2 a=(result) 3 b=(result) 4 c=(result)

    @def setXTimesY(int x,int y) { 
    @{ 
      int result=x*y;
    } @(result)}
    1
    2 a=@setXTimesY(2,3)
    3 b=@setXTimesY(3,5)
    4 c=@setXTimesY(4,7)
    

    is better but adds a space

    So

    is another bug along the same lines