Skip to content

Commit 992a71e

Browse files
authored
Merge pull request astaxie#862 from gmallard/lookup-07.4
Correct compile fail, use .Lookup correctly.
2 parents 483889c + b7c1af1 commit 992a71e

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

en/07.4.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,21 @@ func main() {
365365

366366
templates, err = template.ParseFiles(allFiles...) #parses all .tmpl files in the 'templates' folder
367367

368-
s1, _ := templates.LookUp("header.tmpl")
368+
s1 := templates.Lookup("header.tmpl")
369369
s1.ExecuteTemplate(os.Stdout, "header", nil)
370370
fmt.Println()
371-
s2, _ := templates.LookUp("content.tmpl")
371+
s2 := templates.Lookup("content.tmpl")
372372
s2.ExecuteTemplate(os.Stdout, "content", nil)
373373
fmt.Println()
374-
s3, _ := templates.LookUp("footer.tmpl")
374+
s3 := templates.Lookup("footer.tmpl")
375375
s3.ExecuteTemplate(os.Stdout, "footer", nil)
376376
fmt.Println()
377377
s3.Execute(os.Stdout, nil)
378378
}
379379
```
380380
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.
381381

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.
383383

384384
Templates in one set know each other, but you must parse them for every single set.
385385

es/07.4.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,21 @@ Código:
364364
365365
templates, err = template.ParseFiles(allFiles...) #parses all .tmpl files in the 'templates' folder
366366
367-
s1, _ := templates.LookUp("header.tmpl")
367+
s1 := templates.Lookup("header.tmpl")
368368
s1.ExecuteTemplate(os.Stdout, "header", nil)
369369
fmt.Println()
370-
s2, _ := templates.LookUp("content.tmpl")
370+
s2 := templates.Lookup("content.tmpl")
371371
s2.ExecuteTemplate(os.Stdout, "content", nil)
372372
fmt.Println()
373-
s3, _ := templates.LookUp("footer.tmpl")
373+
s3 := templates.Lookup("footer.tmpl")
374374
s3.ExecuteTemplate(os.Stdout, "footer", nil)
375375
fmt.Println()
376376
s3.Execute(os.Stdout, nil)
377377
}
378378
```
379379
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.
380380

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.
382382

383383
Las plantillas en un conjunto se pueden conocer unas a otras, pero debes analizar por cada conjunto en específico.
384384

0 commit comments

Comments
 (0)