My template file:
{{range $index, $option := .alternatives}}
<div id="splashAlternative{{$index}}" class="col-sm-2">
<select id="flashSrc_{{$index}}">
{{template "alternative_src.html" $option}}
</select>
</div>
{{end}}
I want pass $option
to template, and alternative_src.html
code :
{{if compare .option ""}}
<option value="" selected="selected">
</option>
{{else}}
<option value=""></option>
{{end}}
{{if compare .option "xxx"}}
<option value="xxx" selected="selected">xxx</option>
{{else}}
<option value="xxx">xxx</option>
{{end}}
but I get problem below:
executing "alternative_src.html" at <.option>: can't evaluate field option in type string
When you use the {{template}}
action and you pass something, that becomes the dot .
in the called template. Quoting from the package doc of text/template
:
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
So inside alternative_src.html
refer to the option only as a dot .
(the dot .
will mean the value of the pipeline you passed to the template, which is $option
in the caller template):
{{if compare . ""}}
<option value="" selected="selected">
</option>
{{else}}
<option value=""></option>
{{end}}
{{if compare . "xxx"}}
<option value="xxx" selected="selected">xxx</option>
{{else}}
<option value="xxx">xxx</option>
{{end}}