Using templates in Lift modules

219 views
Skip to first unread message

Elazar Leibovich

unread,
Jul 15, 2010, 4:34:34 AM7/15/10
to Lift
I was reading the MegaProtoUsers and the CRUDify traits' code
yesterday, and something left me uncomfortable. Those traits have
their templates which create the actual forms used to login/logout
etc. in the code! Such as the XHTMLLogin method, etc.

I'm trying to modularize my code as much as possible, so I'd rather
develop parts in my site as a self-contained lift modules, than, for
instance as snippets.

Is there a way to include a view XML file in the module?

Assuming there isn't, can I say something like "override def
XHTMLlogin = getTemplate('user_mgr/login') openOr <default><template/
><for/><login/></default>"?

Daniel Hobi

unread,
Jul 15, 2010, 9:54:53 AM7/15/10
to Lift
I think TemplateFinder is what you are looking for:
http://main.scala-tools.org/mvnsites/liftweb-2.0/framework/scaladocs/net/liftweb/http/TemplateFinder$object.html

In your case it would be something like:
TemplateFinder.findAnyTemplate("user_mgr/login" :: Nil)

Daniel

Elazar Leibovich

unread,
Jul 15, 2010, 10:47:00 AM7/15/10
to Lift
The template finder is a good second option, but what I'm really
looking for is, a way to refactor MegaProtoUser to include a template
XML file for all their login page needs. I want the template to be
inside the module, but not in a scala code.

On Jul 15, 4:54 pm, Daniel Hobi <d.h...@gmx.ch> wrote:
> I think TemplateFinder is what you are looking for:http://main.scala-tools.org/mvnsites/liftweb-2.0/framework/scaladocs/...

TylerWeir

unread,
Jul 15, 2010, 10:52:55 AM7/15/10
to Lift
My User object has an overridden edit template in it.

Keep in mind the templates inside ProtoUser are option.

If you really want to use something else, you just need to write that
code.

Elazar Leibovich

unread,
Jul 15, 2010, 3:55:58 PM7/15/10
to Lift
I'll rephrase my question.
I'm writing my own user module now. I want to include templates in the
new user module.
Or, how can I refactor ProtoUsers (theoretically, I'm not sure it
worth it now), to have the template outside the code.
How can I do that (if this is possible at all).

Ross Mellgren

unread,
Jul 15, 2010, 4:01:39 PM7/15/10
to lif...@googlegroups.com
private def withResource[A](path: String)(f: InputStream => A): A = {
val inputStream = getClass.getResourceAsStream(path)
try {
f(is)
} finally {
try {
inputStream.close
} catch {
case _ => ()
}
}
}

override def loginXhtml = withResource("login.html")(PCDataXmlParser)
override def changePasswordXhtml = withResource("changepassword.html")(PCDataXmlParser)

-Ross

> --
> You received this message because you are subscribed to the Google Groups "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.
>

David Pollak

unread,
Jul 15, 2010, 4:06:47 PM7/15/10
to lif...@googlegroups.com
On Thu, Jul 15, 2010 at 12:55 PM, Elazar Leibovich <ela...@gmail.com> wrote:
I'll rephrase my question.
I'm writing my own user module now. I want to include templates in the
new user module.
Or, how can I refactor ProtoUsers (theoretically, I'm not sure it
worth it now), to have the template outside the code.
How can I do that (if this is possible at all).


If you look at loginXhtml:
  def loginXhtml = {
    (<form method="post" action={S.uri}><table><tr><td
              colspan="2">{S.??("log.in")}</td></tr>
          <tr><td>{S.??("email.address")}</td><td><user:email /></td></tr>
          <tr><td>{S.??("password")}</td><td><user:password /></td></tr>
          <tr><td><a href={lostPasswordPath.mkString("/", "/", "")}
                >{S.??("recover.password")}</a></td><td><user:submit /></td></tr></table>
     </form>)
  }

If you want to subclass ProtoUser, you could do:

override def loginXhtml = TemplateFinder.findAnyTemplate("my_user_templates" :: "login" :: Nil) openOr <b>OMG... missing tempalte</b>

Rather than using S.??(...) for localization, you'd support a login.html, login_EN_us.html, etc. template for each locale.

