【问题标题】:How to stop encoding url in template file on Beego?如何在 Beego 的模板文件中停止编码 url?
【发布时间】:2016-12-15 01:24:09
【问题描述】:

我在处理 Beego 上的模板和 url 编码时遇到了麻烦。

(Beego是Go语言的模板引擎之一)

如何在 Beego 的模板文件中停止 HTML TAG 中的 url 编码?

请告诉我。

--

logcontroller.go

package controllers

import (
    "mycode/models"
)

type FiletranslogController struct {
    baseController
}

func (this *FiletranslogController) Get() {
    // Already encoded url
    this.Data["querystring"] = "/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&edate=2016-12-13%2023%3A59&md5=&trans_type=2"

    this.TplName = "log/filetrans.html"
}

文件传输.html

<!-- Not in TABLE TAG -->
{{str2html .querystring}}

<!-- In TABLE TAG -->
<table  id="table-log"
        data-url="{{str2html .querystring}}"
        data-toggle="table"
        data-toolbar="#toolbar-log"
        data-search="true"
        data-show-refresh="true"
        data-pagination="true"
        data-side-pagination="server"
        >
    <thead>
    <tr>
        <th data-field="rdate">Date</th>
        <th data-field="mail_sender">Mail Sender</th>
        <th data-field="trans_type">Trans Type</th>
        <th data-field="md5">MD5</th>
    </tr>
    </thead>
</table>
<script>

在网络浏览器上查看源代码

<!-- Not in TABLE TAG -->
/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&edate=2016-12-13%2023%3A59&md5=&trans_type=2

<!-- In TABLE TAG -->
<table  id="table-log"
        data-url="/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&amp;edate=2016-12-13%2023%3A59&amp;md5=&amp;trans_type=2"
        data-toggle="table"
        data-toolbar="#toolbar-log"
        data-search="true"
        data-show-refresh="true"
        data-pagination="true"
        data-side-pagination="server"
        >
    <thead>
    <tr>
        <th data-field="rdate">Date</th>
        <th data-field="mail_sender">Mail Sender</th>
        <th data-field="trans_type">Trans Type</th>
        <th data-field="md5">MD5</th>
    </tr>
    </thead>
</table>
<script>

天哪

/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&edate=2016-12-13%2023%3A59&md5=&trans_type=2
---> changed to 
/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&amp;edate=2016-12-13%2023%3A59&amp;md5=&amp;trans_type=2

* ex) PHP Smarty 模板引擎支持 {literal} bla..bla..never encoding {/literal} 标签。 *

【问题讨论】:

标签: templates go encoding ampersand beego


【解决方案1】:
  • str2html 将字符串解析为 HTML,没有转义。 {{str2html .Strhtml}}

https://beego.me/docs/mvc/view/template.md

【讨论】:

  • 很荣幸见到你,AstaXie。我已经试过了。但是,它只能在 TAG 之外工作。{{.str2html .querystring}} vs &lt;table data-url="{{.str2html .querystring}}"&gt;&lt;/table&gt;
【解决方案2】:

第二次测试结果。

template_file.html

{{str2html .querystring}}
<table  data-url="{{.querystring}}"
        data-url='{{.querystring}}'
        data-url="{{str2html .querystring}}"
        data-url='{{str2html .querystring}}'
        >
    <thead>
    <tr>
        <th data-field="rdate">Date</th>
        <th data-field="mail_sender">Mail Sender</th>
        <th data-field="trans_type">Trans Type</th>
        <th data-field="md5">MD5</th>
    </tr>
    </thead>
</table>

在网络浏览器上查看源代码

/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&edate=2016-12-13%2023%3A59&md5=&trans_type=2
<table  data-url="/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&amp;edate=2016-12-13%2023%3A59&amp;md5=&amp;trans_type=2"
        data-url='/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&amp;edate=2016-12-13%2023%3A59&amp;md5=&amp;trans_type=2'
        data-url="/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&amp;edate=2016-12-13%2023%3A59&amp;md5=&amp;trans_type=2"
        data-url='/filetranslog/getlogs?sdate=2016-11-13%2000%3A00&amp;edate=2016-12-13%2023%3A59&amp;md5=&amp;trans_type=2'

        >
    <thead>
    <tr>
        <th data-field="rdate">Date</th>
        <th data-field="mail_sender">Mail Sender</th>
        <th data-field="trans_type">Trans Type</th>
        <th data-field="md5">MD5</th>
    </tr>
    </thead>
</table>

为什么要对文字字符串进行编码?我使用“beego.ParseForm”函数进行表单解析,但是“beego.ParseForm”没有正确解析双编码的url。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2020-01-15
    • 2021-07-11
    相关资源
    最近更新 更多