Can snippets take parameters in lift?
Asked Answered
N

3

13

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.

Nashua answered 28/6, 2011 at 18:55 Comment(0)
T
17

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"/>
Tough answered 28/6, 2011 at 19:33 Comment(7)
Except one should never use open_!. Instead: val x = S.attr("myparam") openOr "Dude, myparam is not defined"Imes
Hm... but could I actually pass a context value, like num_messages (above) to the snippet?Nashua
Also - is there way to get by without these "Boxes"? I am not sure how it is better than option - just more confusing.Nashua
Also what does "<p>I got {x}!</p>" mean? How can you just insert html code into scala code like that? I tried - it works, but its weird!Nashua
@drozzy Box is similar to Option, but has some nice additional functionality, like being able to carry a failure/exception as well as being Full(x) or Empty. I'm not an expert, but here's some reading to look over: blog.getintheloop.eu/2010/04/16/understanding-lifts-boxt-monad. I don't think you can avoid Box in Lift, and once you start using it a bit you'll start to appreciate it.Tough
@drozzy "<p>I got {x}!</p>" is an XML literal which is a feature of the Scala language (not Lift-specific).Tough
@Tough let us continue this discussion in chatNashua
C
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.

Claribelclarice answered 28/6, 2011 at 20:5 Comment(0)
S
2

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))
}
Stumpy answered 6/7, 2011 at 20:21 Comment(1)
Great stuff! I wander if I could inject it into multiple ids at the same time. But that's another question...Nashua

© 2022 - 2024 — McMap. All rights reserved.