All the xxxXhtml methods in ProtoUser are designed to be overridden by something that looks the template up.

Now, why did we include the markup in ProtoUser?  Well... we could either include the markup in Scala or put it in source resource file that's not editable, but baked into the JAR... it makes no difference for how much work you have to do to change the template.


 
--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

Elazar Leibovich

unread,
Jul 15, 2010, 4:47:25 PM7/15/10
to Lift
On Jul 15, 11:06 pm, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> On Thu, Jul 15, 2010 at 12:55 PM, Elazar Leibovich <elaz...@gmail.com>wrote:
>
> > I'll rephrase my question.
> > I'm writing my own user module now. I want to include templates in the
> > new user module.
[snipped]
> Now, why did we include the markup in ProtoUser?  Well... we could either
> include the markup in Scala or put it in source resource file that's not
> editable, but baked into the JAR... it makes no difference for how much work
> you have to do to change the template.

It makes no difference from the library user perspective, however it
does makes a difference from the maintainer perspective, assuming of
course you have a larger piece of less trivial default template you
wish to include in the module.
> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com >
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/liftweb?hl=en.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890

David Pollak

unread,
Jul 15, 2010, 4:53:12 PM7/15/10
to lif...@googlegroups.com
On Thu, Jul 15, 2010 at 1:47 PM, Elazar Leibovich <ela...@gmail.com> wrote:
On Jul 15, 11:06 pm, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> On Thu, Jul 15, 2010 at 12:55 PM, Elazar Leibovich <elaz...@gmail.com>wrote:
>
> > I'll rephrase my question.
> > I'm writing my own user module now. I want to include templates in the
> > new user module.
[snipped]
> Now, why did we include the markup in ProtoUser?  Well... we could either
> include the markup in Scala or put it in source resource file that's not
> editable, but baked into the JAR... it makes no difference for how much work
> you have to do to change the template.

It makes no difference from the library user perspective, however it
does makes a difference from the maintainer perspective,

Yes.  Maintaining two different pieces of code for the default ProtoUser would increase the complexity and likelihood of error.  Having the embedded code saves us library authors a lot of heart-ache.
 
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

Chaba

unread,
Jul 19, 2010, 12:50:07 PM7/19/10
to Lift
ProtoUser.scala has the following definition:
def loginXhtml = { (<form method="post" action={S.uri}
><table><tr><td ...

I think Scala assumes "def loginXhtml: scala.xml.Elem" from this
declaration.

If I use
override def loginXhtml = TemplateFinder.findAnyTemplate("user" ::
"login" :: Nil).openOr <b>Missing login template</b>

the compiler says that findAnyTemplate.openOr returns a
scala.xml.NodeSeq, but loginXhtml should return scala.xml.Elem type
(The type returned by findAnyTemplate is actually scala.xml.Document)

So:

1. How can I get xml.Elem out of findAnyTemplate? The login.html is
just inner XML, not a full document.

2. In case of signupXhtml / editXhtml I will need to create my own
"localForm" function too, right?
I want to have different fields in these two form, ex. the "edit"
form should not have captcha input.

Thanks,
Chaba


On Jul 15, 1:06 pm, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com >
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/liftweb?hl=en.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net
> Beginning Scalahttp://www.apress.com/book/view/1430219890

Naftoli Gugenheim

unread,
Jul 19, 2010, 3:09:58 PM7/19/10
to lif...@googlegroups.com
findAnyTemplate returns a NodeSeq? That's strange. A template has to be an Elem, no?


To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.

Chaba

unread,
Jul 19, 2010, 4:48:22 PM7/19/10
to Lift
LiftSession.scala
def findAnyTemplate(places: List[String]): Box[NodeSeq]

See it here: http://github.com/lift/lift/tree/2.x-2.8_devel/framework/lift-base/lift-webkit/src/main/scala/net/liftweb/http/

BTW, also there's a "def findAndProcessTemplate(name: List[String]):
Box[Elem]" which might do the trick.

Chaba
> > <liftweb%2Bunsu...@googlegroups.com<liftweb%252Bunsubscribe@googlegroup s.com>>
Reply all
Reply to author
Forward
0 new messages