Skip to content

Instantly share code, notes, and snippets.

@drewhamlett
Created September 6, 2012 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewhamlett/3657097 to your computer and use it in GitHub Desktop.
Save drewhamlett/3657097 to your computer and use it in GitHub Desktop.
Render dynamic views with Play 2.0
package helpers
import play.api.Play
import play.Logger
import java.lang.reflect.Method
/**
* The Dynamic object is responsible for calling on static pages via a keyword
* This makes updating and adding pages easier without having to add routes for the pages
*/
object Dynamic {
def renderSubCategory(keyword : String) : Option[ play.api.templates.Html ] = {
renderDynamic("views.html.equipment.static.subcategory." + keyword)
}
def render(keyword : String) : Option[ play.api.templates.Html ] = {
renderDynamic("views.html.static." + keyword)
}
def renderDynamic(viewClazz : String) : Option[ play.api.templates.Html ] = {
try {
val clazz : Class[ _ ] = Play.current.classloader.loadClass(viewClazz)
val render : Method = clazz.getDeclaredMethod("render")
return Some(render.invoke(clazz).asInstanceOf[ play.api.templates.Html ])
}
catch {
case ex : Exception => Logger.error("Html.renderDynamic() : could not find view " + viewClazz, ex)
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