Template code | Chunk output | |
---|---|---|
{#example_1} {!-- You can extend Chunk to incorporate any content-loading -- protocol you can dream up. Just write a class that -- implements com.x5.template.ContentSource and register -- the class with your Theme before rendering your Chunk. -- -- Then use tags in your template of the form: -- {% [protocol].[fetch_item_name] %} -- --} <pre> There once was a man from Nantucket. {% db.contest_winner %} </pre> {#} Theme theme = new Theme("examples"); theme.addProtocol( new DatabaseTemplateAgent() ); // fetch template snippet from themes/examples/db.chtml Chunk html = theme.makeChunk("db#example_1"); StreamWriter out = getStreamWriter(); html.render( out ); import com.x5.template.ContentSource; import com.x5.template.Snippet; public class DatabaseTemplateAgent implements ContentSource { public String fetch(String itemName) { return getFromDB(itemName); } public String getProtocol() { return "db"; } public boolean provides(String itemName) { String item = fetch(itemName); if (item != null) { return true; } else { return false; } } public Snippet getSnippet(String itemName) { return Snippet.getSnippet(fetch(itemName)); } private String getFromDB(String itemName) { // TODO: implement for real return getDummyData(itemName); // something like this: // // String SQL = "SELECT content FROM cms_table " // + "WHERE item_name = " // + Query.quote(itemName); // // Query q = db.doSelect(SQL); // if (q.hasMoreRows()) { // return q.getField(0); // } else { // return null; // } } private String getDummyData(String itemName) { if (itemName != null && itemName.equals("contest_winner")) { return "Who caught his foot fast in a bucket.\n" + "When he pulled good and sound,\n" + "What he finally found\n" + "Was some old chewing gum that had stuck it.\n"; } else { return null; } } } |
|
<pre> There once was a man from Nantucket. Who caught his foot fast in a bucket. When he pulled good and sound, What he finally found Was some old chewing gum that had stuck it. </pre> There once was a man from Nantucket. Who caught his foot fast in a bucket. When he pulled good and sound, What he finally found Was some old chewing gum that had stuck it. |
Template code | Chunk output | |
---|---|---|
{#example_2} <div> {% quotes.randomquote %} </div> <div style="margin-top:10px"> <a href="">Reload page</a> to fetch a new quote. </div> {#} Theme theme = new Theme("examples"); theme.addProtocol( new QuoteAgent() ); // fetch template snippet from themes/examples/db.chtml Chunk html = theme.makeChunk("db#example_2"); StreamWriter out = getStreamWriter(); html.render( out ); import java.io.BufferedReader; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import com.x5.template.Chunk; import com.x5.template.ContentSource; import com.x5.template.Snippet; public class QuoteAgent implements ContentSource { public String getProtocol() { return "quotes"; } public String fetch(String itemName) { // grab a random quote from http://weirdo.purewhite.id.au/rssrandomquotes/ try { String itemBody = QuoteAgent.fetchQuote(itemName); return itemBody; } catch (java.io.IOException e) { String msg = e.getMessage(); if (msg != null && msg.contains("500")) { return "[error 500 fetching rss resource "+itemName+"]"; } e.printStackTrace(System.err); return "[error fetching rss resource "+itemName+"]"; } } public Snippet getSnippet(String itemName) { return Snippet.getSnippet(fetch(itemName)); } public boolean provides(String itemName) { String x = fetch(itemName); if (x == null || x.startsWith("[error fetching")) { return false; } else { return true; } } public static String fetchQuote(String itemName) throws java.io.IOException { // for now, ignore itemName - always fetch random quote. URL url = new URL("http://weirdo.purewhite.id.au/rssrandomquotes/"); URLConnection conn = url.openConnection(); conn.connect(); InputStream in = conn.getInputStream(); BufferedReader lines = new BufferedReader(new java.io.InputStreamReader(in, "UTF8")); String line = null; StringBuffer rssXML = new StringBuffer("{% exec @xml %}\n"); while ((line = lines.readLine()) != null) { rssXML.append(line); } in.close(); rssXML.append("\n{% body %}{$item.description}{% endbody %}\n{% endexec %}\n"); // let chunk do the heavy lifting Chunk c = new Chunk(); c.append(rssXML.toString()); return c.toString(); } } |
|
<div> [error fetching rss resource randomquote] </div> <div style="margin-top:10px"> <a href="">Reload page</a> to fetch a new quote. </div>
[error fetching rss resource randomquote]
Reload page to fetch a new quote.
|