Can snippets take parameters in lift?

只愿长相守 提交于 2019-12-10 01:21:20

问题


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 "" or "s".

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


回答1:


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"/>



回答2:


<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.




回答3:


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


来源:https://stackoverflow.com/questions/6511665/can-snippets-take-parameters-in-lift

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