html/template on range loop re-entry:

940 views
Skip to first unread message

octp...@gmail.com

unread,
Jun 23, 2013, 6:09:13 PM6/23/13
to golan...@googlegroups.com
Hello,

I'm trying to use html/template correctly and it seems there is some logic I haven't quite understood:

error html/template:index:9: on range loop re-entry: {{range}} branches end in different contexts: {stateJS delimNone urlPartNone jsCtxRegexp attrNone elementScript <nil>}, {stateJSRegexp delimNone urlPartNone jsCtxRegexp attrNone elementScript <nil>}

My template fragment looks like this:

{{range .expiries}}
<option>{{.Name }}</option>
{{end }}

Removing the <option>, </option> element from the inner loop fixes the issue. My understanding was that I could use that kind of feature (ie nested html in a range construct).

Help ?!
Thanks for your time
-- 
Pierre.

Nigel Tao

unread,
Jun 24, 2013, 3:26:17 AM6/24/13
to octp...@gmail.com, Mike Samuel, golang-nuts
CC'ing Mike Samuel.
Can you show a bit more of your template around that {{range}}?

octp...@gmail.com

unread,
Jun 24, 2013, 3:35:15 AM6/24/13
to golan...@googlegroups.com, octp...@gmail.com, Mike Samuel
Hello :)

On Monday, June 24, 2013 9:26:17 AM UTC+2, Nigel Tao wrote:
CC'ing Mike Samuel. 



Can you show a bit more of your template around that {{range}}? 


After looking very closely to my template (and one good night after), I found out that the original template was:

<script>
<form class='form-inline' id='gluu_form' method='POST' action='/'>
        <button class='button btn btn-primary' type='submit' />
        <i class='icon-file icon-white'>Gluu</i>

        <span>Expiration <label class='checkbox'>
                <input id='never_expire'> Never</input></label>
        <select id='expiry_delay' class='span3' name='expiry_delay'>
                {{range .expiries}}
      <option {{.Attributes}} >{{.Name }}</option>
                {{end }}
        </select>

</form>
</script>


Can you spot (as now I do) the extra script element surrounding the whole template ? Now, I can, and once removed, the templates compiles and run beautifully. Mind you, the error message was not that explicit (the elementScript should have worried me though)...

Thank you for having heard my babbling. Next time, I'll explain the issue to my rubber duck.

Happy Go'ing
-- 
Pierre.

Mike Samuel

unread,
Jun 24, 2013, 12:40:11 PM6/24/13
to octp...@gmail.com, golang-nuts
Yeah.

I'm not trying to justify the inscrutability of the error message, but
that error message happens because it starts in JS as indicated by
"{stateJS" and then sees

<option>{{.Name }}</option>

which could be a sequence of valid but nonsensical JS tokens as in the
explicitly parenthesized

((1 < option) > "name") < /option>/

and ends inside a regular expression as indicated by "{stateJSRegexp"
because only one slash has been seen.

We could recognize this particular error and mention that there might
be a stray `<script>` or unclosed `onclick="...` earlier in the
template.



2013/6/24 <octp...@gmail.com>:

Ibrahim M. Ghazal

unread,
Jun 25, 2013, 12:57:52 AM6/25/13
to mikes...@gmail.com, octp...@gmail.com, golang-nuts
On Mon, Jun 24, 2013 at 7:40 PM, Mike Samuel <mikes...@gmail.com> wrote:
> Yeah.
>
> I'm not trying to justify the inscrutability of the error message, but
> that error message happens because it starts in JS as indicated by
> "{stateJS" and then sees
>
> <option>{{.Name }}</option>
>
> which could be a sequence of valid but nonsensical JS tokens as in the
> explicitly parenthesized
>
> ((1 < option) > "name") < /option>/
>
> and ends inside a regular expression as indicated by "{stateJSRegexp"
> because only one slash has been seen.
>

This is brilliant. I knew html/template uses context to safely escape
strings in html, but I didn't know it went as far as parsing
JavaScript. Thank you for your great work.
Reply all
Reply to author
Forward
0 new messages