Created
July 21, 2013 17:37
-
-
Save ntbrock/6049260 to your computer and use it in GitHub Desktop.
PartialView Controller Example. Play Framework 2.1.x Scala. Usage in conf/routes:
GET /:controller/_:partial controllers.PartialView.render(controller:String, partial: String) Define this view file: app/views/DemoSix/_helloPartialWorld.scala.html Usage in Browser URL bar: http://localhost:9000/DemoSix/_helloPartialWorld Easy method to use partia…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import java.lang.reflect.Method | |
import play.api._ | |
import play.api.mvc._ | |
import play.api.libs.{ Comet } | |
import play.api.libs.iteratee._ | |
import play.api.libs.concurrent._ | |
import scala.concurrent.duration._ | |
import play.api.libs.concurrent.Execution.Implicits._ | |
import models._ | |
/** | |
* A Play Scala 2.1.x Controller prototype to allow easy inclusion of Ruby on Rails style Partials | |
* using the underscore naming convention. | |
* | |
* Kudos to: | |
* http://thomasheuring.wordpress.com/2013/01/29/scala-playframework-2-04-get-pages-dynamically/ | |
* | |
* Authored: Taylor Brockman, Identityship, Inc. 2013-Jul-21 | |
* | |
* Example Usage in conf/routes to reserve _ as a special character: | |
* | |
* GET /:controller/_:partial controllers.PartialView.render(controller:String, partial: String) | |
* | |
* Strict example to lock to a specific controller: | |
* GET /DemoFive/_:partial controllers.PartialView.render(controllerName = "DemoFive", partial: String) | |
* | |
* Example: ./app/views/DemoSix/_helloPartialWorld.scala.html | |
* | |
* Example: request loads: http://localhost:9000/DemoSix/_helloPartialWorld | |
* | |
* TODO: Add Dynamic GET parameter mapping somehow! | |
*/ | |
object PartialView extends Controller { | |
/** | |
* Entry point that returns an Ok or NotFound of the partial requested. | |
* | |
*/ | |
def render(controllerName: String, partialName: String) = Action { | |
// Autosense the underscore at start | |
val divider = partialName.startsWith("_") match { case true => ""; case _ => "_" } | |
renderDynamic("views.html." + controllerName + "." + divider + partialName) match { | |
case Some(i) => Ok(i) | |
case None => NotFound | |
} | |
} | |
/** | |
* Use Java reflection to find views in the namespace | |
*/ | |
def renderDynamic(viewClazz: String): Option[play.api.templates.Html] = { | |
try { | |
val clazz: Class[_] = Play.current.classloader.loadClass(viewClazz) | |
// PRINT STATEMENT DEBUGGING | |
//println("renderDynamic: Found clazz: " + viewClazz ) | |
//println("renderDynamic: Methods: " + clazz.getMethods().toArray) | |
//for ( method <- clazz.getMethods() ) { | |
// println("renderDynamic: Method: " + method ) | |
//} | |
val render: Method = clazz.getDeclaredMethod("render") //Took off parameter: , classOf[String]) | |
val view = render.invoke(clazz).asInstanceOf[play.api.templates.Html] // Took off parameteR: , "test" | |
return Some(view) | |
} catch { | |
case ex: ClassNotFoundException => Logger.error("Html.renderDynamic() : could not find view " + viewClazz, ex) | |
} | |
return None | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment