You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2009/04/06 23:35:30 UTC

svn commit: r762516 - in /incubator/click/trunk/click/framework/src/org/apache/click/element: CssStyle.java JsScript.java

Author: sabob
Date: Mon Apr  6 21:35:29 2009
New Revision: 762516

URL: http://svn.apache.org/viewvc?rev=762516&view=rev
Log:
added constructor to render JsScript and CssStyle from Velocity templates

Modified:
    incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java
    incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java

Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java?rev=762516&r1=762515&r2=762516&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/CssStyle.java Mon Apr  6 21:35:29 2009
@@ -18,6 +18,9 @@
  */
 package org.apache.click.element;
 
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.click.Context;
 import org.apache.click.util.HtmlStringBuffer;
 import org.apache.commons.lang.builder.HashCodeBuilder;
 
@@ -80,14 +83,11 @@
  *             // Create a default template model to pass to the template
  *             Map model = ClickUtils.createTemplateModel(this, context);
  *
- *             // Specify the path to CSS Velocity template
- *             String cssTemplate = "/css/style-template.css";
+ *             // Specify the path to CSS template
+ *             String templatePath = "/css/style-template.css";
  *
- *             // Render the template using the model above
- *             String content = context.renderTemplate(cssTemplate, model);
- *
- *             // Create the inline Css for the given template
- *             CssStyle cssStyle = new CssStyle(content);
+ *             // Create the inline Css for the given template path and model
+ *             CssStyle cssStyle = new CssStyle(templatePath, model);
  *             headElements.add(cssStyle);
  *         }
  *         return headElements;
@@ -106,14 +106,14 @@
  *
  * <h3>Character data (CDATA) support</h3>
  *
- * Sometimes it is necessary to wrap <tt>inline</tt> {link Css} in CDATA tags.
- * Two use cases are common for doing this:
+ * Sometimes it is necessary to wrap <tt>inline</tt> {@link CssStyle Css} in
+ * CDATA tags. Two use cases are common for doing this:
  * <ul>
  * <li>For XML parsing: When using Ajax one often send back partial
  * XML snippets to the browser, which is parsed as valid XML. However the XML
- * parser will throw an error if the script contains reserved XML characters
+ * parser will throw an error if the content contains reserved XML characters
  * such as '&amp;', '&lt;' and '&gt;'. For these situations it is recommended
- * to wrap the script content inside CDATA tags.
+ * to wrap the style content inside CDATA tags.
  * </li>
  * <li>XHTML validation: if you want to validate your site using an XHTML
  * validator e.g: <a target="_blank" href="http://validator.w3.org/">http://validator.w3.org/</a>.</li>
@@ -155,6 +155,12 @@
      */
     private boolean characterData = false;
 
