Skip to content

Instantly share code, notes, and snippets.

@arashi01
Last active December 13, 2015 17:49
Show Gist options
  • Save arashi01/4951196 to your computer and use it in GitHub Desktop.
Save arashi01/4951196 to your computer and use it in GitHub Desktop.
Lift Scalate module modifications
html(xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/")
head
meta(content="text/html; charset=UTF-8" http-equiv="content-type")
meta(name="description" content="")
meta(name="keywords" content="")
title(data-lift="Menu.title")
|App:
style(data-lift="CSS.blueprint")
style(data-lift="CSS.fancyType")
script#jquery(src="/#{LiftRules.resourceServerPath}/jquery.js")
script#json(src="/#{LiftRules.resourceServerPath}/json.js")
:css
.edit_error_class {
display: block;
color: red;
}
.sidebar ul {
margin:0;
padding:0;
border-bottom:1px solid #ccc;
}
.sidebar ul li {
margin:0;
padding:0;
list-style:none;
border:1px solid #ccc;
border-bottom:none;
}
.sidebar ul li a {
display:block;
padding:3px;
text-indent:30px;
text-decoration:none;
}
.sidebar ul li span {
display:block;
padding:3px;
text-indent:30px;
text-decoration:none;
}
.sidebar ul li a:hover {
background-color: #eee;
}
body
.container
.column.span-12.last(style="text-align: right")
h1.alt
|Lift Application
img#ajax-loader(alt="" style="display:none; margin-bottom: 0px; margin-left: 5px" src="/images/ajax-loader.gif")
hr
.column.span-6.colborder.sidebar
hr.space
span(class="lift:Menu.builder")
div(class="lift:Msgs?showAll=true")
hr.space
.column.span-17.last
#content
hr
.column.span-23.last(style="text-align: center")
h4.alt
:markdown
[Lift](http://www.liftweb.net) is Copyright 2007-2012 WorldWide Conferencing, LLC. Distributed under an Apache 2.0 License.
!!!
html(xmlns="http://www.w3.org/1999/xhtml")
head
meta(content="text/html; charset=UTF-8" http-equiv="content-type")
title Home
body(class="lift:content_id=main")
#main(data-lift="surround?with=default;at=content")
h2 Welcome to Lift Scalate!
import java.io.File
import tools.nsc.Global
import org.fusesource.scalate.layout.DefaultLayoutStrategy
import org.fusesource.scalate.util.{ClassPathBuilder, FileResourceLoader}
import org.fusesource.scalate.{DefaultRenderContext, ResourceNotFoundException, Binding, TemplateEngine}
import net.liftweb._
import common._
import http.LiftRules
import http.provider.servlet.HTTPServletContext
import util.Props
/**
* A TemplateEngine using the Lift web abstractions.
*/
class LiftTemplateEngine extends TemplateEngine(mode = if (Props.devMode) "development" else "production") with Loggable {
bindings = List(Binding("context", classOf[DefaultRenderContext].getName, importMembers = true, isImplicit = true))
classpath = buildClassPath()
resourceLoader = new LiftResourceLoader(this)
layoutStrategy = new DefaultLayoutStrategy(this, "/WEB-INF/scalate/layouts/default.jade",
"/WEB-INF/scalate/layouts/default.scaml", "/WEB-INF/scalate/layouts/default.ssp")
importStatements = "import _root_.net.liftweb.http.S" ::
"import _root_.net.liftweb.http.LiftRules" :: importStatements
allowReload = false
private def buildClassPath(): String = {
val builder = new ClassPathBuilder
// Add containers class path
builder.addPathFrom(getClass)
.addPathFrom(classOf[TemplateEngine])
.addPathFrom(classOf[Product])
.addPathFrom(classOf[Global])
builder.addClassesDir(realPath("/WEB-INF/classes/webapp"))
builder.classPath
}
def realPath(uri: String): String = {
LiftRules.context match {
case http: HTTPServletContext => http.ctx.getRealPath(uri)
case c => logger.warn("Do not know how to get the real path of: " + uri + " for context: " + c); uri
}
}
class LiftResourceLoader(context: LiftTemplateEngine) extends FileResourceLoader {
override protected def toFile(uri: String) = {
realFile(uri)
}
protected def toFileOrFail(uri: String): File = {
val file = realFile(uri)
if (file == null) {
throw new ResourceNotFoundException(resource = uri, root = context.realPath("/"))
}
file
}
/**
* Returns the real path for the given uri
*/
def realPath(uri: String): String = {
val file = realFile(uri)
if (file != null) file.getPath else null
}
/**
* Returns the File for the given uri
*/
def realFile(uri: String): File = {
def findFile(uri: String): File = {
/*
val url = LiftRules.context.resource(uri)
if (url != null) {
url.toFile
}
else {
null
}
*/
val path = context.realPath(uri)
logger.debug("realPath for: " + uri + " is: " + path)
var answer: File = null
if (path != null) {
val file = new File(path)
logger.debug("file from realPath for: " + uri + " is: " + file)
if (file.canRead) {
answer = file
}
}
answer
}
findFile(uri) match {
case file: File => file
case _ => if (uri.startsWith("/") && !uri.startsWith("/WEB-INF")) {
findFile("/WEB-INF" + uri)
}
else {
null
}
}
}
}
}
import net.liftweb._
import common._
import util._
import http._
/**
* A `LiftView` which uses a <a href="http://scalate.fusesource.org/">Scalate</a>
* template engine to resolve a URI and render it as markup
*/
class ScalateView(engine: LiftTemplateEngine = new LiftTemplateEngine()) extends Logger {
/**
* Registers this view with Lift's dispatcher
*/
def register() {
val scalateView: ScalateView = this
/**
* Registers viewDispatch to render templates using scalate.
*/
LiftRules.viewDispatch.prepend(NamedPF("Scalate View") {
case path if (canRender(path, "scaml")) =>
debug("scalate viewDispatch Path: [" + path.mkString("/") + "]")
Left(() => Full(engine.layoutAsNodes(createUri(path, "scaml"))))
case path if (canRender(path, "jade")) =>
debug("scalate viewDispatch Path: [" + path.mkString("/") + "]")
Left(() => Full(engine.layoutAsNodes(createUri(path, "jade"))))
case path if (canRender(path, "ssp")) =>
debug("scalate viewDispatch Path: [" + path.mkString("/") + "]")
Left(() => Full(engine.layoutAsNodes(createUri(path, "ssp"))))
})
}
def canRender(path: List[String], ext: String): Boolean = {
debug("=== attempting to find: " + path + " ext: '" + ext + "'")
if (ext == "") {
canLoad(createUri(path, "scaml")) || canLoad(createUri(path, "jade")) || canLoad(createUri(path, "ssp"))
}
else {
val uri = createUri(path, ext)
(uri.endsWith(".scaml") || uri.endsWith(".jade") || uri.endsWith(".ssp")) && canLoad(uri)
}
}
protected def createUri(path: List[String], ext: String): String = path.mkString("/") +
(if (ext.length > 0) "." + ext else "")
protected def canLoad(v: String): Boolean = {
engine.canLoad(v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment