开发者

Can snippets take parameters in lift?

Is there a way in lift to pass parameters to snippets?

I am trying to write a pluraize filter for my page that will display the word "user" or "users" depending on how many there are:

1 user
2 users

The way it works in Django is called filters and they are written as follows:

You have {{ num_messages }} message{{ num_messages|pluralize }}.

So here you can see pluralize function takes an integer num_messages and outputs and appropriate string - either empty "" o开发者_如何学编程r "s".

EDIT: Note that the num_messages in this case is an actual context variable, passed down to the template from the view.


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))
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