Erubis Users' Guide
4 Multi-Language Support
Erubis supports the following languages(*2):
- (*2)
- If you need template engine in pure PHP/Perl/JavaScript, try Tenjin (http://www.kuwata-lab.com/tenjin/). Tenjin is a very fast and full-featured template engine implemented in pure PHP/Perl/JavaScript.
4-1 PHP
example.ephp
<?xml version="1.0"?>
<html>
<body>
<p>Hello <%= $user %>!</p>
<table>
<tbody>
<% $i = 0; %>
<% foreach ($list as $item) { %>
<% $i++; %>
<tr bgcolor="<%= $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= $i %></td>
<td><%== $item %></td>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>
compiled source code
$ erubis -l php example.ephp
<<?php ?>?xml version="1.0"?>
<html>
<body>
<p>Hello <?php echo $user; ?>!</p>
<table>
<tbody>
<?php $i = 0; ?>
<?php foreach ($list as $item) { ?>
<?php $i++; ?>
<tr bgcolor="<?php echo $i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?>">
<td><?php echo $i; ?></td>
<td><?php echo htmlspecialchars($item); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
4-2 C
example.ec
<%
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
%>
<html>
<body>
<p>Hello <%= "%s", argv[0] %>!</p>
<table>
<tbody>
<% for (i = 1; i < argc; i++) { %>
<tr bgcolor="<%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %>">
<td><%= "%d", i %></td>
<td><%= "%s", argv[i] %></td>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>
<%
return 0;
}
%>
compiled source code
$ erubis -l c example.ec
#line 1 "example.ec"
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
fputs("<html>\n"
" <body>\n"
" <p>Hello ", stdout); fprintf(stdout, "%s", argv[0]); fputs("!</p>\n"
" <table>\n"
" <tbody>\n", stdout);
for (i = 1; i < argc; i++) {
fputs(" <tr bgcolor=\"", stdout); fprintf(stdout, i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); fputs("\">\n"
" <td>", stdout); fprintf(stdout, "%d", i); fputs("</td>\n"
" <td>", stdout); fprintf(stdout, "%s", argv[i]); fputs("</td>\n"
" </tr>\n", stdout);
}
fputs(" </tbody>\n"
" </table>\n"
" </body>\n"
"</html>\n", stdout);
return 0;
}
4-3 Java
Example.ejava
<%
import java.util.*;
public class Example {
private String user;
private String[] list;
public example(String user, String[] list) {
this.user = user;
this.list = list;
}
public String view() {
StringBuffer _buf = new StringBuffer();
%>
<html>
<body>
<p>Hello <%= user %>!</p>
<table>
<tbody>
<% for (int i = 0; i < list.length; i++) { %>
<tr bgcolor="<%= i % 2 == 0 ? "#FFCCCC" : "#CCCCFF" %>">
<td><%= i + 1 %></td>
<td><%== list[i] %></td>
</tr>
<% } %>
</tbody>
</table>
<body>
</html>
<%
return _buf.toString();
}
public static void main(String[] args) {
String[] list = { "<aaa>", "b&b", "\"ccc\"" };
Example ex = Example.new("Erubis", list);
System.out.print(ex.view());
}
public static String escape(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': sb.append("<"); break;
case '>': sb.append(">"); break;
case '&': sb.append("&"); break;
case '"': sb.append("""); break;
default: sb.append(ch);
}
}
return sb.toString();
}
}
%>
compiled source code
$ erubis -b -l java example.ejava
import java.util.*;
public class Example {
private String user;
private String[] list;
public example(String user, String[] list) {
this.user = user;
this.list = list;
}
public String view() {
StringBuffer _buf = new StringBuffer();
_buf.append("<html>\n"
+ " <body>\n"
+ " <p>Hello "); _buf.append(user); _buf.append("!</p>\n"
+ " <table>\n"
+ " <tbody>\n");
for (int i = 0; i < list.length; i++) {
_buf.append(" <tr bgcolor=\""); _buf.append(i % 2 == 0 ? "#FFCCCC" : "#CCCCFF"); _buf.append("\">\n"
+ " <td>"); _buf.append(i + 1); _buf.append("</td>\n"
+ " <td>"); _buf.append(escape(list[i])); _buf.append("</td>\n"
+ " </tr>\n");
}
_buf.append(" </tbody>\n"
+ " </table>\n"
+ " <body>\n"
+ "</html>\n");
return _buf.toString();
}
public static void main(String[] args) {
String[] list = { "<aaa>", "b&b", "\"ccc\"" };
Example ex = Example.new("Erubis", list);
System.out.print(ex.view());
}
public static String escape(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '<': sb.append("<"); break;
case '>': sb.append(">"); break;
case '&': sb.append("&"); break;
case '"': sb.append("""); break;
default: sb.append(ch);
}
}
return sb.toString();
}
}
4-4 Scheme
example.escheme
<html>
<body>
<%
(let ((user "Erubis")
(items '("<aaa>" "b&b" "\"ccc\""))
(i 0))
%>
<p>Hello <%= user %>!</p>
<table>
<%
(for-each
(lambda (item)
(set! i (+ i 1))
%>
<tr bgcolor="<%= (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF") %>">
<td><%= i %></td>
<td><%= item %></td>
</tr>
<%
) ; lambda end
items) ; for-each end
%>
</table>
<%
) ; let end
%>
</body>
</html>
compiled source code
$ erubis -l scheme example.escheme
(let ((_buf '())) (define (_add x) (set! _buf (cons x _buf))) (_add "<html>
<body>\n")
(let ((user "Erubis")
(items '("<aaa>" "b&b" "\"ccc\""))
(i 0))
(_add " <p>Hello ")(_add user)(_add "!</p>
<table>\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(_add " <tr bgcolor=\"")(_add (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(_add "\">
<td>")(_add i)(_add "</td>
<td>")(_add item)(_add "</td>
</tr>\n")
) ; lambda end
items) ; for-each end
(_add " </table>\n")
) ; let end
(_add " </body>
</html>\n")
(reverse _buf))
compiled source code (with
--func=display property)$ erubis -l scheme --func=display example.escheme
(display "<html>
<body>\n")
(let ((user "Erubis")
(items '("<aaa>" "b&b" "\"ccc\""))
(i 0))
(display " <p>Hello ")(display user)(display "!</p>
<table>\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(display " <tr bgcolor=\"")(display (if (= (modulo i 2) 0) "#FFCCCC" "#CCCCFF"))(display "\">
<td>")(display i)(display "</td>
<td>")(display item)(display "</td>
</tr>\n")
) ; lambda end
items) ; for-each end
(display " </table>\n")
) ; let end
(display " </body>
</html>\n")
4-5 Perl
example.eprl
<%
my $user = 'Erubis';
my @list = ('<aaa>', 'b&b', '"ccc"');
%>
<html>
<body>
<p>Hello <%= $user %>!</p>
<table>
<% $i = 0; %>
<% for $item (@list) { %>
<tr bgcolor=<%= ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= $i %></td>
<td><%= $item %></td>
</tr>
<% } %>
</table>
</body>
</html>
compiled source code
$ erubis -l perl example.eperl
use HTML::Entities;
my $user = 'Erubis';
my @list = ('<aaa>', 'b&b', '"ccc"');
print('<html>
<body>
<p>Hello '); print($user); print('!</p>
<table>
'); $i = 0;
for $item (@list) {
print(' <tr bgcolor='); print(++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); print('">
<td>'); print($i); print('</td>
<td>'); print($item); print('</td>
</tr>
'); }
print(' </table>
</body>
</html>
');
4-6 JavaScript
example.ejs
<%
var user = 'Erubis';
var list = ['<aaa>', 'b&b', '"ccc"'];
%>
<html>
<body>
<p>Hello <%= user %>!</p>
<table>
<tbody>
<% var i; %>
<% for (i = 0; i < list.length; i++) { %>
<tr bgcolor="<%= i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
<td><%= i + 1 %></td>
<td><%= list[i] %></td>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>
compiled source code
$ erubis -l js example.ejs
var _buf = [];
var user = 'Erubis';
var list = ['<aaa>', 'b&b', '"ccc"'];
_buf.push("<html>\n\
<body>\n\
<p>Hello "); _buf.push(user); _buf.push("!</p>\n\
<table>\n\
<tbody>\n");
var i;
for (i = 0; i < list.length; i++) {
_buf.push(" <tr bgcolor=\""); _buf.push(i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); _buf.push("\">\n\
<td>"); _buf.push(i + 1); _buf.push("</td>\n\
<td>"); _buf.push(list[i]); _buf.push("</td>\n\
</tr>\n");
}
_buf.push(" </tbody>\n\
</table>\n\
</body>\n\
</html>\n");
document.write(_buf.join(""));
If command-line option '--docwrite=false' is specified,
'_buf.join("");' is used instead of 'document.write(_buf.join(""));'.
This is useful when passing converted source code to eval() function in JavaScript.
You can pass :docwrite=>false to Erubis::Ejavascript.new() in your Ruby script.
s = File.read('example.jshtml')
engine = Erubis::Ejavascript.new(s, :docwrite=>false)
If you want to specify any JavaScript code, use '--postamble=...'.
Notice that default value of 'docwrite' property will be false in the future release.