Przeglądaj źródła

Merge pull request #44 from qor5/html-lang

Felix Sun 1 rok temu
rodzic
commit
145cb859a6
2 zmienionych plików z 24 dodań i 12 usunięć
  1. 14 12
      builder.go
  2. 10 0
      page_injector.go

+ 14 - 12
builder.go

@@ -65,18 +65,20 @@ func NoopLayoutFunc(r *http.Request, injector *PageInjector, body string) (outpu
 }
 
 func defaultLayoutFunc(r *http.Request, injector *PageInjector, body string) (output string, err error) {
-
-	root := h.HTML(
-		h.Head(
-			injector.GetHeadHTMLComponent(),
-		),
-		h.Body(
-			h.Div(
-				h.RawHTML(body),
-			).Id("app").Attr("v-cloak", true),
-			injector.GetTailHTMLComponent(),
-		).Class("front"),
-	)
+	root := h.HTMLComponents{
+		h.RawHTML("<!DOCTYPE html>\n"),
+		h.Tag("html").Children(
+			h.Head(
+				injector.GetHeadHTMLComponent(),
+			),
+			h.Body(
+				h.Div(
+					h.RawHTML(body),
+				).Id("app").Attr("v-cloak", true),
+				injector.GetTailHTMLComponent(),
+			).Class("front"),
+		).AttrIf("lang", injector.GetHTMLLang(), injector.GetHTMLLang() != ""),
+	}
 
 	buf := bytes.NewBuffer(nil)
 	ctx := new(EventContext)

+ 10 - 0
page_injector.go

@@ -21,6 +21,7 @@ type keyComp struct {
 
 type PageInjector struct {
 	comps map[injectPosition][]*keyComp
+	lang  string
 }
 
 type injectPosition int
@@ -124,6 +125,15 @@ func (b *PageInjector) GetExtraHTMLComponent() h.HTMLComponent {
 	return toHTMLComponent(b.comps[extra])
 }
 
+func (b *PageInjector) HTMLLang(lang string) {
+	b.lang = lang
+	return
+}
+
+func (b *PageInjector) GetHTMLLang() string {
+	return b.lang
+}
+
 func (b *PageInjector) addCharsetViewPortIfMissing() {
 	if b.getComp(MetaKey("charset"), head) == nil {
 		b.Meta(MetaKey("charset"), "charset", "utf8")