Git Product home page Git Product logo

pug's Introduction

pug

import "github.com/eknkc/pug"

Package pug.go is an elegant templating engine for Go Programming Language. It is a port of Pug template engine, previously known as Jade.

Pug.go compiles .pug templates to standard go templates (https://golang.org/pkg/html/template/) and returns a *template.Template instance.

While there is no JavaScript environment present, Pug.go provides basic expression support over go template syntax. Such as a(href="/user/" + UserId) would concatenate two strings. You can use arithmetic, logical and comparison operators as well as ternery if operator.

Please check Pug Language Reference for details: https://pugjs.org/api/getting-started.html.

Differences between Pug and Pug.go (items with checkboxes are planned, just not present yet)

  • Multiline attributes are not supported
  • &attributes syntax is not supported
  • case statement is not supported
  • Filters are not supported
  • Mixin rest arguments are not supported.
  • Mixin blocks are not supported. Go templates do not allow variable template includes so this is tricky.
  • while loops are not supported as Go templates do not provide it. We could use recursive templates or channel range loops etc but that would be unnecessary complexity.
  • Unbuffered code blocks are not possible as we don't have a JS environment. However it is possible to define variables using - var x = "foo" syntax as an exception.

Apart from these missing features, everything in the language reference should be supported.

doc.go pug.go

func CompileFile(filename string, options ...Options) (*template.Template, error)

Parses and compiles the contents of supplied filename. Returns corresponding Go Template (html/templates) instance. Necessary runtime functions will be injected and the template will be ready to be executed

func CompileString(input string, options ...Options) (*template.Template, error)

Parses and compiles the supplied template string. Returns corresponding Go Template (html/templates) instance. Necessary runtime functions will be injected and the template will be ready to be executed

func ParseFile(filename string, options ...Options) (string, error)

Parses the contents of supplied filename template and return the Go Template source You would not be using this unless debugging / checking the output. Please use Compile method to obtain a template instance directly

func ParseString(input string, options ...Options) (string, error)

Parses the supplied template string and return the Go Template source You would not be using this unless debugging / checking the output. Please use Compile method to obtain a template instance directly

type Options struct {
    // Setting if pretty printing is enabled.
    // Pretty printing ensures that the output html is properly indented and in human readable form.
    // If disabled, produced HTML is compact. This might be more suitable in production environments.
    // Default: false
    PrettyPrint bool

    // A Dir implements FileSystem using the native file system restricted to a specific directory tree.
    //
    // While the FileSystem.Open method takes '/'-separated paths, a Dir's string value is a filename on the native file system, not a URL, so it is separated by filepath.Separator, which isn't necessarily '/'.
    // By default, a os package is used but you can supply a different filesystem using this option
    Dir compiler.Dir
}

Generated by godoc2md

pug's People

Contributors

eknkc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pug's Issues

Nested classes dont compile

Im so glad you've decided to make a pure pug template parser/compiler/thingie! Amber's been a solid goto, to fill this void (for Go) for a while, but it's not pure pug.

Unfortunately I've ran into a little snag. Basically I have 2 templates:

layout.pug

doctype html
html
    head
        title Title
        block head
    body
        block content

and index.pug

extend layout

block content
    .columns
        .column
            h1 Left
        .column
            h1 Right

When I try to compile index.pug, I get this error:

Error #01: parse error: templates/index.pug, [line 5, col 0]: interface conversion: interface {} is nil, not string

So it seems it handles the first class, but not the second.

If I prefix classes with div it works like a charm though, so there's an easy work around for now.

&& in a conditional doesn't work

If I use logical AND in an if statement, it isn't treated as a conditional, but instead gets turned into tags.

You can try this out by first doing

if true
    p Hello

And it compiles to

<p>Hello</p>

However if you change it to

if true && true
    p Hello

It compiles to

<if>true && true<p>Hello</p></if>

Extends/Extend doesn't work

Tested with extend and doesn't work.

[line 2, col 1]: no match found, expected: "!=", "#", "+", "-", ".", "//", "<", "=", "\x02", "block", "doctype", "each", "extend", "if", "include", "mixin", "unless", "|", [ \t], [\n], [_a-zA-Z] or EOF

Even i already have extend at the beginning of the file.

error with data in struct

go version go1.10.3 linux/386

this code

package main
import (
  "fmt"
  "os"
  "github.com/eknkc/pug"
)

type index_page struct {
  title string
  host string
  A string
  a string
}

func main() {
  tmpl, err := pug.CompileFile("test")
  if err != nil {
    fmt.Println(err)
    fmt.Println("\n-----\n")
  }

  data := index_page{title: "TITLE", host: "HOST", A: "A", a: "a"}

  tmpl_err := tmpl.Execute(os.Stdout, &data)
  if tmpl_err != nil {
    fmt.Println("\n-----\n")
    fmt.Println(tmpl_err)
  }
}

this template

div TEST HTML :: #{A} :: #{a}

and this output what i got

<div>TEST HTML :: A ::
-----

template: test:1:33: executing "test" at <.a>: a is an unexported field of struct type *main.index_page

When use uppercase for literate in struct - all done. But when use lowercase to literate in struct - got error.

Boolean evaluation doesn't support ! (not operator)

Assume someValue = true...

These all evaluate to true:

if someValue

if !someValue

if someValue == true

if someValue == false

Is this expected behavior? How can I check if a bool is false without using an else statement?

Failing test cases

These are the issues I found in my first five minutes trying to use this library. I've also written test cases suitable for pasting into pug_test.go, I hope this is helpful for you.

Summary:

  • Single quotes in attributes (div(id='foo')) cause parse error.
  • Block comments cause parse error.
  • Buffered comments (which should appear in the output), aren't.
  • Tag interpolation seems not to be implemented.
  • Certain kinds of if are converted into <if> tags.
func Test_Attribute_SingleQuote(t *testing.T) {
		res, err := run(`div(id='foo')`, nil)

		if err != nil {
				t.Fatal(err.Error())
		} else {
				expect(res, `<div id="foo"></div>`, t)
		}
}

func Test_UnbufferedComment(t *testing.T) {
		res, err := run(`
//- This is an unbuffered comment. It should not appear in the output.
		It is also a multiline comment. This line shouldn't appear either.
`, nil)

		if err != nil {
				t.Fatal(err.Error())
		} else {
				expect(res, ``, t)
		}
}

func Test_Comment(t *testing.T) {
		res, err := run(`// This is a comment`, nil)

		if err != nil {
				t.Fatal(err.Error())
		} else {
				expect(res, `<!-- This is a comment -->`, t)
		}
}

func Test_TagInterpolation(t *testing.T) {
		res, err := run(`p This text is #[strong important!]`, nil)

		if err != nil {
				t.Fatal(err.Error())
		} else {
				expect(res, `<p>This text is <strong>important!</strong></p>`, t)
		}
}

func Test_If(t *testing.T) {
	res, err := run(`
if Key == "foo"
	| foo
else
 | bar
if Key == 'foo'
	|baz
`, testStruct{Key: "foo"})

	if err != nil {
		t.Fatal(err.Error())
	} else {
		expect(res, `foobaz`, t)
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.