+    /** The path of the template to render. */
+    private String template;
+
+    /** The model of the template to render. */
+    private Map model;
+
     // ------------------------------------------------------------ Constructor
 
     /**
@@ -177,6 +183,41 @@
         setAttribute("rel", "stylesheet");
     }
 
+    /**
+     * Construct a new Css style element for the given template path
+     * and template model.
+     * <p/>
+     * When the CssStyle is rendered the template and model will be merged and
+     * the result will be rendered together with any CssStyle
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+     * <p/>
+     *
+     * For example:
+     * <pre class="prettyprint">
+     * public class MyPage extends Page {
+     *     public void onInit() {
+     *         Context context = getContext();
+     *
+     *         // Create a default template model
+     *         Map model = ClickUtils.createTemplateModel(this, context);
+     *
+     *         // Create CssStyle for the given template path and model
+     *         CssStyle style = new CssStyle("/mypage-template.css", model);
+     *
+     *         // Add style to the Page Head elements
+     *         getHeadElements().add(style);
+     *     }
+     * } </pre>
+     *
+     * @param template the path of the template to render
+     * @param model the template model
+     */
+    public CssStyle(String template, Map model) {
+        this(null);
+        setTemplate(template);
+        setModel(model);
+    }
+
     // ------------------------------------------------------ Public properties
 
     /**
@@ -228,6 +269,55 @@
         this.characterData = characterData;
     }
 
+    /**
+     * Return the path of the template to render.
+     *
+     * @see #setTemplate(java.lang.String)
+     *
+     * @return the path of the template to render
+     */
+    public String getTemplate() {
+        return template;
+    }
+
+    /**
+     * Set the path of the template to render.
+     * <p/>
+     * If the {@link #template} property is set, the template and {@link #model}
+     * will be merged and the result will be rendered together with any CssStyle
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+     *
+     * @param template the path of the template to render
+     */
+    public void setTemplate(String template) {
+        this.template = template;
+    }
+
+    /**
+     * Return the model of the {@link #setTemplate(java.lang.String) template}
+     * to render.
+     *
+     * @see #setModel(java.util.Map)
+     *
+     * @return the model of the template to render
+     */
+    public Map getModel() {
+        return model;
+    }
+
+    /**
+     * Set the model of the template to render.
+     * <p/>
+     * If the {@link #template} property is set, the template and {@link #model}
+     * will be merged and the result will be rendered together with any CssStyle
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+     *
+     * @param model the model of the template to render
+     */
+    public void setModel(Map model) {
+        this.model = model;
+    }
+
     // --------------------------------------------------------- Public Methods
 
     /**
@@ -314,11 +404,29 @@
     // ------------------------------------------------------ Protected Methods
 
     /**
-     * Render this CssStyle content to the specified buffer.
+     * Render the CssStyle {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}
+     * to the specified buffer.
+     * <p/>
+     * <b>Please note:</b> if the {@link #setTemplate(java.lang.String) template}
+     * property is set, this method will merge the {@link #setTemplate(java.lang.String) template}
+     * and {@link #setModel(java.util.Map) model} and the result will be
+     * rendered, together with the CssStyle
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content},
+     * to the specified buffer.
      *
      * @param buffer the buffer to append the output to
      */
     protected void renderContent(HtmlStringBuffer buffer) {
+        if (getTemplate() != null) {
+            Context context = getContext();
+
+            Map templateModel = getModel();
+            if (templateModel == null) {
+                templateModel = new HashMap();
+            }
+            buffer.append(context.renderTemplate(getTemplate(), templateModel));
+
+        }
         buffer.append(getContent());
     }
 

Modified: incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java?rev=762516&r1=762515&r2=762516&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/element/JsScript.java Mon Apr  6 21:35:29 2009
@@ -18,6 +18,9 @@
  */
 package org.apache.click.element;
 
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.click.Context;
 import org.apache.click.util.HtmlStringBuffer;
 import org.apache.commons.lang.builder.HashCodeBuilder;
 
@@ -82,14 +85,11 @@
  *             // Add the id of the div to hide
  *             model.put("divId", "myDiv");
  *
- *             // Specify the path to the JavaScript Velocity template
- *             String jsTemplate = "/js/mycorp-template.js";
+ *             // Specify the path to the JavaScript template
+ *             String templatePath = "/js/mycorp-template.js";
  *
- *             // Render the template providing it with the model
- *             String template = getContext().renderTemplate(jsTemplate, model);
- *
- *             // Create the inline JavaScript for the given template
- *             JsScript jsScript = new JsScript(template);
+ *             // Create the inline JavaScript for the given template path and model
+ *             JsScript jsScript = new JsScript(templatePath, model);
  *             headElements.add(jsScript);
  *         }
  *         return headElements;
@@ -109,12 +109,12 @@
  *
  * <h3>Character data (CDATA) support</h3>
  *
- * Sometimes it is necessary to wrap <tt>inline</tt> {@link JsScript} in CDATA
- * tags. Two use cases are common for doing this:
+ * Sometimes it is necessary to wrap <tt>inline</tt> {@link JsScript JavaScript}
+ * in CDATA tags. Two use cases are common for doing this:
  * <ul>
  * <li>For XML parsing: When using Ajax one often send back partial
  * XML snippets to the browser, which is parsed as valid XML. However the XML
