Looking for HTML templating system

  • Thread starter Andreas Schwarz
  • Start date
A

Andreas Schwarz

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
 
R

Ruby Baby

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)
'<html><head><title>%s - %s</title><head><body>' % [@lang['sitename'],
pagetitle]
end
def loginbox_loggedin(username)
'<div class="loginbox">%s %s</div>' % [@lang['logged_in_as'], username]
end
def loginbox_anon
'<div class="loginbox">%s</div>' % [@lang['please_log_in']]
end
# ... etc
# one method for each often-used "chunk" of your website
def footer
'</body></html>'
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.
 
A

Andreas Schwarz

Kirk Haines said:
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:

<if oid="logged_in?">
@text_logged_in_as
</if>



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.

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
 
S

Shu-yu Guo

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)?
 
A

Andreas Schwarz

Ruby said:
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)
'<html><head><title>%s - %s</title><head><body>' % [@lang['sitename'],
pagetitle]
end
def loginbox_loggedin(username)
'<div class="loginbox">%s %s</div>' % [@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?).
 
S

Shu-yu Guo

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.

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.
 
A

Andreas Schwarz

Shu-yu Guo said:
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 <span id="login_string"/> 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.
 
S

Shu-yu Guo

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 :) If you're going to have a
separate section then I suppose you do need to make it selectable.
 
A

Andreas Schwarz

Shu-yu Guo said:
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 :)
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.
 
A

Andreas Schwarz

Shu-yu Guo said:
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.
Looks like our original intentions were reversed :) 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
;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top