【问题标题】:beego template - using key or value of a map as key of other map or pass it to template functionbeego 模板 - 使用地图的键或值作为其他地图的键或将其传递给模板函数
【发布时间】:2017-02-03 07:04:56
【问题描述】:

我有以下 JSON 结构。

func getJsonMappings() *string {
    data := `{
        "CategoryGroup": {
            "Category subgrp1": ["type1", "type2"],

        },
        "CategoryGroupDetail" : {
            "type1":{
                "extension":"abc",
                "title" : "this is description",
                "other": "i have some other details apart from above in this map"
                },
            "type2" :{
                "extension":"abc",
                "title" : "this is description",
                "other": "i have some other details apart from above in this map"
                }
        }

        }`
return &data
}

我正在将 JSON 之上的数据解组为 golang 数据结构,如下面的控制器函数定义

func (this *MainPageController) Get() {
      jsonData := getJsonMappings()

var catMapObj map[string]interface{}

err := json.Unmarshal([]byte(*jsonData), &catMapObj)
if err != nil {
    panic(err.Error())
}
    this.Data["CategoryGroup"] = catMapObj["CategoryGroup"]
    this.Data["CatAttributeMapping"] = catMapObj["CatAttributeMapping"]
    this.TplName = "index.tpl"
}

并尝试在模板下方呈现。

<ul class="collapsible collapsible-accordion">
    {{ range $eachCategory, $subCategoriesList := .CategoryGroup }}
    <li class="bold"><a class="collapsible-header  waves-effect waves-teal">{{ $eachCategory }}</a>
          <div class="collapsible-body" style="">
            <ul>
            {{ range $_, $subConvertorCategoryId := $subCategoriesList }}
                <li><a id="{{ $subConvertorCategoryId }}" class='doc_cvt' href="#">{{ $subConvertorCategoryId|getCategoryTitle }}</a></li>
            {{ end }}
            </ul>
          </div>
    </li>
    {{ end }}
    </ul>

其中 getCategoryTitle 是模板函数。但是我没有得到类型变量作为函数参数的任何值。我的模板函数定义看起来像

func GetCategoryTitle(type string) (title string) {
.....
}

如果我在函数内部将“type”的值硬编码为“type1”,那么一切看起来都很好。但我想在运行时从模板发送值。同时,我可以将“.CategoryGroup”的值传递给模板函数。

因此我的问题是:

1-如何将解析模板时收到的map的键或值传递给golang模板函数?

2- 如果你仔细看上面的结构,你会发现我不需要编写模板函数。我应该得到像 {{ .CategoryGroupDetail.$subConvertorCategoryId.title }}。但我不能这样做。我对 django(python 框架)做了同样的事情。在 golang/beego 中也一定有办法做到这一点。

我是 golang 和 Beego 的新手。请指导我如何进一步进行。

尝试1:

<ul class="collapsible collapsible-accordion">
{{ range $eachCategory, $subCategoriesList := .CategoryGroup }}
<li class="bold"><a class="collapsible-header  waves-effect waves-teal">{{ $eachCategory }}</a>
      <div class="collapsible-body" style="">
        <ul>
        {{ range $_, $subConvertorCategoryId := $subCategoriesList }}
        {{ $categoryDetail := index .CatAttributeMapping $subConvertorCategoryId}}
            <li><a id="{{ $subConvertorCategoryId }}" class='doc_cvt' href="#">{{ $categoryDetail.title }}</a></li>
        {{ end }}
        </ul>
      </div>
</li>
{{ end }}

我在运行时遇到错误 -

template: index.tpl:38:32: executing "index.tpl" at <.CatAttributeMapping>: can't evaluate field CatAttributeMapping in type interface {}

【问题讨论】:

    标签: go beego go-templates


    【解决方案1】:

    您似乎正在尝试使用点运算符访问地图的内部。不知道最终结果应该是什么样子会使这变得更加困难。

    我认为这个例子可以帮助你解决这两个问题:

    {{ range $eachCategory, $subCategoriesList := .CategoryGroup }}
    <li class="bold"><a class="collapsible-header  waves-effect waves-teal">{{ $eachCategory }}</a>
          <div class="collapsible-body" style="">
            <ul>
            {{ range $key, $val := index .CatAttributeMapping $eachCategory }}
                <li><a id="{{ $key}}" class='doc_cvt' href="#">{{ $val.title }}</a></li>
            {{ end }}
            </ul>
          </div>
    </li>
    {{ end }}
    

    有关索引命令的更多详细信息,请查看:http://golang.org/pkg/text/template/

    关键部分是

    index 返回第一个参数的索引结果 以下论点。因此,在 Go 语法中,“index x 1 2 3”是 x1[2][3]。每个索引项必须是映射、切片或数组。

    在大多数情况下,Beego 只是使用 golang 基础模板系统。

    编辑 1:您在问一个复合问题,所以事情变得有点棘手。首先要弄清楚的是您是否将数据正确解析为结构。如果您不这样做,则将其传递到一个界面中,以便您弄清楚如何处理。从您的代码看来,您正在处理这种情况。

    所以一次解决一个问题。 this is a great Q&A that covers parsing nested structures in go

    如果您仍有问题,我建议您使用不同的标签重新提交您的问题。提交一份到 golang json 尝试正确解析,提交一份到 golang 模板,一旦你验证了结构设置正确。
    首先将您的数据放入结构中,然后使其与模板一起使用。很抱歉,我没有适合您的复制粘贴解决方案,但我现在有点忙。

    我会尽我所能回到这个问题,因为我认为你并不孤单。使用 JSON 解析器处理嵌套结构和处理模板引擎可能很困难。我们需要创造更多的例子来说明如何做事!

    【讨论】:

    • @Diablojoe-感谢您的回复。但是结构与你想象的有点不同。请再次参考json结构。
    • @Diablojoe- 在您的指导下,我已将 Try1 添加为新尝试。请看一下。在将 jsone 数据转换为 golang 结构时,我似乎应该使用 struct 而不是 interface{}。
    • @Diablojoe- 没问题.. 我已经尝试过 struct .. 那个时候我也得到了错误。顺便说一句,只要您有时间,请提出解决方案。会有帮助的。
    猜你喜欢
    • 2012-03-07
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多