Looking for HTML templating system

I am looking for a templating system for my forum
(http://rforum.rubyforge.org). I would prefer something lightweight like
PageTemplate over heavy XML stuff like Amrita, because the forum will
have to handle quite a lot of traffic and my server doesn’t have too
much memory. But the reason why I can’t use most of the templating
engines is that I want to make the forum multi-language, so that I can’t
write:

[%if logged_in%]
Logged in as [%var username%]
[%endif%]

but would have to write something like

[%if logged_in%]
[%var text_logged_in_as(username)%]
[%endif%]

Does anyone know a templating system I could use?

Thanks
Andreas

I am looking for a templating system for my forum
(http://rforum.rubyforge.org). … I can’t use most of the templating
engines is that I want to make the forum multi-language, so that I can’t
write:
[%if logged_in%]
Logged in as [%var username%]
[%endif%]
but would have to write something like
[%if logged_in%]
[%var text_logged_in_as(username)%]
[%endif%]
Does anyone know a templating system I could use?

I’m just about to do the same thing (mutli-language), and here’s the solution
I came up with (so far)…

If your output is going to be multi-language and filled with variables, then
your HTML template is going to be way more template than HTML. Like your
example above.

At that point, it seems smarter to shift your focus from “HTML templates to
put my Ruby in” to “Ruby class that puts out HTML”.

QUICK UNTESTED EXAMPLE:

HTML_Template.rb

class HTML_Template
def initialize(lang)
@lang=lang
end
def header(pagetitle)
‘%s - %s’ % [@lang[‘sitename’],
pagetitle]
end
def loginbox_loggedin(username)

%s %s
’ % [@lang[‘logged_in_as’], username]
end
def loginbox_anon
%s
’ % [@lang[‘please_log_in’]]
end
# … etc
# one method for each often-used “chunk” of your website
def footer
‘’
end
end

Lang.rb

english = {
‘sitename’ => ‘RForum’,
‘logged_in_as’ => ‘Logged in as’,
‘please_log_in’ => ‘Please log in’

etc

}

welcome.rbx

load ‘Lang.rb’
require ‘HTML_Template’
ht = HTML_Template.new(english)
puts ht.header
if username
puts ht.loginbox_loggedin(username)
else
puts ht.loginbox_anon
end
puts ht.footer

I might be totally wrong with this. I haven’t started my new project yet
because I have to finish something else first. But it’s just an idea, and
I’d love to hear any other suggestions.

I am looking for a templating system for my forum
(http://rforum.rubyforge.org). I would prefer something lightweight
like PageTemplate over heavy XML stuff like Amrita, because the
forum will have to handle quite a lot of traffic and my server
doesn’t have too much memory. But the reason why I can’t use most of
the templating engines is that I want to make the forum
multi-language, so that I can’t write:

[%if logged_in%]
Logged in as [%var username%]
[%endif%]

but would have to write something like

[%if logged_in%]
[%var text_logged_in_as(username)%]
[%endif%]

Does anyone know a templating system I could use?

Iowa might work.

Thanks, I’ll have a look at it.

How much traffic do you get, and what is your server’s mem and CPU,
out of curiosity?

At the moment I am using an extremely simple self-written PHP
forum which gets about 500-600k hits per month
(www.mikrocontroller.net/forum/). The typical loading time is < 0.2s,
and I dont’t want it to increase. The server is a virtual(UML) server
from www.vd-server.de with 96MB RAM.

In HTML file:

@text_logged_in_as

In code file:

def text_logged_in_as

No idea how you are handling the multiple languages.

The assumption here is that language choice is either stored

in a cookie or derived from the HTTP_ACCEPT_LANG paramter,

and that whichever way it’s determined, we put the choice in a

variable, @lang. Also assuming, for this example, that there’s

a big hash that contains the text items.

@site_text[@lang][‘logged_in_as’]
end

I don’t want to create a method for every string, and there is another
problem: the strings have to take “arguments” (the username, in this
case). I could use ERuby with a method like this:

def gettext(string, *args)
@text[@lang] % args
end

where @text[@lang] is “Logged in as %s”. But it looks a bit ugly to
have <%=gettext(‘LOGGED_IN_AS’, @username)%> everywhere in the file…

Another idea I just got: writing TEXT_LOGGED_IN_AS(username) in the
template file and using a script to generate the different language
versions of the template.


Another way would be to have more than one HTML file; one for each
language.

I don’t want to do that because it would mean writing a lot of
redundant code.

This is how I’d probably do it, personally. If you go to
http://enigo.com/demos/multi_lang/index.html
there is a very quick, simple demo of this. I’d been planning to do
one, and your query gave me the impetus to do it. If you look at that
page with your langauge preference set to English, you’ll see the
content in English. If you switch your preference to Spanish, you’ll
see it in Spanish.

I think I could use the accept-language setting to set the
language cookie and also add some small form field to allow the user to
change the language, because many browsers aren’t configured correctly.

Andreas

···

Kirk Haines khaines@enigo.com wrote:

On Mon, 8 Mar 2004, Andreas Schwarz wrote:

Hello,

I have also chosen to write a forum in Ruby, and the solution I came up with
for i18n is that the templates should not include program-determined
user-visible strings. What that basically means is that the task of
translating the message isn’t in the template files but in the program.

For example, using an Amrita-like system, I would do something like

exp_data = { :login_string => i18n("Login") }
# Expand <span id="login_string"/> or somesuch.

where i18n is just a wrapper for gettext from the Ruby GetText library.

This eases the job of reproducing similar messages for template writers, since
user-visible strings are now kept in the program.

As for different languages via preferences, I originally thought of doing this
but a friend has convinced me that it’s pointless to have a forum run on more
than one language at one time. A forum run in Germany will have all its posts
in German anyhow, what’s the point of having the menus in English (and
vice-versa)?

···


Lo-lee-ta: the tip of the tongue taking a trip of three
steps down the palate to tap, at three, on the teeth.
Lo. Lee. Ta. GUO Shu-yu shu@rufuran.org

I am looking for a templating system for my forum

(http://rforum.rubyforge.org). I would prefer something lightweight like

PageTemplate over heavy XML stuff like Amrita, because the forum will

have to handle quite a lot of traffic and my server doesn’t have too

much memory. But the reason why I can’t use most of the templating

engines is that I want to make the forum multi-language, so that I can’t

write:

···

On Mon, 8 Mar 2004, Andreas Schwarz wrote:

[%if logged_in%]

Logged in as [%var username%]

[%endif%]

but would have to write something like

[%if logged_in%]

[%var text_logged_in_as(username)%]

[%endif%]

Does anyone know a templating system I could use?

I’m not sure if anyone mentioned this previously, but the templating
system included with rdoc (which is included as part of the standard ruby
distribution these days) is very simple and would probably meet your
needs. You would use it from your code like this (You can’t do a
method call in the template, so you would want to do the language-specific
stuff in code and just use the template for presentation):

—begin-code-snippet—
require ‘rdoc/template’

alternatively, you could load the template text from files…

just inlining it to simplify the example

TEMPLATE = <<- DOC
IF:logged_in
%username%
ENDIF:logged_in
DOC
values = { “username” => text_logged_in_as(username), logged_in => true }

t = TemplatePage.new(TEMPLATE)
res = ''
t.write_html_on(res, values)
browser_output_stream << res
—end-code-snippet—

rdoc/template.rb provides conditionals and loops. It’s very easy to use,
and with a recent ruby version, requires no additional installation.

Chad

Ruby Baby wrote:

I am looking for a templating system for my forum
(http://rforum.rubyforge.org). … I can’t use most of the templating
engines is that I want to make the forum multi-language, so that I can’t
write:
[%if logged_in%]
Logged in as [%var username%]
[%endif%]
but would have to write something like
[%if logged_in%]
[%var text_logged_in_as(username)%]
[%endif%]
Does anyone know a templating system I could use?

I’m just about to do the same thing (mutli-language), and here’s the solution
I came up with (so far)…

If your output is going to be multi-language and filled with variables, then
your HTML template is going to be way more template than HTML. Like your
example above.

At that point, it seems smarter to shift your focus from “HTML templates to
put my Ruby in” to “Ruby class that puts out HTML”.

The problem is that the methods generating html could end up containing
more and more of the functional core of the application, so that they
aren’t easily exchangeable. But I will think about it again…

QUICK UNTESTED EXAMPLE:

HTML_Template.rb

class HTML_Template
def initialize(lang)
@lang=lang
end
def header(pagetitle)
‘%s - %s’ % [@lang[‘sitename’],
pagetitle]
end
def loginbox_loggedin(username)

%s %s
’ % [@lang[‘logged_in_as’], username]
end

It seems to be far more comfortable to keep things like pagetitle,
username etc. in attributes instead of local variables. It might seem
like a “dirty” coding style, but as these methods get always called with
exactly the same arguments it seems acceptable IMO (what do the Ruby
gurus think?).

Shu-yu Guo wrote:

I have also chosen to write a forum in Ruby, and the solution I came up with
for i18n is that the templates should not include program-determined
user-visible strings. What that basically means is that the task of
translating the message isn’t in the template files but in the program.

Yes, of course.

For example, using an Amrita-like system, I would do something like

exp_data = { :login_string => i18n(“Login”) }

Expand or somesuch.

where i18n is just a wrapper for gettext from the Ruby GetText library.

I didn’t take a closer look at gettext, what are the main advantages
over a simple hash-based system?

As for different languages via preferences, I originally thought of doing this
but a friend has convinced me that it’s pointless to have a forum run on more
than one language at one time.
A forum run in Germany will have all its posts in German anyhow,
what’s the point of having the menus in English (and
vice-versa)?

I originally intended to hard-code the language setting in the config
file, but as I plan to add an english section to my forum I thought it
could be useful to make it selectable for the user.

I agree with Andreas here. Templates are templates, there should be no logic
in them; they are only presentation. You shouldn’t ask your template writers
to learn your programming language just to change how your software looks. If
your templates become your code, there is no point in having the templates,
just go ahead and embed HTML.

While it is a given that your templates will grow complex, they do not
necessarily have to have code in them. I think a system like Amrita
demonstrates that well.

···

On Mon, Mar 08, 2004 at 07:19:40AM +0900, Andreas Schwarz wrote:

At that point, it seems smarter to shift your focus from “HTML templates to
put my Ruby in” to “Ruby class that puts out HTML”.

The problem is that the methods generating html could end up containing
more and more of the functional core of the application, so that they
aren’t easily exchangeable. But I will think about it again…


Lo-lee-ta: the tip of the tongue taking a trip of three
steps down the palate to tap, at three, on the teeth.
Lo. Lee. Ta. GUO Shu-yu shu@rufuran.org

I didn’t take a closer look at gettext, what are the main advantages
over a simple hash-based system?

The translation files (po’s and mo’s) are in the gettext format, which means
you can use existing tools to edit and create them. This would make it easiers
for the translators, who probably are already familiar with GNU gettext.

I originally intended to hard-code the language setting in the config
file, but as I plan to add an english section to my forum I thought it
could be useful to make it selectable for the user.

Looks like our original intentions were reversed :slight_smile: If you’re going to have a
separate section then I suppose you do need to make it selectable.

···

On Mon, Mar 08, 2004 at 07:39:40AM +0900, Andreas Schwarz wrote:


Lo-lee-ta: the tip of the tongue taking a trip of three
steps down the palate to tap, at three, on the teeth.
Lo. Lee. Ta. GUO Shu-yu shu@rufuran.org

Shu-yu Guo wrote:

At that point, it seems smarter to shift your focus from “HTML templates to
put my Ruby in” to “Ruby class that puts out HTML”.

The problem is that the methods generating html could end up containing
more and more of the functional core of the application, so that they
aren’t easily exchangeable. But I will think about it again…

I agree with Andreas here. Templates are templates, there should be no logic
in them; they are only presentation. You shouldn’t ask your template writers
to learn your programming language just to change how your software looks. If
your templates become your code, there is no point in having the templates,
just go ahead and embed HTML.
^^^^^^^^^^
I did that in the last forum i wrote, and I hate it :slight_smile:

While it is a given that your templates will grow complex, they do not
necessarily have to have code in them.

Most template systems have “if” constructs, iterators etc…

I think a system like Amrita demonstrates that well.

I won’t use Amrita because it seems to be a real memory and cpu hog
(haven’t tried the latest version though), and I really don’t see the
point in parsing the whole template with rexml just to exchange a few
strings. For big complex sites and with caching, maybe - but not for a
forum that is intended to have speed as its main (but I hope not only)
feature.

···

On Mon, Mar 08, 2004 at 07:19:40AM +0900, Andreas Schwarz wrote:

Shu-yu Guo wrote:

I didn’t take a closer look at gettext, what are the main advantages
over a simple hash-based system?

The translation files (po’s and mo’s) are in the gettext format, which means
you can use existing tools to edit and create them. This would make it easiers
for the translators, who probably are already familiar with GNU gettext.

I’m not convinced that gettext has many advantages in a web application,
and the main disadvantage is that people need special tools to make even
small changes.

I originally intended to hard-code the language setting in the config
file, but as I plan to add an english section to my forum I thought it
could be useful to make it selectable for the user.

Looks like our original intentions were reversed :slight_smile: If you’re going to have a
separate section then I suppose you do need to make it selectable.

At the moment I am still at a very early stage of the project, and I’m
not too sure about what will and what will not be included in the result
:wink:

···

On Mon, Mar 08, 2004 at 07:39:40AM +0900, Andreas Schwarz wrote:

it seems smarter to shift your focus from “HTML templates to
put my Ruby in” to “Ruby class that puts out HTML”.

Templates are templates, there should be no logic
in them; they are only presentation.

Presentation DEFINITELY needs logic, for anything beyond the
stupidest “put a variable on a page” website.

The differences is that a templating system should only have
PRESENTATION LOGIC, but no business-logic.

The presentation having logic is a big part of making an
easy-to-use responsive website!

For example, my site is http://www.cdbaby.com

The presentation/display is constantly changing based on the
contents. It is deciding how it wants to present the business
information given to it.

If an item is on sale, the designer has decided to put a big
yellow flag in the top-right corner. If it is one hour until
we close, the designer has decided to change the color of the
page to let the customer know we’re almost closing, and they
should hurry. The designer has decided to capitalize the
artist names, and lowercase the album titles.

All of this is presentation logic.

And that stuff really should be up to the graphic designer

  • not the business programmer.

Then considering that the user can change their languages or
currency while shopping, I guess that’s why I’m leaning towards
a Ruby class that outputs the HTML tags around the info,
instead of a flat HTML page that is filled with tons of logic
tags and variables for everything but the HTML markup itself.

I guess I’d rather have a graphic designer take a day to learn
some basic if/then logic, than to design a permanently-ugly
system because we are assuming the graphic designer is an
unteachable idiot.

I did that in the last forum i wrote, and I hate it :slight_smile:

Yeah, heh, I would hate it too. But you get the hyperbole.

Most template systems have “if” constructs, iterators etc…

Well, that doesn’t mean you have to follow the crowd. PHP boards are quite
poorly written. Thinking of VBulletin’s templates gives me the shivers.

I won’t use Amrita because it seems to be a real memory and cpu hog
(haven’t tried the latest version though), and I really don’t see the
point in parsing the whole template with rexml just to exchange a few
strings. For big complex sites and with caching, maybe - but not for a
forum that is intended to have speed as its main (but I hope not only)
feature.

Well, I agree Amrita’s quite slow. In fact, for my forum I rewrote an
Amrita-like system using xmlparser instead of REXML. In any case, it’s faster.
That aside, I think a majority of the time will be spent on SQL queries. As
for caching, I don’t really know how that will work in something like a forum,
where the data could be updated frequently (I assume you’re expecting a lot of
users, if you’re making speed a top goal).

Good luck with your project. :slight_smile:

···

On Mon, Mar 08, 2004 at 08:29:41AM +0900, Andreas Schwarz wrote:


Lo-lee-ta: the tip of the tongue taking a trip of three
steps down the palate to tap, at three, on the teeth.
Lo. Lee. Ta. GUO Shu-yu shu@rufuran.org

I won’t use Amrita because it seems to be a real memory and cpu hog
(haven’t tried the latest version though), and I really don’t see the
point in parsing the whole template with rexml just to exchange a few
strings. For big complex sites and with caching, maybe - but not for a
forum that is intended to have speed as its main (but I hope not only)
feature.

Have you tried Amrita with the bytecode compiler enabled?

Ari

If you need a fast temlate system, try Kwartz.
http://www.kuwata-lab.com/webtech/kwartz/
Kwartz is a template system for Ruby, PHP and Java.
And it runs very fast.

If you are interested in presentation logic, you also should try Kwartz.
Kwartz can separate presentation logic from HTML template.
It is a significant feature of Kwartz.
See
http://www.kuwata-lab.com/webtech/kwartz/kwartz-overview/index.php?page4

There are some merits if you separate presentation logic from HTML template:

  • you never break HTML design
  • you don’t have to merge presentation logic into main program.
···

Ruby Baby ruby@hitmedia.com wrote:

Presentation DEFINITELY needs logic, for anything beyond the
stupidest “put a variable on a page” website.

The differences is that a templating system should only have
PRESENTATION LOGIC, but no business-logic.

I think so. It is just same as what I have thought.


regards
kwa

it seems smarter to shift your focus from “HTML templates to
put my Ruby in” to “Ruby class that puts out HTML”.

Templates are templates, there should be no logic
in them; they are only presentation.

Presentation DEFINITELY needs logic, for anything beyond the
stupidest “put a variable on a page” website.

The differences is that a templating system should only have
PRESENTATION LOGIC, but no business-logic.

The presentation having logic is a big part of making an
easy-to-use responsive website!

For example, my site is http://www.cdbaby.com

The presentation/display is constantly changing based on the
contents. It is deciding how it wants to present the business
information given to it.

If an item is on sale, the designer has decided to put a big
yellow flag in the top-right corner. If it is one hour until
we close, the designer has decided to change the color of the
page to let the customer know we’re almost closing, and they
should hurry. The designer has decided to capitalize the
artist names, and lowercase the album titles.

All of this is presentation logic.

And that stuff really should be up to the graphic designer

  • not the business programmer.

Then considering that the user can change their languages or
currency while shopping, I guess that’s why I’m leaning towards
a Ruby class that outputs the HTML tags around the info,
instead of a flat HTML page that is filled with tons of logic
tags and variables for everything but the HTML markup itself.

I guess I’d rather have a graphic designer take a day to learn
some basic if/then logic, than to design a permanently-ugly
system because we are assuming the graphic designer is an
unteachable idiot.

Kwartz! That’s the one that I was trying to think of for my mini-list of
templating systems/web frameworks yesterday. IMHO, Kwartz looks like it’d
be great for things like generating large volumes of customized email.
It’s on my todo list to learn about it in more depth with this in mind for
use with my clients who offer email products or who use email to stay in
touch with their customers.

Kirk Haines

···

On Tue, 9 Mar 2004, Makoto Kuwata wrote:

If you need a fast temlate system, try Kwartz.
http://www.kuwata-lab.com/webtech/kwartz/
Kwartz is a template system for Ruby, PHP and Java.
And it runs very fast.

Aredridel wrote:

I won’t use Amrita because it seems to be a real memory and cpu hog
(haven’t tried the latest version though), and I really don’t see the
point in parsing the whole template with rexml just to exchange a few
strings. For big complex sites and with caching, maybe - but not for a
forum that is intended to have speed as its main (but I hope not only)
feature.

Have you tried Amrita with the bytecode compiler enabled?

No, not yet; but what is this bytecode compiler good for, wouldn’t it be
much easier to compile the template into ruby code? Is that possible?

Andreas Schwarz wrote:

Aredridel wrote:

I won’t use Amrita because it seems to be a real memory and cpu hog
(haven’t tried the latest version though), and I really don’t see the
point in parsing the whole template with rexml just to exchange a few
strings. For big complex sites and with caching, maybe - but not for a
forum that is intended to have speed as its main (but I hope not only)
feature.

Have you tried Amrita with the bytecode compiler enabled?

No, not yet; but what is this bytecode compiler good for, wouldn’t it be
much easier to compile the template into ruby code? Is that possible?

I tried use_compiler = true, but my simple example (table with 30 rows)
is still too slow (100/s). I will try the latest development version,
maybe it is a bit faster…

Andreas Schwarz wrote:

Andreas Schwarz wrote:

Aredridel wrote:

I won’t use Amrita because it seems to be a real memory and cpu hog
(haven’t tried the latest version though), and I really don’t see the
point in parsing the whole template with rexml just to exchange a few
strings. For big complex sites and with caching, maybe - but not for a
forum that is intended to have speed as its main (but I hope not only)
feature.

Have you tried Amrita with the bytecode compiler enabled?

No, not yet; but what is this bytecode compiler good for, wouldn’t it be
much easier to compile the template into ruby code? Is that possible?

I tried use_compiler = true, but my simple example (table with 30 rows)
is still too slow (100/s). I will try the latest development version,
maybe it is a bit faster…

The new version is about 4 times faster but uses quite a lot of RAM
(8M).