just a few minutes and you have a complete Play application based on the Play Pagelets Module running.
See the app in action: Pagelets on Heroku *note that it runs on a free dyno which takes a couple of seconds to wake up.
The demo application serves the purpose to
- showcase the Play Pagelets Module and it's advantages
- demonstrate how to build a pagelet based application
- serve as a template for pagelet based projects
A pagelet is a small independent unit which consists of view, controller action and optionally a service which obtains the data to be rendered. Usually a web page is composed of multiple pagelets.
This demo application shows how a modular and resilient web application can be built using the Playframework 2 and the Play Pagelets Module.
This example app is a small multi-language website. The page is composed of multiple pagelets. Each pagelet is completely independent and obtains data from a remote service and renders the data by means of a standard twirl template.
Depending on the selected language, the home page is composed from different pagelets. This shows how the page composition can be manipulated at runtime based on the properties of an incoming request.
The demo also invites to to simulate failure by configuring remote services to time-out or serve broken data.
A page configuration
def tree(r: RequestHeader) =
Tree("root".id, Seq(
Leaf("header".id, () => header).withJavascript(Javascript("lib/bootstrap/js/dropdown.min.js")),
Tree("content".id, Seq(
Leaf("carousel".id, () => carousel).withFallback(fallback("Carousel") _),
Leaf("text".id, () => text).withFallback(fallback("Text") _)
)),
Leaf("footer".id, () => footer).withCss(Css("stylesheets/footer.min.css"))
))
A main action to render a complete page
def index = PageAction.async(routes.HomeController.errorPage)(_ => "Page Title", tree) { (request, page) =>
views.html.wrapper(routes.HomeController.resourceFor)(page)
}
A pagelet (just a standard Play action)
def carousel = Action.async { implicit request =>
carouselService.carousel.map { teasers =>
Ok(views.html.pagelets.carousel(teasers))
}
}
A fallback pagelet (also just a standard Play action)
def fallback(name: String)() = Action {
Ok(views.html.pagelets.fallback(name))
}
Open a terminal and clone the github repository with
git clone git@github.com:splink/pagelets-seed.git
- then enter
cd play-pagelets-seed
- then enter
sbt run
then point your browser to http://localhost:9000
If you are interested in more details, check out the main Play Pagelets repository