scala erb equivalent?

View: New views
7 Messages — Rating Filter:   Alert me  

scala erb equivalent?

by Maurice Codik-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Hey all,

I'm looking for a library similar to Ruby's ERB for scala: something that I can use to embed scala code in ordinary text, and then have that translated into a scala class I can call into. I know I can get close to this by using { expr } in xml tags, but I'd like something I can use outside of xml documents if I need to.

Does something like this exist already? My google-fu is failing me..

Thanks!

Maurice



Re: scala erb equivalent?

by bearfeeder :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Short answer: nothing but the built-in XML support.

Why is this failing you?

On 1/9/08, Maurice Codik <mcodik@...> wrote:
Hey all,

I'm looking for a library similar to Ruby's ERB for scala: something that I can use to embed scala code in ordinary text, and then have that translated into a scala class I can call into. I know I can get close to this by using { expr } in xml tags, but I'd like something I can use outside of xml documents if I need to.

Does something like this exist already? My google-fu is failing me..

Thanks!

Maurice





--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us

Re: scala erb equivalent?

by Jim Miller-12 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

I use the following function that goes through an imput string and calls a function for every occurrence of #{name}, passing 'name' as the argument to the function.

import java.util.regex.{Pattern,Matcher}

def populate (template: String, map : (String) => String ) : String = {
 
  val p = Pattern.compile("#\\{(.+?)\\}");
  val m = p.matcher(template);
  val sb = new StringBuffer();

  while (m.find()) {
    m.appendReplacement(sb, map( m.group(1) ) );
  }

  m.appendTail(sb);

  return sb.toString()
}

Then in practice I do something like:

    populate(
      """XDR.writeInt32(d, #{name}.size() );
          for ( #{type} v : #{name} ) {  XDR.write( d, v ); }
      """,
      tag => tag match {
         case "name" => "foo"
        case "type" => "bar"
      })

I do a LOT of code generation with Scala and this function works out very well.  The nice thing is that I can do anything I want in the case clauses as long as the result is a string for substituting in the template.

On 1/9/08, Maurice Codik <mcodik@...> wrote:
Hey all,

I'm looking for a library similar to Ruby's ERB for scala: something that I can use to embed scala code in ordinary text, and then have that translated into a scala class I can call into. I know I can get close to this by using { expr } in xml tags, but I'd like something I can use outside of xml documents if I need to.

Does something like this exist already? My google-fu is failing me..

Thanks!

Maurice




Re: scala erb equivalent?

by Maurice Codik-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

A few reasons:

- not all documents are xml.

- I've run into a few issues with the built in xml support that make it annoying for use as a templating language for xml. for example, with simple conditionals:

object xml {
  def main(a:Array[String]) {                                                                                                                                                          
    val data = <testing>
                {
                  if (true) {
                    <huh>huh?!</huh>
                  }
               }
               </testing>                                                                                                                  
    Console.println(data)                                                                                                                   
  }
}

only prints: <testing></testing>. I can work around this by adding a dummy else block to that if, but that can get tedious.

there are also some slightly annoying things, like how it turns self closing tags to normal tags: <img src="test.jpg" /> => <img ..></img>


On Jan 9, 2008 9:57 AM, David Pollak < feeder.of.the.bears@...> wrote:
Short answer: nothing but the built-in XML support.

Why is this failing you?


On 1/9/08, Maurice Codik <mcodik@...> wrote:
Hey all,

I'm looking for a library similar to Ruby's ERB for scala: something that I can use to embed scala code in ordinary text, and then have that translated into a scala class I can call into. I know I can get close to this by using { expr } in xml tags, but I'd like something I can use outside of xml documents if I need to.

Does something like this exist already? My google-fu is failing me..

Thanks!

Maurice





--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us


Re: scala erb equivalent?

by David Pollak :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Maurice,

Maurice Codik wrote:
A few reasons:

- not all documents are xml.

- I've run into a few issues with the built in xml support that make it annoying for use as a templating language for xml. for example, with simple conditionals:

