Keep text in lift snippet

北战南征 提交于 2020-01-16 12:28:37

问题


there is a lift snippet:

<lift:Login>
  <entry:name>
     No user logged in
  </entry:name>
</lift:Login>
I know that I can Helpers.bind the user name if the user is logged in, but how can I preserve the former text enclosed in ? There seems to be no support to project prefixed elements when I see scala api, xhtml \\ "entry:name" yields nothing more than empty Node. So how can I accomplish such goal?
EDIT: In case when the user is logged, I want to show: User 123 In the other case, I want to show the original text in snippet, in other words, I want to just remove the lift prefixed tags which are indispensable for framework, but they have nothing to do in end user html : No user logged in

回答1:


It’s not clear what you’ve tried already but in most cases there is no need for Scala xml transformations. Using Helpers.bind is usually sufficient and can deal with xml prefixes properly. (Scala’s xml transformations API sometimes feels a bit uneven in this respect.)

Not 100% sure what you want to do but this is how I’d bind the user name to <entry:name/> if logged in or else show the default text.

class Login {
  def render(xhtml: NodeSeq) = bind("entry", xhtml, "name" -> name _)
  def name(in: NodeSeq) = User.currentUser.map(_.shortName).map(Text(_)) openOr in
}

Addition: The "name" -> name _ part means that the method name should be called with the contents of the <entry:name> tag and the result should replace the whole tag. (I must say, I’m not quite sure what you already know about lift; my impression is that if one knows how to bind User 123 one should also know how to bind other information…)

The trailing underscore is needed to help the compiler here. If you don’t want to do a transformation of the original content of the tag, you’d simply bind a val or a def someMethod: NodeSeq and then use it without underscore or even inline. E.g. bind("entry", xhtml, "name" -> <span>Some NodeSeq</span>)



来源:https://stackoverflow.com/questions/4404111/keep-text-in-lift-snippet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!