Skip to content

Instantly share code, notes, and snippets.

@kjorg50
Last active August 29, 2015 14:14
Show Gist options
  • Save kjorg50/1f832e873a1af622cb46 to your computer and use it in GitHub Desktop.
Save kjorg50/1f832e873a1af622cb46 to your computer and use it in GitHub Desktop.
Adding Bootstrap 3 to Play 2.3

Steps I took to add Bootstrap to the Play 2.3 Hello World app.

  1. Added the following dependencies to the build.sbt file for the project
  "org.webjars" %% "webjars-play" % "2.3.0-2",
  "org.webjars" % "bootstrap" % "3.3.2",
  "org.webjars" % "jquery" % "2.1.3"

The jquery one isn't necessary, but I added it because I might want it in the future.

  1. I added the following lines to app/views/main.scala.html.
<link rel='stylesheet' href='@routes.Assets.at("lib/bootstrap/css/bootstrap.min.css")'>
<script type='text/javascript' src='@routes.Assets.at("lib/jquery/jquery.min.js")'></script>

Unlike the directions here, I found that I was not able to use @routes.WebJarAssets.at() because it could not find the WebJarAssets. So, I tried the instructions here and just used @routes.Assets.at() and this worked. The next trick was figuring out the correct URL where they get generated within /lib. After some trial and error I got it to work with the lines above.

  1. Added a bootstrap field constructor template (example).
@(elements: helper.FieldElements)
<div class="form-group @if(elements.hasErrors) {has-error}">
    <label for="@elements.id" class="control-label">@elements.label</label>
    @elements.input
    <span class="help-block">@elements.infos.mkString(", ")</span>
    <span class="help-block">@elements.errors.mkString(", ")</span>
</div>

This will bootstraipify some of the elements in your HTML

  1. Use the field constructor template in your view
@implicitField = @{ FieldConstructor(bootstrapFieldConstructorTemplate.f) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment