liftweb问答备忘录-dynamically building the menu

[color=red]Ask:[/color]
Hi,
I want to add/remove Menu at runtime, for dynamically managing menu
list.
so I need to persist the menu list(SiteMap?) to the database,
and retrieve it from the database when users access the home page.

the menu items link to the same page, pass some different params so
let that page show different data.
so I want to dynamically build the menu of home page.

is it possible to do this by using SiteMap and Menu of liftweb?


[color=red]Answer:[/color]
In a word, yes.

In a bunch of example... well if you have a static menu that you want to
enable, disable based on some calculation, use If() and Unless() when you're
defining your Loc(). You can see an example in ProtoUser.scala.

For a custom menu based on RDBMS content:

class BaseContentLoc(val name: String, _aspect: String) extends
Loc[CustomContent] {
// the name of the page
// def name = "Content"

val BaseAspect = _aspect

def defaultParams = Full(ContentLocStuff.NullCustomContent)

override def forceParam = defaultParams

// the default parameters (used for generating the menu listing)
override def additionalKidParams =
CustomContent.findAll(By(CustomContent.aspect, BaseAspect),

OrderBy(CustomContent.displayOrder, Ascending))

// no extra parameters
def params = List(Loc.PlaceHolder, Loc.Template(myTemplate))

def myTemplate() =
<lift:surround with="default-final" at="content"><lift:display
/></lift:surround>

/**
* Generate a link based on the current page
*/
val link =
new Loc.Link[CustomContent](List(BaseAspect), false) {
override def createLink(in: CustomContent) = {
Full(Text("/"+urlEncode(BaseAspect)+"/"+urlEncode(in.page)))
}
}

/**
* What's the text of the link?
*/
val text = new Loc.LinkText(calcLinkText _)

def calcLinkText(in: CustomContent): NodeSeq = {
if (in.page.length > 0) Text(in.page) else Text(name)
}

object Finder {
def unapply(page: String): Option[CustomContent] =
CustomContent.findContent(page, BaseAspect)
}

/**
* Rewrite the request and emit the type-safe parameter
*/
override val rewrite: LocRewrite =
Full({
case RewriteRequest(ParsePath(BaseAspect :: Finder(content) :: Nil,
_, _, _), _, _) =>
(RewriteResponse(BaseAspect :: Nil), content)
})

/**
* Check for page-specific snippets and
* do appropriate dispatching
*/
override val snippets: SnippetTest = {
case ("display", Full(v)) => display(v) _
}

def display(v: CustomContent)(in: NodeSeq) = v.content openOr in

}

Thanks,

David
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值