Reputation: 11669
I am trying to use Render Snake HTML library to programatically generate HTML for me. I am trying to make a HTML table by using RenderSnake
as shown below.
PoolName TotalSyncCount TotalAsyncCount SyncNinetyFivePercentile AsyncNinetyFivePercentile
Hello 100 100 4 0
World 300 300 2 0
Here PoolName
, TotalSyncCount
, TotalAsyncCount
, SyncNinetyFivePercentile
and AsyncNinetyFivePercentile
are my column names.
Below is my example which I am able to create using RenderSnake
library and it generates HTML for me.
public class RendersnakeTest {
public static void main(String[] args) throws IOException {
List<PoolMetrics> poolMetricsList = new ArrayList<>();
poolMetricsList.add(new PoolMetrics("A", "0", "0", "0", "0"));
poolMetricsList.add(new PoolMetrics("A", "1", "1", "1", "1"));
poolMetricsList.add(new PoolMetrics("A", "2", "2", "2", "2"));
poolMetricsList.add(new PoolMetrics("A", "3", "3", "3", "3"));
poolMetricsList.add(new PoolMetrics("A", "4", "4", "4", "4"));
HtmlCanvas html = new HtmlCanvas();
html.html().body().table().tr().th().content("PoolName").th().content("TotalSyncCount").th()
.content("TotalAsyncCount").th().content("SyncNinetyFivePercentile").th()
.content("AsyncNinetyFivePercentile")._tr();
// add the rows
for (PoolMetrics pool : poolMetricsList) {
html.tr()
.td(class_("city-table")).content(pool.getPoolName())
.td().content(pool.getTotalAsyncCount())
.td().content(pool.getTotalSyncCount())
.td().content(pool.getSyncNinetyFivePercentile())
.td().content(pool.getAsyncNinetyFivePercentile())
._tr();
}
// close the table
html._table()._body()._html();
// write the file
final String rendered = html.toHtml();
final File output = new File("c:/output.html");
Files.write(output.toPath(), rendered.getBytes("UTF-8"), StandardOpenOption.TRUNCATE_EXISTING);
// and send out an html email with above table
// so at this moment I would like to have css embedded in my html table so that once I receive html email
// it should have applied css in it
SendEmail.getInstance().sendEmail("[email protected]", "[email protected]", "TestSubject", html.toHtml());
}
}
class PoolMetrics {
private String poolName;
private String totalSyncCount;
private String totalAsyncCount;
private String syncNinetyFivePercentile;
private String asyncNinetyFivePercentile;
public PoolMetrics(String poolName, String totalSyncCount, String totalAsyncCount, String syncNinetyFivePercentile, String asyncNinetyFivePercentile) {
this.poolName = poolName;
this.totalSyncCount = totalSyncCount;
this.totalAsyncCount = totalAsyncCount;
this.syncNinetyFivePercentile = syncNinetyFivePercentile;
this.asyncNinetyFivePercentile = asyncNinetyFivePercentile;
}
public String getPoolName() {
return poolName;
}
public String getTotalSyncCount() {
return totalSyncCount;
}
public String getTotalAsyncCount() {
return totalAsyncCount;
}
public String getSyncNinetyFivePercentile() {
return syncNinetyFivePercentile;
}
public String getAsyncNinetyFivePercentile() {
return asyncNinetyFivePercentile;
}
}
Problem Statement:
Now I would like to apply CSS on my above table which I am not sure how to apply. In general, I would like to use this CSS in my above table using RenderSnake
library.
I am not able to understand from their documentation how to apply the CSS. Can anyone help me to do this?
The thing which I am not able to understand is where do we put the CSS file so that it can apply that CSS on my table and how my program will know that it has to apply this CSS. In general, I will be sending out my table as part of an HTML e-mail so in the e-mail it should have all the CSS embedded in the table.
I am familiar with HTML and CSS and how they work but getting confuse with RenderSnake
library and how to use it.
Upvotes: 1
Views: 1425
Reputation: 11416
Given the examples here RenderSnake Examples you're must missing to create the head-section where you can add the linked stylesheet like this:
public class Head implements Renderable {
public void renderOn(HtmlCanvas html) throws IOException {
html
.head()
.macros().stylesheet("htdocs/style-01.css"))
._head();
}
}
Full example:
public class Head implements Renderable {
public void renderOn(HtmlCanvas html) throws IOException {
html
.head()
.title().content(canvas.getPageContext().getString("title"))
.meta(name("description").add("content","renderSnake doc",false))
.macros().stylesheet("htdocs/style-01.css"))
.render(JQueryLibrary.core("1.4.3"))
.render(JQueryLibrary.ui("1.8.6"))
.render(JQueryLibrary.baseTheme("1.8"))
._head();
}
}
One question remaining is how you will send the email - not every mail app is able to append a head-section with linked stylesheets to the email. In case your mail app isn't, you should consider to set the CSS as inline styles. Also because many email clients may remove the head-section, using inline styles is the recommended approach - http://www.benchmarkemail.com/help-FAQ/answer/Using-CSS-in-HTML-emails.
There's also an interview with the RenderSnake project owner Ernest Micklei Simple HTML Output From Java Using renderSnake with an example for setting styles while adding HTML ouput, but this approach wouldn't be recommended as the styles are set as css-class above the appended element:
public class Logo implements Renderable {
public void renderOn(HtmlCanvas html) throws IOException { //@formatter:off
html
.div(id("logo"))
.div(id("logo_text"))
.h1()
.a(href("index.html"))
.write("render")
.style().write(".snake { color:yellow; }")._style()
.span(class_("snake")).write("S")._span()
.write("nake")
._a()
._h1()._div()
._div();
}
}
This will result in (as viewable at the RenderSnake site html-source):
<a href="index.html">render<style>.snake { color:yellow; }</style>
<span class="snake">S</span>nake</a>
So because these classes wrapped in style-tags in the body may also be removed by the email client, it should be recommended to add inline styles in the same way as mentioned in the example for setting html-attributes here: http://rendersnake.org/devguide.html#attributes
html.div(id("me").class_("hidden")).content("Invisible text");
like e.g.
html.td(style("color:#000000;")).content("Black text");
If not already done, it's necessary to
import static org.rendersnake.HtmlAttributesFactory.*
for this.
In case this shouldn't work, it's also mentioned at link above that it's possible to add new attributes:
new HtmlAttributes("key","value").add("another-key","another-value");
but I guess that RenderSnake will already include the style-attribute.
As further references: Best practices for styling HTML emails, some useful information here: http://kb.mailchimp.com/campaigns/ways-to-build/using-css-in-campaigns (though I won't recommend any special mailing service, the issues with CSS and HTML-email are well described there) and the tool mentioned in one of the answers of linked SO-post: http://templates.mailchimp.com/resources/inline-css/
Upvotes: 2