beego模板-使用地图的键或值作为其他地图的键或将其传递给模板功能

I am having below JSON structure.

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
}

I am Unmarshalling above JSON to golang data-structure like below in controller function definition

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"
}

and trying to render below template.

<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>

where getCategoryTitle is template function. But I am not getting any value for type variable as function parameter. My template function definition looks like

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

if I hardcode the value of "type" to "type1" inside the function then all looks good. But I want to sent the value from template at run time. At the same moment of time I am able to pass the value of ".CategoryGroup" to template function.

Hence my question is :

1- How to pass key or value of map received while parsing template to golang template function?

2- If you will look above structure closely then you will find that I need not to write the template function. I should get the value like {{ .CategoryGroupDetail.$subConvertorCategoryId.title }}. But I am not able to do so. I have did the same with django (python framework). There must be way to do the same in golang/beego as well.

I am new to golang and Beego. Please guide me how to proceed further.

Try1:

<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 }}

I am getting below error at run time -

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

It looks like you are trying to access the internals of a map using a dot operator. Not knowing what the final result should look like makes this a little more difficult.

I think this example will get you where you want to go for both questions:

{{ 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 }}

For more details on the index command check out: http://golang.org/pkg/text/template/

The key part being

index Returns the result of indexing its first argument by the following arguments. Thus "index x 1 2 3" is, in Go syntax, x1[2][3]. Each indexed item must be a map, slice, or array.

Beego is just using the golang base template system in most cases.

Edit 1: You are asking a compound question so things get a little tricky. The first thing to figure out is if you are parsing the data correctly into a struct. If you are not doing that go will pass it into an interface for you to figure out how to deal with. From your code it looks like you are dealing with that sort of situation.

So take it one problem at a time. this is a great Q&A that covers parsing nested structures in go

I would encourage you to resubmit your question with different tags if you are still having problems. Submit one to golang json trying to get the parse correct and one to golang templates once you have verified the structs are properly set up.
First get your data into a struct then make it work with a template. I am sorry I do not have a copy paste solution for you but I am a little busy now.

I will do what I can to come back to this question because I don't think that you are alone. Dealing with nested structs with the JSON parser and dealing with the templating engine can be difficult. We need to create more examples of how to do things!