You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/07.4.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -365,21 +365,21 @@ func main() {
365
365
366
366
templates, err = template.ParseFiles(allFiles...) #parses all .tmpl files in the 'templates' folder
367
367
368
-
s1, _:= templates.LookUp("header.tmpl")
368
+
s1:= templates.Lookup("header.tmpl")
369
369
s1.ExecuteTemplate(os.Stdout, "header", nil)
370
370
fmt.Println()
371
-
s2, _:= templates.LookUp("content.tmpl")
371
+
s2:= templates.Lookup("content.tmpl")
372
372
s2.ExecuteTemplate(os.Stdout, "content", nil)
373
373
fmt.Println()
374
-
s3, _:= templates.LookUp("footer.tmpl")
374
+
s3:= templates.Lookup("footer.tmpl")
375
375
s3.ExecuteTemplate(os.Stdout, "footer", nil)
376
376
fmt.Println()
377
377
s3.Execute(os.Stdout, nil)
378
378
}
379
379
```
380
380
Here we can see that `template.ParseFiles` parses all nested templates into cache, and that every template defined by `{{define}}` are independent of each other. They are persisted in something like a map, where the template names are keys and the values are the template bodies. We can then use `ExecuteTemplate` to execute the corresponding sub-templates, so that the header and footer are independent and content contains them both. Note that if we try to execute `s1.Execute`, nothing will be outputted because there is no default sub-template available.
381
381
382
-
When you don't want to use `{{define}}`, then you can just create a text file with the name of the sub template, for instance `_head.tmpl` is a sub template which you'll use across your project then create this file in the templates folder, and use the normal syntax. Lookup cache is basically created so that you don't read the file every time you serve a request, because if you do, then you are wasting a lot of resources for reading a file which won't change unless the codebase is being rewritten, it doesn't make sense to parse the template files during each HTTP GET request, so the technique is used where we parse the files once and then do a `LookUp()` on the cache to execute the template when we need it to display data.
382
+
When you don't want to use `{{define}}`, then you can just create a text file with the name of the sub template, for instance `_head.tmpl` is a sub template which you'll use across your project then create this file in the templates folder, and use the normal syntax. Lookup cache is basically created so that you don't read the file every time you serve a request, because if you do, then you are wasting a lot of resources for reading a file which won't change unless the codebase is being rewritten, it doesn't make sense to parse the template files during each HTTP GET request, so the technique is used where we parse the files once and then do a `Lookup()` on the cache to execute the template when we need it to display data.
383
383
384
384
Templates in one set know each other, but you must parse them for every single set.
Copy file name to clipboardExpand all lines: es/07.4.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -364,21 +364,21 @@ Código:
364
364
365
365
templates, err = template.ParseFiles(allFiles...) #parses all .tmpl files in the 'templates' folder
366
366
367
-
s1, _ := templates.LookUp("header.tmpl")
367
+
s1:= templates.Lookup("header.tmpl")
368
368
s1.ExecuteTemplate(os.Stdout, "header", nil)
369
369
fmt.Println()
370
-
s2, _ := templates.LookUp("content.tmpl")
370
+
s2:= templates.Lookup("content.tmpl")
371
371
s2.ExecuteTemplate(os.Stdout, "content", nil)
372
372
fmt.Println()
373
-
s3, _ := templates.LookUp("footer.tmpl")
373
+
s3:= templates.Lookup("footer.tmpl")
374
374
s3.ExecuteTemplate(os.Stdout, "footer", nil)
375
375
fmt.Println()
376
376
s3.Execute(os.Stdout, nil)
377
377
}
378
378
```
379
379
Como podemos ver aquí `template.ParseFiles` analiza todos las plantillas analizadas en un caché y cada plantilla definida por `{{define}}` es independiente de la otra. Ellas persisten en algo como un mapa, donde el nombre de la plantilla es la llave y el valor es el cuerpo de la plantilla. Podemos entonces usar `ExecuteTemplate` para ejecutar el subtemplate correspondiente, entonces el encavezado y el pié de página sin independientes y el contenido los tiene a ambos. Nota que si tratamos de ejecutar `s1.Execute` nada saldrá porque no hay ningún subtemplate disponible.
380
380
381
-
Cuando quieras usar `define`, tienes que crear un archivo de texto con el mismo nombre de la subplantilla, por ejemplo `_head.tmpl` es una subplantila que se usa al rededor de tuproyecto, entonces crea este archivo en una carpeta para palanillas y úsala la sintaxis normal. El caché de verificación es creado básicamente para que no tengas que leer la plantilla cada vez que sirvas la petición, porque si lo haces, estás gastando un montón de recursos para leer un archivo que no cambia a menos que el código base sea reescrito, lo cual no tiene sentido para hacer en cada petición GET, entonces esta técnica es usada para analizar los archivos y luego usar un `LookUp()` en el caché para ejecutar la plantilla cuando se necesite mostrar la información.
381
+
Cuando quieras usar `define`, tienes que crear un archivo de texto con el mismo nombre de la subplantilla, por ejemplo `_head.tmpl` es una subplantila que se usa al rededor de tuproyecto, entonces crea este archivo en una carpeta para palanillas y úsala la sintaxis normal. El caché de verificación es creado básicamente para que no tengas que leer la plantilla cada vez que sirvas la petición, porque si lo haces, estás gastando un montón de recursos para leer un archivo que no cambia a menos que el código base sea reescrito, lo cual no tiene sentido para hacer en cada petición GET, entonces esta técnica es usada para analizar los archivos y luego usar un `Lookup()` en el caché para ejecutar la plantilla cuando se necesite mostrar la información.
382
382
383
383
Las plantillas en un conjunto se pueden conocer unas a otras, pero debes analizar por cada conjunto en específico.
0 commit comments