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
Added Slim template support: https://slim-lang.com/
- Added a new directive "@slim" to parse Slim templates into Go code
- Added "@haml" for existing Haml templates
Deprecating "@Goht" template directive; templates should use "@haml" instead.
* Slim templating added
- VSCode [Extension](https://marketplace.visualstudio.com/items?itemName=stackus.goht-vscode) and code [repository](https://github.com/stackus/goht-vscode)
154
180
- JetBrains (GoLand and others) [Plugin](https://plugins.jetbrains.com/plugin/23783-goht) and code [repository](https://github.com/stackus/goht-jetbrains)
@@ -217,7 +243,7 @@ The second parameter passed into the `Render` method can be anything that implem
217
243
such as a file or a buffer, or the `http.ResponseWriter` that you get from an HTTP handler.
218
244
219
245
### Using GoHT with HTTP handlers
220
-
Using the GoHT templates is made very easy.
246
+
Using the GoHT templates is made straightforward.
221
247
```go
222
248
package main
223
249
@@ -245,36 +271,48 @@ There are a number of examples showing various Haml and GoHT features in the [ex
245
271
### A big nod to Templ
246
272
The way that you use GoHT is very similar to how you would use [Templ](https://templ.guide). This is no accident as I am a big fan of the work being done with that engine.
247
273
248
-
After getting the Haml properly lexed, and parsed, I did not want to reinvent the wheel and come up with a whole new rendering API.
274
+
After getting the Haml properly lexed and parsed, I did not want to reinvent the wheel and come up with a whole new rendering API.
249
275
The API that Templ presents is nice and easy to use, so I decided to replicate it in GoHT.
250
276
251
277
## The GoHT template
252
278
GoHT templates are files with the extension `.goht` that when processed will produce a matching Go file with the extension `.goht.go`.
253
279
254
-
In these files you are free to write any Go code that you wish, and then drop into Haml mode using the `@goht` directive.
280
+
In these files you are free to write any Go code that you wish, and then drop into Haml mode using the `@haml` directive.
281
+
282
+
> Note: The original `@goht` directive is still supported for HAML templating, but it is deprecated and will be removed in a future version.
255
283
256
284
The following starts the creation of a SiteLayout template:
257
285
```haml
258
-
@goht SiteLayout() {
286
+
@haml SiteLayout() {
287
+
288
+
or
289
+
290
+
@slim SiteLayout() {
259
291
```
260
292
261
293
GoHT templates are closed like Go functions, with a closing brace `}`. So a complete but empty example is this:
262
294
```haml
263
-
@goht SiteLayout() {
295
+
@haml SiteLayout() {
296
+
}
297
+
298
+
or
299
+
300
+
@slim SiteLayout() {
264
301
}
265
302
```
266
-
Inside the template you can use any Haml features, such as tags, attributes, classes,
303
+
Inside the templates you can use any Haml or Slim features, such as tags, attributes, classes,
267
304
IDs, text, comments, interpolation, code inlining, code rendering, and filters.
268
305
269
-
## Haml Syntax
306
+
## GoHT Syntax
270
307
The Haml syntax is documented at the [Haml](http://haml.info/) website.
271
308
Please see that site or the [Haml Reference](https://haml.info/docs/yardoc/file.REFERENCE.html) for more information.
309
+
The Slim syntax is documented at the [Slim](https://slim-lang.com/) website.
272
310
273
-
GoHT has implemented the Haml syntax very closely.
274
-
So, if you are already familiar with Haml then you should be able to jump right in.
311
+
GoHT has implemented nearly all Haml and Slim syntax.
312
+
So, if you are already familiar with Haml or Slim then you should be able to jump right in.
275
313
There are some minor differences that I will document in the next section.
276
314
277
-
### GoHT and Haml differences
315
+
### GoHT template differences
278
316
279
317
Important differences are:
280
318
-[Go package and imports](#go-package-and-imports): You can declare a package and imports for your templates.
@@ -283,9 +321,9 @@ Important differences are:
283
321
-[Indents](#indents): GoHT follows the rules of GoFMT for indents.
284
322
-[Inlined code](#inlined-code): You won't be using Ruby here, you'll be using Go.
285
323
-[Rendering code](#rendering-code): The catch is what is being outputted will need to be a string in all cases.
286
-
-[Attributes](#attributes): Only the Ruby 1.9 style of attributes is supported.
324
+
-[Attributes](#attributes): Only the Ruby 1.9 (`{...}`) style of attributes is supported.
287
325
-[Classes](#classes): Multiple sources of classes are supported.
288
-
-[Object References](#object-references): Limited support for object references.
326
+
-[Object References](#object-references): Haml Only: Limited support for object references.
289
327
-[Filters](#filters): Partial list of supported filters.
290
328
-[Template nesting](#template-nesting): Templates can be nested, and content can be passed into them.
291
329
@@ -295,19 +333,21 @@ You can provide a package name at the top of your GoHT template file. If you do
295
333
You may also import any packages that you need to use in your template. The imports you use and the ones brought in by GoHT will be combined and deduplicated.
296
334
297
335
### Multiple templates per file
298
-
You can declare as many templates in a file as you wish. Each template must have a unique name in the module they will be output into.
336
+
You can declare as many templates in a file as you wish.
337
+
Each template must have a unique name in the module they will be output into.
338
+
You may also mix Haml and Slim templates in the same file.
299
339
```haml
300
-
@goht SiteLayout() {
340
+
@slim SiteLayout() {
301
341
}
302
342
303
-
@goht HomePage() {
343
+
@haml HomePage() {
304
344
}
305
345
```
306
346
307
347
The templates are converted into Go functions, so they must be valid Go function names.
308
348
This also means that you can declare them with parameters and can use those parameters in the template.
309
349
```haml
310
-
@goht SiteLayout(title string) {
350
+
@haml SiteLayout(title string) {
311
351
!!!
312
352
%html{lang:"en"}
313
353
%head
@@ -316,23 +356,38 @@ This also means that you can declare them with parameters and can use those para
316
356
-# ... the rest of the template
317
357
}
318
358
```
359
+
The same applies to Slim templates:
360
+
```slim
361
+
@slim SiteLayout(title string) {
362
+
doctype html
363
+
html(lang="en")
364
+
head
365
+
title= title
366
+
body
367
+
/ ... the rest of the template
368
+
}
369
+
```
319
370
320
371
### Doctypes
321
372
Only the HTML 5 doctype is supported, and is written using `!!!`.
322
373
```haml
323
-
@goht SiteLayout() {
374
+
@haml SiteLayout() {
324
375
!!!
325
376
}
377
+
378
+
@slim SiteLayout() {
379
+
doctype
380
+
}
326
381
```
327
382
328
-
> Note about indenting. GoHT follows the similar rules as Haml for indenting. The first line of the template must be at the same level as the `@goht` directive. After that, you MUST use tabs to indent the content of the template.
383
+
> Note about indenting. GoHT follows the same rules as Haml for indenting. The first line of the template must be at the same level as the `@haml` or `@slim` directive. After that, you MUST use tabs to indent the content of the template.
329
384
330
385
### Indents
331
386
GoHT follows the rules of GoFMT for indents, meaning that you should use tabs for indentation.
332
387
333
-
You must also indent the content of the template, and the closing brace should be at the same level as the `@goht` directive.
388
+
You must also indent the content of the template, and the closing brace should be at the same level as the `@haml` directive.
334
389
```haml
335
-
@goht SiteLayout() {
390
+
@haml SiteLayout() {
336
391
%html
337
392
%head
338
393
%title GoHT
@@ -341,6 +396,18 @@ You must also indent the content of the template, and the closing brace should b
341
396
}
342
397
```
343
398
399
+
Slim:
400
+
```slim
401
+
@slim SiteLayout() {
402
+
doctype
403
+
html
404
+
head
405
+
title GoHT
406
+
body
407
+
h1 GoHT
408
+
}
409
+
```
410
+
344
411
> Note: Two spaces are being used in this README for display only. Keep that in mind if you copy and paste the examples from this document.
345
412
346
413
### Inlined code
@@ -353,20 +420,26 @@ So instead of this with Ruby:
353
420
- if user
354
421
%strong The user exists
355
422
```
356
-
You would write this with Go:
423
+
You would write this with Go (Go needs the `!= nil` check):
357
424
```haml
358
425
- if user != nil
359
426
%strong The user exists
360
427
```
361
428
There is minimal processing performed on the Go code you put into the templates, so it needs to be valid Go code sans braces.
429
+
362
430
> You may continue to use the braces if you wish. Existing code with braces will continue to work without modifications.
363
431
364
432
### Rendering code
365
-
Like in Haml, you can output variables and the results of expressions. The `=` script syntax and text interpolation `#{}` are supported.
433
+
Like in Haml, you can output variables and the results of expressions. The `=` script syntax and text interpolation `#{}` are supported for both template languages.
366
434
```haml
367
435
%strong= user.Name
368
436
%strong The user's name is #{user.Name}
369
437
```
438
+
Slim:
439
+
```slim
440
+
strong= user.Name
441
+
strong The user's name is #{user.Name}
442
+
```
370
443
371
444
The catch is what is being outputted will need to be a string in all cases.
372
445
So instead of writing this to output an integer value:
@@ -388,7 +461,8 @@ The interpolation also supports the shortcut:
388
461
When formatting a value into a string `fmt.Sprintf` is used under the hood, so you can use any of the formatting options that it supports.
389
462
390
463
### Attributes
391
-
Only the Ruby 1.9 style of attributes is supported.
464
+
**Only the Ruby 1.9 style of attributes is supported.**
465
+
392
466
This syntax is closest to the Go syntax, and is the most readable.
393
467
Between the attribute name, operator, and value you can include or leave out as much whitespace as you like.
394
468
```haml
@@ -472,15 +546,29 @@ type ObjectClasser interface {
472
546
The result of these methods will be used
473
547
to populate the id and class attributes in a similar way to how Haml would apply the Ruby object references.
474
548
549
+
### Inlined Tags
550
+
**Slim Only**
551
+
552
+
GoHT supports inlining tags to keep templates as compact as possible.
553
+
554
+
```slim
555
+
ul
556
+
li: a.First Fist Item
557
+
li: a.Second Second Item
558
+
li: a.Third Third Item
559
+
```
560
+
475
561
### Filters
476
562
Only the following Haml filters are supported:
477
-
-`:plain`
478
-
-`:escaped`
479
-
-`:preserve`
563
+
-`:plain` (Haml Only)
564
+
-`:escaped` (Haml Only)
565
+
-`:preserve` (Haml Only)
480
566
-`:javascript`
481
567
-`:css`
482
568
483
569
### Whitespace Removal
570
+
**Haml Only**
571
+
484
572
GoHT supports the removal of whitespace between tags. This is done by adding a `>` or `<` to the end of the tag.
485
573
486
574
-`>` will remove all whitespace between the tag and its parent or siblings.
@@ -491,20 +579,20 @@ Both can be used together to remove whitespace both inside and outside a tag; th
491
579
### Template nesting
492
580
The biggest departure from Haml is how templates can be combined. When working Haml you could use `= render :partial_name` or `= haml :partial_name` to render a partial. The `render` and `haml` functions are not available in GoHT, instead you can use the `@render` directive.
493
581
```haml
494
-
@goht HomePage() {
582
+
@haml HomePage() {
495
583
= @render SiteLayout()
496
584
}
497
585
```
498
586
The above would render the `SiteLayout` template, and you would call it with any parameters that it needs. You can also call it and provide it with a block of content to render where it chooses.
499
587
```haml
500
-
@goht HomePage() {
588
+
@haml HomePage() {
501
589
= @render SiteLayout()
502
590
%p This is the home page for GoHT.
503
591
}
504
592
```
505
593
Any content nested under the `@render` directive will be passed into the template that it can render where it wants using the `@children` directive.
0 commit comments