{"id":13394031,"url":"https://github.com/eknkc/amber","last_synced_at":"2025-05-15T06:06:54.667Z","repository":{"id":5299163,"uuid":"6480193","full_name":"eknkc/amber","owner":"eknkc","description":"Amber is an elegant templating engine for Go Programming Language, inspired from HAML and Jade","archived":false,"fork":false,"pushed_at":"2020-10-13T09:28:15.000Z","size":99,"stargazers_count":917,"open_issues_count":24,"forks_count":62,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-04-14T10:42:35.318Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eknkc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-31T20:27:24.000Z","updated_at":"2025-04-13T18:50:47.000Z","dependencies_parsed_at":"2022-09-09T10:00:39.436Z","dependency_job_id":null,"html_url":"https://github.com/eknkc/amber","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eknkc%2Famber","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eknkc%2Famber/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eknkc%2Famber/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eknkc%2Famber/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eknkc","download_url":"https://codeload.github.com/eknkc/amber/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254283339,"owners_count":22045140,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:01:06.492Z","updated_at":"2025-05-15T06:06:49.657Z","avatar_url":"https://github.com/eknkc.png","language":"Go","funding_links":[],"categories":["开源类库","模板引擎","Go","HTML template engines","Open source library","Template Engines","模板引擎`模版渲染和模版生成处理库`","Relational Databases","\u003cspan id=\"模板引擎-template-engines\"\u003e模板引擎 Template Engines\u003c/span\u003e"],"sub_categories":["模板引擎","交流","Template Engine","Advanced Console UIs","HTTP Clients","查询语","高级控制台界面","Middlewares","高級控制台界面","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# amber [![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](http://godoc.org/github.com/eknkc/amber) [![Build Status](https://travis-ci.org/eknkc/amber.svg?branch=master)](https://travis-ci.org/eknkc/amber)\n\n## Notice\n\u003e While Amber is perfectly fine and stable to use, I've been working on a direct Pug.js port for Go. It is somewhat hacky at the moment but take a look at [Pug.go](https://github.com/eknkc/pug) if you are looking for a [Pug.js](https://github.com/pugjs/pug) compatible Go template engine.\n\n### Usage\n```go\nimport \"github.com/eknkc/amber\"\n```\n\nAmber is an elegant templating engine for Go Programming Language\nIt is inspired from HAML and Jade\n\n### Tags\n\nA tag is simply a word:\n\n    html\n\nis converted to\n\n```html\n\u003chtml\u003e\u003c/html\u003e\n```\n\nIt is possible to add ID and CLASS attributes to tags:\n\n    div#main\n    span.time\n\nare converted to\n\n```html\n\u003cdiv id=\"main\"\u003e\u003c/div\u003e\n\u003cspan class=\"time\"\u003e\u003c/span\u003e\n```\n\nAny arbitrary attribute name / value pair can be added this way:\n\n    a[href=\"http://www.google.com\"]\n\nYou can mix multiple attributes together\n\n    a#someid[href=\"/\"][title=\"Main Page\"].main.link Click Link\n\ngets converted to\n\n```html\n\u003ca id=\"someid\" class=\"main link\" href=\"/\" title=\"Main Page\"\u003eClick Link\u003c/a\u003e\n```\n\nIt is also possible to define these attributes within the block of a tag\n\n    a\n        #someid\n        [href=\"/\"]\n        [title=\"Main Page\"]\n        .main\n        .link\n        | Click Link\n\n### Doctypes\n\nTo add a doctype, use `!!!` or `doctype` keywords:\n\n    !!! transitional\n    // \u003c!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"\u003e\n\nor use `doctype`\n\n    doctype 5\n    // \u003c!DOCTYPE html\u003e\n\nAvailable options: `5`, `default`, `xml`, `transitional`, `strict`, `frameset`, `1.1`, `basic`, `mobile`\n\n### Tag Content\n\nFor single line tag text, you can just append the text after tag name:\n\n    p Testing!\n\nwould yield\n\n    \u003cp\u003eTesting!\u003c/p\u003e\n\nFor multi line tag text, or nested tags, use indentation:\n\n    html\n        head\n            title Page Title\n        body\n            div#content\n                p\n                    | This is a long page content\n                    | These lines are all part of the parent p\n\n                    a[href=\"/\"] Go To Main Page\n\n### Data\n\nInput template data can be reached by key names directly. For example, assuming the template has been\nexecuted with following JSON data:\n\n```json\n{\n  \"Name\": \"Ekin\",\n  \"LastName\": \"Koc\",\n  \"Repositories\": [\n    \"amber\",\n    \"dateformat\"\n  ],\n  \"Avatar\": \"/images/ekin.jpg\",\n  \"Friends\": 17\n}\n```\n\nIt is possible to interpolate fields using `#{}`\n\n    p Welcome #{Name}!\n\nwould print\n\n```html\n\u003cp\u003eWelcome Ekin!\u003c/p\u003e\n```\n\nAttributes can have field names as well\n\n    a[title=Name][href=\"/ekin.koc\"]\n\nwould print\n\n```html\n\u003ca title=\"Ekin\" href=\"/ekin.koc\"\u003e\u003c/a\u003e\n```\n\n### Expressions\n\nAmber can expand basic expressions. For example, it is possible to concatenate strings with + operator:\n\n    p Welcome #{Name + \" \" + LastName}\n\nArithmetic expressions are also supported:\n\n    p You need #{50 - Friends} more friends to reach 50!\n\nExpressions can be used within attributes\n\n    img[alt=Name + \" \" + LastName][src=Avatar]\n\n### Variables\n\nIt is possible to define dynamic variables within templates,\nall variables must start with a $ character and can be assigned as in the following example:\n\n    div\n        $fullname = Name + \" \" + LastName\n        p Welcome #{$fullname}\n\nIf you need to access the supplied data itself (i.e. the object containing Name, LastName etc fields.) you can use `$` variable\n\n    p $.Name\n\n### Conditions\n\nFor conditional blocks, it is possible to use `if \u003cexpression\u003e`\n\n    div\n        if Friends \u003e 10\n            p You have more than 10 friends\n        else if Friends \u003e 5\n            p You have more than 5 friends\n        else\n            p You need more friends\n\nAgain, it is possible to use arithmetic and boolean operators\n\n    div\n        if Name == \"Ekin\" \u0026\u0026 LastName == \"Koc\"\n            p Hey! I know you..\n\nThere is a special syntax for conditional attributes. Only block attributes can have conditions;\n\n    div\n        .hasfriends ? Friends \u003e 0\n\nThis would yield a div with `hasfriends` class only if the `Friends \u003e 0` condition holds. It is\nperfectly fine to use the same method for other types of attributes:\n\n    div\n        #foo ? Name == \"Ekin\"\n        [bar=baz] ? len(Repositories) \u003e 0\n\n### Iterations\n\nIt is possible to iterate over arrays and maps using `each`:\n\n    each $repo in Repositories\n        p #{$repo}\n\nwould print\n\n    p amber\n    p dateformat\n\nIt is also possible to iterate over values and indexes at the same time\n\n    each $i, $repo in Repositories\n        p\n            .even ? $i % 2 == 0\n            .odd ? $i % 2 == 1\n\n### Mixins\n\nMixins (reusable template blocks that accept arguments) can be defined:\n\n    mixin surprise\n        span Surprise!\n    mixin link($href, $title, $text)\n        a[href=$href][title=$title] #{$text}\n\nand then called multiple times within a template (or even within another mixin definition):\n\n    div\n    \t+surprise\n    \t+surprise\n        +link(\"http://google.com\", \"Google\", \"Check out Google\")\n\nTemplate data, variables, expressions, etc., can all be passed as arguments:\n\n    +link(GoogleUrl, $googleTitle, \"Check out \" + $googleTitle)\n\n### Imports\n\nA template can import other templates using `import`:\n\n    a.amber\n        p this is template a\n\n    b.amber\n        p this is template b\n\n    c.amber\n        div\n            import a\n            import b\n\ngets compiled to\n\n    div\n        p this is template a\n        p this is template b\n\n### Inheritance\n\nA template can inherit other templates. In order to inherit another template, an `extends` keyword should be used.\nParent template can define several named blocks and child template can modify the blocks.\n\n    master.amber\n        !!! 5\n        html\n            head\n                block meta\n                    meta[name=\"description\"][content=\"This is a great website\"]\n\n                title\n                    block title\n                        | Default title\n            body\n                block content\n\n    subpage.amber\n        extends master\n\n        block title\n            | Some sub page!\n\n        block append meta\n            // This will be added after the description meta tag. It is also possible\n            // to prepend someting to an existing block\n            meta[name=\"keywords\"][content=\"foo bar\"]\n\n        block content\n            div#main\n                p Some content here\n\n### License\n(The MIT License)\n\nCopyright (c) 2012 Ekin Koc \u003cekin@eknkc.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n## Usage\n\n```go\nvar DefaultOptions = Options{true, false}\nvar DefaultDirOptions = DirOptions{\".amber\", true}\n```\n\n#### func  Compile\n\n```go\nfunc Compile(input string, options Options) (*template.Template, error)\n```\nParses and compiles the supplied amber template string. Returns corresponding Go\nTemplate (html/templates) instance. Necessary runtime functions will be injected\nand the template will be ready to be executed.\n\n#### func  CompileFile\n\n```go\nfunc CompileFile(filename string, options Options) (*template.Template, error)\n```\nParses and compiles the contents of supplied filename. Returns corresponding Go\nTemplate (html/templates) instance. Necessary runtime functions will be injected\nand the template will be ready to be executed.\n\n#### func  CompileDir\n```go\nfunc CompileDir(dirname string, dopt DirOptions, opt Options) (map[string]*template.Template, error)\n```\nParses and compiles the contents of a supplied directory name. Returns a mapping of template name (extension stripped) to corresponding Go Template (html/template) instance. Necessary runtime functions will be injected and the template will be ready to be executed.\n\nIf there are templates in subdirectories, its key in the map will be it's path relative to `dirname`. For example:\n```\ntemplates/\n   |-- index.amber\n   |-- layouts/\n         |-- base.amber\n```\n```go\ntemplates, err := amber.CompileDir(\"templates/\", amber.DefaultDirOptions, amber.DefaultOptions)\ntemplates[\"index\"] // index.amber Go Template\ntemplates[\"layouts/base\"] // base.amber Go Template\n```\nBy default, the search will be recursive and will match only files ending in \".amber\". If recursive is turned off, it will only search the top level of the directory. Specified extension must start with a period.\n\n#### type Compiler\n\n```go\ntype Compiler struct {\n\t// Compiler options\n\tOptions\n}\n```\n\nCompiler is the main interface of Amber Template Engine. In order to use an\nAmber template, it is required to create a Compiler and compile an Amber source\nto native Go template.\n\n    compiler := amber.New()\n    // Parse the input file\n    err := compiler.ParseFile(\"./input.amber\")\n    if err == nil {\n    \t// Compile input file to Go template\n    \ttpl, err := compiler.Compile()\n    \tif err == nil {\n    \t\t// Check built in html/template documentation for further details\n    \t\ttpl.Execute(os.Stdout, somedata)\n    \t}\n    }\n\n#### func  New\n\n```go\nfunc New() *Compiler\n```\nCreate and initialize a new Compiler\n\n#### func (*Compiler) Compile\n\n```go\nfunc (c *Compiler) Compile() (*template.Template, error)\n```\nCompile amber and create a Go Template (html/templates) instance. Necessary\nruntime functions will be injected and the template will be ready to be\nexecuted.\n\n#### func (*Compiler) CompileString\n\n```go\nfunc (c *Compiler) CompileString() (string, error)\n```\nCompile template and return the Go Template source You would not be using this\nunless debugging / checking the output. Please use Compile method to obtain a\ntemplate instance directly.\n\n#### func (*Compiler) CompileWriter\n\n```go\nfunc (c *Compiler) CompileWriter(out io.Writer) (err error)\n```\nCompile amber and write the Go Template source into given io.Writer instance You\nwould not be using this unless debugging / checking the output. Please use\nCompile method to obtain a template instance directly.\n\n#### func (*Compiler) Parse\n\n```go\nfunc (c *Compiler) Parse(input string) (err error)\n```\nParse given raw amber template string.\n\n#### func (*Compiler) ParseFile\n\n```go\nfunc (c *Compiler) ParseFile(filename string) (err error)\n```\nParse the amber template file in given path\n\n#### type Options\n\n```go\ntype Options struct {\n\t// Setting if pretty printing is enabled.\n\t// Pretty printing ensures that the output html is properly indented and in human readable form.\n\t// If disabled, produced HTML is compact. This might be more suitable in production environments.\n\t// Defaukt: true\n\tPrettyPrint bool\n\t// Setting if line number emitting is enabled\n\t// In this form, Amber emits line number comments in the output template. It is usable in debugging environments.\n\t// Default: false\n\tLineNumbers bool\n}\n```\n\n#### type DirOptions\n\n```go\n// Used to provide options to directory compilation\ntype DirOptions struct {\n\t// File extension to match for compilation\n\tExt string\n\t// Whether or not to walk subdirectories\n\tRecursive bool\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feknkc%2Famber","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feknkc%2Famber","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feknkc%2Famber/lists"}