Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Play 2.3.8] If condition bring line feed in scala template #77

Open
puneetar opened this issue May 11, 2015 · 9 comments
Open

[Play 2.3.8] If condition bring line feed in scala template #77

puneetar opened this issue May 11, 2015 · 9 comments

Comments

@puneetar
Copy link

Hi all,

I am facing issue with if conditions in scala template. In the following circumstance, the html will have white spaces and line breaks in between the two divs.

<div>
    content in previous div.
</div>
@if(condition) {
    <div>
        content in next div
    </div>

}
@jroper
Copy link
Member

jroper commented May 12, 2015

Is that problem?

@puneetar
Copy link
Author

Yup, because of the line breaks and white spaces, the CSS breaks.. There is extra spacing added in between the spaces.

@jroper
Copy link
Member

jroper commented May 12, 2015

What do you mean the css breaks? Space between divs should have no impact. Anyway, if you want to remove the whitespace, you can do this:

<div>
    content in previous div.
</div>
@if(condition) {<div>
        content in next div
    </div>

}

@marcospereira
Copy link
Member

@puneetar could you please send us a comprehensive test case? If you could show us the html with the CSS and explain exactly what is broken, it will be easier to understand what is happening.

@slorber
Copy link

slorber commented Aug 18, 2015

The problem is that it is not easy to use @if conditions without introducing whitespaces, even when it's not intended to append a white space.

@Messages("emails.invitedToSharing.sharedTheSpace")
@categoryName(categoryMailData)
@Messages("emails.invitedToSharing.withYou")
@if(categoryMailData.isCommunitySubCategory) {
    ,<span> @Messages("emails.invitedToSharing.inCommunity")</span>           
    <span>@categoryName(categoryMailData.inCommunity.get)</span>  
}

The output is:
Edward Silhol shared the space CategoryName with you , in community CommunityName

Expected:

Edward Silhol shared the space CategoryName with you, in community CommunityName

(I don't want a space between "with you" and the ",")

I can confirm the solution of @jroper works, but this really makes the HTML much more unreadable and I guess many developers will try to reformat it, creating a bug...

@Messages("emails.invitedToSharing.sharedTheSpace")
@categoryName(categoryMailData)
@Messages("emails.invitedToSharing.withYou")@if(categoryMailData.isCommunitySubCategory) {,
    <span>@Messages("emails.invitedToSharing.inCommunity")</span>
    <span>@categoryName(categoryMailData.inCommunity.get)</span>
}

@wsargent
Copy link
Member

@slorber in those situations, you're better off using a helper (either in a Scala file, a Twirl template, or a fragment) that will do the logic for you.

@Messages("emails.invitedToSharing.sharedTheSpace")
@categoryName(categoryMailData)
@helper.intro(categoryMailData)
import views.helper.html

object intro {
   def apply(categoryMailData: CategoryMailData): Html = {
     ...
   }
}

@slorber
Copy link

slorber commented Sep 1, 2015

yes that could do the job too

@steinybot
Copy link

This makes Twirl impractical for plain text templating, say for example when you have both HTML and plain text emails.

I quite like having the template separate from my code.

I think the approach that Freemarker takes with whitespace stripping and an additional compress directive is pretty good and solves the simple cases.

@steinybot
Copy link

Another solution that may work is to allow any whitespace to go between the keyword and the brace.

Here is an example of how I want to use Twirl:

baseEmail.scala.txt

@(givenName: String)(content: Txt)
Hi @givenName,

@content

Thanks

newAccountEmail.scala.txt

@(givenName: String, admin: Boolean, url: String)
@baseEmail(givenName) {
@if(admin) {
An administrator account has been created for you.

Once you have activated your account, you can create accounts for other users in your organization.
} else {
An account has been created for you.
}

Use the link below to set the a password and activate your account.

Set password: @url
}

The current workaround:

newAccountEmail.scala.txt

@(givenName: String, admin: Boolean, url: String)
@baseEmail(givenName) {@if(admin) {An administrator account has been created for you.

Once you have activated your account, you can create accounts for other users in your organization.} else {An account has been created for you.}

Use the link below to set the a password and activate your account.

Set password: @url}

If braces could go on the following line it would be a little bit better:

newAccountEmail.scala.txt

@(givenName: String, admin: Boolean, url: String)
@baseEmail(givenName)
{@if(admin)
{An administrator account has been created for you.

Once you have activated your account, you can create accounts for other users in your organization.}
else
{An account has been created for you.}

Use the link below to set the a password and activate your account.

Set password: @url}

Ideal solution would be to have some kind of directive that allows the whitespace stripping from lines that only contain tags. At least this would be backwards compatible. Something like: @setting(settings.stripTagLines, true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants