views:

51

answers:

1

My problem is extracting xhtml attributes to generate absolute links, since they need to be different on testing and production environment. I would like to use a "global snippet" that binds all "src" and "href" attributes to "localhost:8080" or "www.mydomain.com" depending on a conf value.

This is how the template looks like:

<lift:Global>
  <html><body><a G:href="/somelink">some text</a></body></html>
</lift:Global>

And this is the Global.render method:

bind("G",template,
  AttrBindParam("href",Conf.localhost
    +BindHelpers.attr("G","href").map(_.toString).getOrElse("none") ,"href")
)

But in the rendered page all I see is ... href="confValueNone".

What am I doing wrong? Is there a better way to configure for different environments?

+1  A: 

I use AttributeSnippets now. They are a bit heavier on the template side, but result in cleaner snippets.

snippet:

import xml.{UnprefixedAttribute, MetaData}

...

def src(in:MetaData):MetaData = {
  new UnprefixedAttribute("src",Conf.localhost+in.value.toString,scala.xml.Null)
}

def href(in:MetaData):MetaData = {
  val out = new UnprefixedAttribute("href",Conf.localhost+in.value.toString,scala.xml.Null)
  out
}

template:

...
<script type="text/javascript" lift:Global.src="/inc/showdown.js" />
<link rel="stylesheet" type="text/css" lift:Global.href="/inc/style.css" />
...
Jasper