Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can snippets take parameters in lift?

Tags:

scala

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

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

like image 331
Andriy Drozdyuk Avatar asked Jun 28 '11 18:06

Andriy Drozdyuk


1 Answers

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"/>
like image 83
overthink Avatar answered Oct 21 '22 12:10

overthink