在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Olivine-Labs/lustache开源软件地址(OpenSource Url):https://github.com/Olivine-Labs/lustache开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):lustache - Logic-less {{mustache}} templates with Lua
lustache is an implementation of the mustache template system in Lua. Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values. For a language-agnostic overview of mustache's template syntax, see the
Where to use lustache?You can use lustache to render mustache templates anywhere you can use Lua. lustache exposes itself as a module, so you only have to require the file. and assign it. UsageInstallationDownload lustache.lua and place it in your project, or install it with luarocks
using Below is quick example how to use lustache:
In this example, the Running TestsLustache uses the busted testing framework. Run TemplatesA mustache template is a string that contains
any number of mustache tags. Tags are indicated by the double mustaches that
surround them. There are several types of tags available in lustache. VariablesThe most basic tag type is a simple variable. A All variables are HTML-escaped by default. If you want to render unescaped HTML,
use the triple mustache: Template:
View:
Output:
Dot notation may be used to access keys that are properties of objects in a view. Template:
View:
Output:
SectionsSections render blocks of text one or more times, depending on the value of the key in the current context. A section begins with a pound and ends with a slash. That is, The behavior of the section is determined by the value of the key. False Values or Empty ListsIf the Template:
View:
Output:
Non-Empty ListsIf the When the value is a list, the block is rendered once for each item in the list. The context of the block is set to the current item in the list for each iteration. In this way we can loop over collections. Template:
View:
Output:
When looping over an array of strings, a Template:
View:
Output:
If the value of a section variable is a function, it will be called in the context of the current item in the list on each iteration. Template:
View:
Output:
FunctionsIf the value of a section key is a function, it is called with the section's literal block of text, un-rendered, as its first argument. The second argument is a special rendering function that uses the current view as its view argument. Template:
View:
Output:
Inverted SectionsAn inverted section opens with Template:
View:
Output:
CommentsComments begin with a bang and are ignored. The following template:
Will render as follows:
Comments may contain newlines. PartialsPartials begin with a greater than sign, like {{> box}}. Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops. They also inherit the calling context. Whereas in ERB you may have this:
Mustache requires only this:
Why? Because the For example, this template and partial:
Can be thought of as a single, expanded template:
In lustache an object of partials may be passed as the third argument to
Set DelimiterSet Delimiter tags start with an equals sign and change the tag delimiters from
Consider the following contrived example:
Here we have a list with three items. The first item uses the default tag style, the second uses ERB style as defined by the Set Delimiter tag, and the third returns to the default style after yet another Set Delimiter declaration. According to ctemplates, this "is useful for languages like TeX, where double-braces may occur in the text and are awkward to use for markup." Custom delimiters may not contain whitespace or the equals sign. Testinglustache uses the lunit testing framework. In order to run the tests you'll need to install lunit, which can be done through luarocks or your choice of lua package manager.
Then run the tests.
Thankslustache began as a direct port of Jan Lehnardt's excellent mustache.js. It would be significantly further behind without the available code from the many contributors. <3 LicenseMIT licensed. View LICENSE file for more details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论