Skip to content

Instantly share code, notes, and snippets.

@julienrf
julienrf / fb-page-feed.js
Created November 29, 2010 11:06
A small js snippet retrieving the feed of a Facebook page and displaying its elements.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://connect.facebook.net/fr_FR/all.js"></script>
<script>
// Replace the id by any other public page id
FB.api('170913076256527/feed', { limit: 3 }, function(result) {
$('#fb-feed').empty();
$(result.data).each(function(i, post) {
entry = $('<div class="fb-item"></div>');
if (post.icon) {
entry.append('<img class="icon" src="' + post.icon + '" />');
@julienrf
julienrf / glitter-sample.scala
Created April 11, 2011 21:06
Template sample using Glitter
// Import the Glitter DSL
import glitter._
object Templates {
// Define a reusable layout
def layout(body: Xml) =
html5dtd | 'html (
'head :: 'title :: "Glitter is amazing!"
| 'body :: body
@julienrf
julienrf / bigtable.scala
Created April 12, 2011 08:30
Bigtable template in Glitter
import glitter._
object Bigtable {
def template(table: Array[Array[Int]]) =
'table (
forM (table) (row =>
'tr :: forM (row) ('td :: _.toString)
)
)
}
@julienrf
julienrf / tyco_sample.scala
Created April 20, 2011 12:11
Is it possible to write more expressive things?
import tyco.compiler._
object MyWebsite extends tyco.Site {
for (page <- Content.allPages)
page.uri ==> Template.show(page)
for (file <- Path("stylesheets/*.scss"))
"/css/"+file.name ==> new TextFile(file) with Sass with CssMinifier
}
@julienrf
julienrf / 1_Max.scala
Created April 29, 2011 10:28
Scala typeclass pattern
/** Say we have a `max` function, computing the maximum of two numbers */
def max(a: Int, b: Int): Int = {
if (a < b) b
else a
}
/** It can be used like that: */
max(3, 4)

What do we want to do?

Find a way to easily allow industrial web applications developers to write consistent user interfaces without having to repeat themselves, and to efficiently handle variability.

The user interface is displayed by the web browser from the HTML of the page. Developers write the HTML markup using a template engine. This template engine allows to include parametrized fragments, giving the ability to define some UI traits (capturing one UI concept) and to reuse them across the application.

*{ Liste éditable d’éléments }*
*{ @param _arg: Iterable[A] Liste des éléments constitutifs de la liste }*
*{ @param as: String Name of the iterator }*
*{ @param (optional) addAction: ActionDefinition URL permettant d’ajouter un élément }*
*{ @param (optional) pager: Pager }*
#{ui.list users, as: 'user', addAction: @Users.add(), pager: pager}
#{useritem user /}
#{/ui.list}
*{ Formulaire }*
@julienrf
julienrf / jQueryButtons.js
Created October 16, 2011 16:21
Don’t use jQuery to write a web application
$next.attr('disabled', 'disabled');
object Happy {
def sumSquare(ds: List[Int]): Int = ds map (d => d * d) sum
def digits(n: Int): List[Int] = if (n < 10) List(n) else (n % 10) :: digits(n / 10)
def happy(n: Int, visited: List[Int] = Nil): Boolean = sumSquare(digits(n)) match {
case 1 => true
case n => if (visited contains n) false else happy(n, n :: visited)
}
@julienrf
julienrf / 1_Secured.scala
Last active September 28, 2015 06:08
Dependency injection in Scala with Play 2: it’s free
object MyApp extends Controller {
// Home page
val index = Action {
Ok(views.html.index())
}
// Login action: here I just bind a (non empty) username from the query and put it in the session
val login = Action { implicit request =>
Form(mapping("username" -> nonEmptyText)(identity)(Some(_))).bindFromRequest.fold(
noUser => redirectToIndex,