Liquescent is an implementation of the Liquid templating language for the Scala programming language.
This example program shows how to create a custom tag to output an HTML unordered list, and also demonstrates a Liquid for
loop.
import java.io.PrintStream
import scala.collection.mutable
import xyz.hyperreal.liquescent._
object Example extends App {
val input =
"""
|<h2>Vaudeville Acts</h2>
|<ol>
| {% for act in acts %}
| <li>
| <h3>{{ act.name }}</h3>
| {% ul act.members %}
| </li>
| {% endfor %}
|</ol>
""".trim.stripMargin
val acts =
List(
Map(
"name" -> "Three Stooges",
"members" -> List( "Larry", "Moe", "Curly" )
),
Map(
"name" -> "Andrews Sisters",
"members" -> List( "LaVerne", "Maxine", "Patty" )
),
Map(
"name" -> "Abbott and Costello",
"members" -> List( "William (Bud) Abbott", "Lou Costello" )
)
)
val customtag =
new Tag( "ul" ) {
def apply( settings: Map[Symbol, Any], vars: mutable.Map[String, Any], out: PrintStream, args: List[Any], context: AnyRef ) = {
val list = args.head.asInstanceOf[List[String]]
out.print(s"<ul>${list map (item => s"<li>$item</li>") mkString}</ul>")
}
}
new Interpreter( StandardFilters.map, Tag(customtag), Map(), Map("acts" -> acts), null ).
render( LiquescentParser.parse(io.Source.fromString(input)), Map(), Console.out, false )
}
This program prints
<h2>Vaudeville Acts</h2>
<ol>
<li>
<h3>Three Stooges</h3>
<ul><li>Larry</li><li>Moe</li><li>Curly</li></ul>
</li>
<li>
<h3>Andrews Sisters</h3>
<ul><li>LaVerne</li><li>Maxine</li><li>Patty</li></ul>
</li>
<li>
<h3>Abbott and Costello</h3>
<ul><li>William (Bud) Abbott</li><li>Lou Costello</li></ul>
</li>
</ol>
This next example shows how to use Liquescent as an executable on the command line.
echo "{{ v | join: \", \" }}" | java -jar liquescent-0.3.jar -j "{v: [\"one\", \"two\", \"three\"]}" --
The above command prints
one, two, three
Use the following definition to use Liquescent in your Maven project:
<repository>
<id>hyperreal</id>
<url>https://dl.bintray.com/edadma/maven</url>
</repository>
<dependency>
<groupId>xyz.hyperreal</groupId>
<artifactId>liquescent</artifactId>
<version>0.3</version>
</dependency>
Add the following to your build.sbt
file to use Liquescent in your SBT project:
resolvers += "Hyperreal Repository" at "https://dl.bintray.com/edadma/maven"
libraryDependencies += "xyz.hyperreal" %% "liquescent" % "0.3"
- Java 11+
- SBT 1.2.8+
- Scala 2.13.0+
git clone git://github.com/edadma/liquescent.git
cd liquescent
sbt assembly
The command sbt assembly
also runs all the unit tests.
ISC © 2019 Edward Maxedon