- * parser will throw an error if the script contains reserved XML characters
+ * parser will throw an error if the content contains reserved XML characters
  * such as '&amp;', '&lt;' and '&gt;'. For these situations it is recommended
  * to wrap the script content inside CDATA tags.
  * </li>
@@ -156,6 +156,12 @@
      */
     private boolean characterData = false;
 
+    /** The path of the template to render. */
+    private String template;
+
+    /** The model of the template to render. */
+    private Map model;
+
     // ----------------------------------------------------------- Constructors
 
     /**
@@ -177,6 +183,41 @@
         setAttribute("type", "text/javascript");
     }
 
+    /**
+     * Construct a new inline JavaScript element for the given template path
+     * and template model.
+     * <p/>
+     * When the JsScript is rendered the template and model will be merged and
+     * the result will be rendered together with any JsScript
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+     * <p/>
+     *
+     * For example:
+     * <pre class="prettyprint">
+     * public class MyPage extends Page {
+     *     public void onInit() {
+     *         Context context = getContext();
+     *
+     *         // Create a default template model
+     *         Map model = ClickUtils.createTemplateModel(this, context);
+     *
+     *         // Create JsScript for the given template path and model
+     *         JsScript script = new JsScript("/mypage-template.js", model);
+     *
+     *         // Add script to the Page Head elements
+     *         getHeadElements().add(script);
+     *     }
+     * } </pre>
+     *
+     * @param template the path of the template to render
+     * @param model the template model
+     */
+    public JsScript(String template, Map model) {
+        this(null);
+        setTemplate(template);
+        setModel(model);
+    }
+
     // ------------------------------------------------------ Public Properties
 
     /**
@@ -227,6 +268,55 @@
         this.characterData = characterData;
     }
 
+    /**
+     * Return the path of the template to render.
+     *
+     * @see #setTemplate(java.lang.String)
+     *
+     * @return the path of the template to render
+     */
+    public String getTemplate() {
+        return template;
+    }
+
+    /**
+     * Set the path of the template to render.
+     * <p/>
+     * If the {@link #template} property is set, the template and {@link #model}
+     * will be merged and the result will be rendered together with any JsScript
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+     *
+     * @param template the path of the template to render
+     */
+    public void setTemplate(String template) {
+        this.template = template;
+    }
+
+    /**
+     * Return the model of the {@link #setTemplate(java.lang.String) template}
+     * to render.
+     *
+     * @see #setModel(java.util.Map)
+     *
+     * @return the model of the template to render
+     */
+    public Map getModel() {
+        return model;
+    }
+
+    /**
+     * Set the model of the template to render.
+     * <p/>
+     * If the {@link #template} property is set, the template and {@link #model}
+     * will be merged and the result will be rendered together with any JsScript
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}.
+     *
+     * @param model the model of the template to render
+     */
+    public void setModel(Map model) {
+        this.model = model;
+    }
+
     // --------------------------------------------------------- Public Methods
 
     /**
@@ -314,11 +404,29 @@
     // ------------------------------------------------------ Protected Methods
 
     /**
-     * Render this JsScript content to the specified buffer.
+     * Render the JsScript {@link #setContent(org.apache.click.util.HtmlStringBuffer) content}
+     * to the specified buffer.
+     * <p/>
+     * <b>Please note:</b> if the {@link #setTemplate(java.lang.String) template}
+     * property is set, this method will merge the {@link #setTemplate(java.lang.String) template}
+     * and {@link #setModel(java.util.Map) model} and the result will be
+     * rendered, together with the JsScript
+     * {@link #setContent(org.apache.click.util.HtmlStringBuffer) content},
+     * to the specified buffer.
      *
      * @param buffer the buffer to append the output to
      */
     protected void renderContent(HtmlStringBuffer buffer) {
+        if (getTemplate() != null) {
+            Context context = getContext();
+
+            Map templateModel = getModel();
+            if (templateModel == null) {
+                templateModel = new HashMap();
+            }
+            buffer.append(context.renderTemplate(getTemplate(), templateModel));
+
+        }
         buffer.append(getContent());
     }