Mustache template to print text table instead of html table

1,596 views
Skip to first unread message

Meredith

unread,
Jul 8, 2016, 1:53:36 AM7/8/16
to mustache.java
Hi,
I am trying to use mustache.java to display json data in tabular format on console. 
I am able to create html table using the below mustache template but I want the output to be a text/ascii table instead of an html table.

Mustache template:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
     
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Mark</th>
                <th>Subject</th>
            </tr>
        </thead>
        <tbody>
            {{#students}}
            <tr>
                <td>{{name}}</td>
                <td>{{mark}}</td>
                <td>{{subject}}</td>
            </tr>
            {{/students}}
        </tbody>
    </table>
</body>
</html>

JSON data:
{
    "students": [

        {
            "name": "sandeep1",
            "mark": 35,
            "subject": "Geography"
        },

        {
            "name": "sangeeta",
            "mark": 135,
            "subject": "English"
        },

        {
            "name": "surabhi",
            "mark": 315,
            "subject": "History"
        },

        {
            "name": "sumanta",
            "mark": 305,
            "subject": "Mathematics"
        },

        {
            "name": "Ram",
            "mark": 352,
            "subject": "History"
        },

        {
            "name": "John",
            "mark": 350,
            "subject": "Geography"
        },

        {
            "name": "Jack",
            "mark": 355,
            "subject": "English"
        }
    ]}

Is there a way to display json data as a text table using mustache.java?
I have attached a sample format that I found online.
 
pt.jpg

Sam Pullara

unread,
Jul 8, 2016, 2:42:29 AM7/8/16
to mustac...@googlegroups.com
No, you need to do some business logic to determine what the columns are and how wide they should be. You will have to add a small view class that evaluates the JSON and figures out that information about the data at least.

Sam,


--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Sam Pullara

unread,
Jul 12, 2016, 12:31:47 PM7/12/16
to mustac...@googlegroups.com
Thought about your use case for a bit and I don't think that it is such a great fit for templating. Almost everything needs to be calculated, like how many dashes and spaces need to be there and the width of each column.  In the HTML case, the markup is mostly independent of the values and the rendering engine takes care of figuring out the widths and spacing. It is possible to do it, I just think it would awkward.

Sam


On Fri, Jul 8, 2016 at 9:17 AM, Meredith <shimanti...@gmail.com> wrote:
Thanks Sam for your response. I'm new to mustache and handlebars. Could you please provide me a sample view class in java for mustache? I tried searching online but couldn't find any.
Reply all
Reply to author
Forward
0 new messages