duanqian1888 2018-08-17 23:12
浏览 93
已采纳

渲染html模板golang狂欢

I am hoping someone will be able to help me out. I have started building a web app and decided to go with GO and Revel. So far I've learnt quite a few things but there is this one functionality I can't seem to be able to get working. I have the following code:

package controllers

import (
"github.com/revel/revel"
_ "github.com/denisenkom/go-mssqldb"
"database/sql")

type App struct {
*revel.Controller
}

type resultRow struct {
TransactionDomain string
TransactionType string
TransactionIsoResponse string
Store string
Terminal string
Vendor string
RequestDT string
ResponseDT string
AccountDisplay string
AccountDetails1 string
InvoiceNumber string
Amount string
}

type colNames struct {
Name string
}

type resultTable struct {
fpk string
columns []colNames
resultRows []resultRow
}
func (c App) FpkTable() revel.Result {
//all db section goes here. I have confirmed the results are obtaind back 
//from the db.
    err = rows.Scan(&resRow.TransactionDomain,
        &resRow.TransactionType,
        &resRow.TransactionIsoResponse,
        &resRow.Store,
        &resRow.Terminal,
        &resRow.Vendor,
        &resRow.RequestDT,
        &resRow.ResponseDT,
        &resRow.AccountDisplay,
        &resRow.AccountDetails1,
        &resRow.InvoiceNumber,
        &resRow.Amount)
    if err != nil {
        revel.INFO.Println("Scan failed:", err.Error())
    }
    arrRow = append(arrRow, resRow)
}
columnNames := []colNames{{Name:"Domain"}, {Name:"Type"}, {Name:"Vendor Response"}, {Name:"Store"}, {Name:"Register"}, 
{Name:"Request DT"}, {Name:"Response DT"}, {Name:"Account"}, {Name:"Token"}, {Name:"Invoice"}, {Name:"Amount"}}
table := &resultTable{c.Request.FormValue("store") + "-" + c.Request.FormValue("register") + "-" + c.Request.FormValue("invoice") + "-" + c.Request.FormValue("date"), columnNames, arrRow}
return c.Render(table)

I have the following html template:

<div class="row">
    <a>{{.fpk}}</a>
    <ul>
        {{range .columns}}
        <li>{{.Name}}</li>
        {{end}}
    </ul>
</div>

And I am expecting to see something like this

<div class="row">
    <a>AAA-BBB-CCC-DDD</a>
    <ul>
        <li>Domain</li>
        <li>Type</li>
        .
        .
        .
    </ul>
</div>

But Instead I am getting no values passed into the template at

 <div class="row">
    <a></a>
        <ul>

        </ul>
        ::after
        //This line makes me believe the processing of the
        //template is being done properly except the data 
        //is not being passed properly to the template.
 </div>

I have gone through every possible site wondering about this but I haven't been able to find a solution.

I am sure this will be something simple but anyhow, thanks for any help you might be able to provide.

  • 写回答

1条回答 默认 最新

  • dongzaotiao2863 2018-08-18 16:29
    关注

    I am not sure about your template syntax, usually I range doing it like so:

    <div class="row">
        <a>{{.fpk}}</a>
        <ul>
            {{range $index, $element := .columns}}
            <li>{{$element.Name}}</li>
            {{end}}
        </ul>
    </div>
    

    Another thing that I can think off is that your data may be empty, have you tried checking the columns struct field to see if it contains data?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突