当你需要将一个页面的内容分为几块时(大多数情况下你都会这样做).可以使用embed方式
将需要切分的页面内容放入embed.html,位置在webapp/下
在你需要调用这个内容地方使用
<span class="lift:embed?what=embed">
replaced with embedded content
</span>保存后刷新页面,你会看到此处内容被替换为embed.html中的内容(被引用的内容自然也会被解析)
一个增强型例子
/webapp/newtemp/form1.html
<div>
<form class="lift:DumbForm?form=post">
Name: <input name="name"/><br/>
Age: <input name="age" value="0"/><br/>
<input type="submit" value="Submit"/>
</form>
</div>
index.html
<lift:embed what="/newtemp/form1">
submit my first submit
</lift:embed>你还记得用create语句创建snippet么
lift create snippet
按提示输入DumbFormpackage example.travel.snippet
import net.liftweb._
import http._
import util.Helpers._
import scala.xml.NodeSeq
object DumbForm {
def render = {
// define some variables to put our values into
var name = ""
var age = 0
// process the form
def process() {
// if the age is < 13, display an error
if (age < 13) S.error("Too young!")
else {
// otherwise give the user feedback and
// redirect to the home page
S.notice("Name: "+name)
S.notice("Age: "+age)
S.redirectTo("/")
}
}
// associate each of the form elements
// with a function... behavior to perform when the
// for element is submitted
"name=name" #> SHtml.onSubmit(name = _) & // set the name
// set the age variable if we can convert to an Int
"name=age" #> SHtml.onSubmit(s => asInt(s).foreach(age = _)) &
// when the form is submitted, process the variable
"type=submit" #> SHtml.onSubmitUnit(process)
}
}现在你可以重新运行一下程序,有什么惊喜发现么?
本文介绍如何在Lift Web框架中使用嵌入(embed)功能来组织页面内容,通过示例展示了如何创建和调用可复用的表单片段(snippet),并实现表单提交后的变量处理。
1206

被折叠的 条评论
为什么被折叠?