object xml {
  def main(a:Array[String]) {                                                                                                                                                          
    val data = <testing>
                {
                  if (true) {
                    <huh>huh?!</huh>
                  }
               }
               </testing>                                                                                                                  
    Console.println(data)                                                                                                                   
  }
}

only prints: <testing></testing>. I can work around this by adding a dummy else block to that if, but that can get tedious.
First, this is good practice for remembering that everything in Scala returns a value, but sometimes the value is of type Unit.

Second, you could do something like:

def showIf(test: Boolean)(toShow: => NodeSeq) = if (test) toShow else Text("")

Then you could write showIf(true){<huh>huh?!</huh>}


there are also some slightly annoying things, like how it turns self closing tags to normal tags: <img src="test.jpg" /> => <img ..></img>

There's an XML to String to UTF-8 encoded byte array facility in lift that "does the right thing"(tm) for XML -> XHTMLish stuff that even IE 6 is comfortable with.


On Jan 9, 2008 9:57 AM, David Pollak < feeder.of.the.bears@...> wrote:
Short answer: nothing but the built-in XML support.

Why is this failing you?


On 1/9/08, Maurice Codik <mcodik@...> wrote:
Hey all,

I'm looking for a library similar to Ruby's ERB for scala: something that I can use to embed scala code in ordinary text, and then have that translated into a scala class I can call into. I know I can get close to this by using { expr } in xml tags, but I'd like something I can use outside of xml documents if I need to.

Does something like this exist already? My google-fu is failing me..

Thanks!

Maurice





--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us


Re: scala erb equivalent?

by David Bernard-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Hi,

Scala can use all the templates lib available in the javaworld like velocity, freemarker, stringtemplate,...
Or simple like the code provide by Jim cf Substitutor from commons-lang.

/davidB

Jim Miller wrote:

> I use the following function that goes through an imput string and calls
> a function for every occurrence of #{name}, passing 'name' as the
> argument to the function.
>
> import java.util.regex.{Pattern,Matcher}
>
> def populate (template: String, map : (String) => String ) : String = {
>  
>   val p = Pattern.compile("#\\{(.+?)\\}");
>   val m = p.matcher(template);
>   val sb = new StringBuffer();
>
>   while (m.find()) {
>     m.appendReplacement(sb, map( m.group(1) ) );
>   }
>
>   m.appendTail(sb);
>
>   return sb.toString()
> }
>
> Then in practice I do something like:
>
>     populate(
>       """XDR.writeInt32(d, #{name}.size() );
>           for ( #{type} v : #{name} ) {  XDR.write( d, v ); }
>       """,
>       tag => tag match {
>          case "name" => "foo"
>         case "type" => "bar"
>       })
>
> I do a LOT of code generation with Scala and this function works out
> very well.  The nice thing is that I can do anything I want in the case
> clauses as long as the result is a string for substituting in the template.
>
> On 1/9/08, *Maurice Codik* <mcodik@... <mailto:mcodik@...>>
> wrote:
>
>     Hey all,
>
>     I'm looking for a library similar to Ruby's ERB for scala: something
>     that I can use to embed scala code in ordinary text, and then have
>     that translated into a scala class I can call into. I know I can get
>     close to this by using { expr } in xml tags, but I'd like something
>     I can use outside of xml documents if I need to.
>
>     Does something like this exist already? My google-fu is failing me..
>
>     Thanks!
>
>     Maurice
>
>
>


Re: scala erb equivalent?

by Maurice Codik-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message


Second, you could do something like:

def showIf(test: Boolean)(toShow: => NodeSeq) = if (test) toShow else Text("")


very neat! I still dont know the language enough to realize things like that are doable. thanks.

I think Jim's solution is pretty cool too, and generally useful for simple string interpolation.. maybe something like that would be good to add to scalax/scalaz ?

davidB brings up a good point-- I hadn't really looked at many of the templating solutions from Java yet.. I'll take a look at those before trying to roll my own erb-clone (which I might do anyway for fun).

thanks for all the replies!