2015-01-05 2 views
3

HTML 템플릿 '' 탈출 방지 방법 :golang의 HTML 템플릿 '에 '탈출

package main 

import (
    "html/template" 
    "os" 
) 

const tmpl = `<html> 
    <head> 
     <title>{{.Title}}</title> 
    </head> 
</html>` 

func main() { 
    t := template.Must(template.New("ex").Parse(tmpl)) 
    v := map[string]interface{}{ 
     "Title": template.HTML("Hello World'"), 
    } 
    t.Execute(os.Stdout, v) 
} 

그것은 출력 :

<html> 
    <head> 
     <title>Hello World&#39;</title> 
    </head> 
</html> 

원하는 출력 :

<html> 
    <head> 
     <title>Hello World'</title> 
    </head> 
</html> 

playouground

+0

사용중인 js 라이브러리를 제공해주십시오. – Innovation

+2

' 괜찮 으면 그냥 둡니다. – Volker

+1

@Innovation no js, ​​just golang –

답변

1

@dyoo는 <title> 콘텐츠가 RCDATA로 취급됩니다. 이스케이프 처리를 수행하는 코드는 here입니다. 지점 if t == contentTypeHTMLtemplate.HTML과 함께 발생합니다.

원본의 출력을 제어해야하는 경우 text/template을 사용하고 수동으로 이스케이프 처리하십시오.