Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Template Library

I'm trying to create a view that only houses reusable HTML blocks that can be used by other views. Wanted to know if something like this is possible:

In views.home.common.scala.html:

@component1 = {
  some common html
}
@component2 = {
  some other stuff
}

In views.home.sample.scala.html:

@(user:User)
import home._

@component1
@common.component2

Haven't had any luck thus far and I don't see anything similar in the samples but the idea is covered in the Template common use cases.

like image 620
user1257547 Avatar asked Mar 19 '26 18:03

user1257547


1 Answers

I was having this same issue. What I did was defining for each common block a file and then import the package that contains all those files.

For example:

In views.common.component1.scala.html:

<div>
    Common component 1
</div>

In views.common.component2.scala.html:

<div>
    Common component 2
</div>

In views.main.scala.html:

@(content: Html)

@import common._

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        @component1()
        @component2()
    </body>
</html>
like image 166
GuidoMB Avatar answered Mar 21 '26 06:03

GuidoMB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!