Looking for HTML templating system

Discussion in 'Ruby' started by Andreas Schwarz, Mar 7, 2004.

  1. 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
     
    Andreas Schwarz, Mar 7, 2004
    #1
    1. Advertisements

  2. Andreas Schwarz

    Ruby Baby Guest

    I am looking for a templating system for my forum


    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.
     
    Ruby Baby, Mar 7, 2004
    #2
    1. Advertisements

  3. Thanks, I'll have a look at it.
    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.
    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.
    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
     
    Andreas Schwarz, Mar 7, 2004
    #3
  4. Andreas Schwarz

    Shu-yu Guo Guest

    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)?
     
    Shu-yu Guo, Mar 7, 2004
    #4
  5. 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...
    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?).
     
    Andreas Schwarz, Mar 7, 2004
    #5
  6. Andreas Schwarz

    Shu-yu Guo Guest

    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.
     
    Shu-yu Guo, Mar 7, 2004
    #6
  7. Yes, of course.
    I didn't take a closer look at gettext, what are the main advantages
    over a simple hash-based system?
    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.
     
    Andreas Schwarz, Mar 7, 2004
    #7
  8. Andreas Schwarz

    Shu-yu Guo Guest

    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.
    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.
     
    Shu-yu Guo, Mar 7, 2004
    #8
  9. ^^^^^^^^^^
    I did that in the last forum i wrote, and I hate it :)
    Most template systems have "if" constructs, iterators etc...
    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.
     
    Andreas Schwarz, Mar 7, 2004
    #9
  10. 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.
    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
    ;)
     
    Andreas Schwarz, Mar 7, 2004
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.