Can snippets take parameters in lift?

谁说胖子不能爱 提交于 2019-12-05 01:38:28

You can pass parameters to snippets, yes.

class MySnippet {
  def foo: NodeSeq = {
    x = S.attr("myparam") openOr "myparam: Y U NO DEFINED!?"
    <p>I got {x}!</p>
  }
}

Use:

<lift:MySnippet.foo myparam="3"/>

Or, newer Lift 2.3+ style:

<div class="lift:MySnippet.foo?myparam=3"/>
<div id="main" class="cl1 cl2 lift:surround?with=default;at=content">

This is also a snippet invocation with parameters.

See lift docs: Lift docs, 3.4.1 Snippets in markup

In order to indicate that content is dynamic, the markup contains a snippet invocation. That typically takes the form class="someclass someothercss lift:mysnippet". If a class attribute contains lift:xxx, the xxx will be resolved to a snippet. The snippet may take attributes. Attributes are encoded like URL parameters... offset by a ? (question mark), then name=value, separted by ? (question mark), ; (semicolon) or & (ampersand). name and value are URL encoded.

Can't you do it like this way.

<div class="lift:MyClass">
  You have <span class="num_messages"/>.
</div>

and your lift code would look something like:

class MyClass {
 def render = "num_messages" #> (num_messages + pluralize("message", num_messages))
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!