click-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From AndreyRybin <apr...@gmail.com>
Subject Page.template (or similar) method and more seamless Velocity/Freemarker integration
Date Thu, 22 Jul 2010 08:50:23 GMT

Just copy of 
http://click.1134972.n2.nabble.com/recaptcha-works-with-click-tp5296015p5324509.html
http://click.1134972.n2.nabble.com/recaptcha-works-with-click-tp5296015p5324509.html 

=== example code:
           return "br/>To verify that you are human<br/>" +
             "<script type=\"text/javascript\"
    src=\"http://api.recaptcha.net/challenge?k=" +
             public_key +
             "\"></script><noscript><iframe
src=\"http://api.recaptcha.net/noscript?k=" +
             public_key +
             "\" height=\"300\" width=\"500\"
    frameborder=\"0\"></iframe><br></noscript><br";
===



May be add one method in Page class for this (some copy-paste from Spring):

/**
         * Process the specified template with the given model and write
         * the result to the given Writer.
         * <p>When using this method to prepare a text for a mail to be sent
with Spring's
         * mail support, consider wrapping IO/TemplateException in
MailPreparationException.
         * @param model the model object, typically a Map that contains
model names
         * as keys and model objects as values
         * @return the result as String
         * @throws IOException if the template wasn't found or couldn't be
read
         * @throws TemplateException if rendering failed
         */
public String template (String templatePath, Map<String, ?> model) throws
IOException, TemplateException {

// Get the template object
        Template template = configuration.getTemplate(templatePath);
        StringWriter writer = new StringWriter();

       
        try { // Merge the data-model and the template
            template.process(model, writer);

        } catch (freemarker.template.TemplateException fmte) {
            throw new TemplateException(fmte);
        }

        return result.toString();
}//template

and also good idea to add handy functions from Google Guava to ClickUtils:
public static <K, V> Map<K, V> mapOf (final K k, final V v) {
    final Map<K, V> map = new LinkedHashMap<K, V>();
    map.put(k, v);
    return map;
  }//mapOf


  public static <K, V> Map<K, V> mapOf (final K k1, final V v1, final K k2,
final V v2) {
    final Map<K, V> map = new LinkedHashMap<K, V>();
    map.put(k1, v1);
    map.put(k2, v2);
    return map;
  }//mapOf


  public static <K, V> Map<K, V> mapOf (final K k1, final V v1, final K k2,
final V v2, final K k3, final V v3) {
    final Map<K, V> map = new LinkedHashMap<K, V>();
    map.put(k1, v1);
    map.put(k2, v2);
    map.put(k3, v3);
    return map;
  }//mapOf


then you can make above code easy
return template("/com/andreig/hiredonphone/cap.ftl_or_vm",
mapOf("public_key", public_key);

Template in external file (not in java String) is good thing: IDE can
highlight and verify it.

Only one problem here: Freemarker/Velocity binding (if you change template
engine code stop working).

But there is my comment in JIRA with idea how to solve it:
https://issues.apache.org/jira/browse/CLK-364?focusedCommentId=12801392&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_12801392


PS: just my fast brain dump (so sorry for usual bad English) 
-- 
View this message in context: http://click.1134972.n2.nabble.com/Page-template-or-similar-method-and-more-seamless-Velocity-Freemarker-integration-tp5324521p5324521.html
Sent from the click-development mailing list archive at Nabble.com.

Mime
View raw message