All Projects → DoubleSpout → lua-resty-aries

DoubleSpout / lua-resty-aries

Licence: MIT License
openresty and lua multi-function template

Programming Languages

lua
6591 projects
shell
77523 projects
HTML
75241 projects

Projects that are alternatives of or similar to lua-resty-aries

Jade
Jade.go - pug template engine for Go (golang)
Stars: ✭ 251 (+434.04%)
Mutual labels:  template-engine, templates
dry
Dry is a new template engine and language, and is a superset of Shopify's Liquid, with first-class support for advanced inheritance features, and more. From the creators of Enquirer, Assemble, Remarkable, and Micromatch.
Stars: ✭ 66 (+40.43%)
Mutual labels:  template-engine, templates
liquid
A Python engine for the Liquid template language.
Stars: ✭ 40 (-14.89%)
Mutual labels:  template-engine, templates
Jinja
A very fast and expressive template engine.
Stars: ✭ 8,170 (+17282.98%)
Mutual labels:  template-engine, templates
redis cluster
a openresty nginx lua redis cluster
Stars: ✭ 26 (-44.68%)
Mutual labels:  openresty, resty
Handlebars.java
Logic-less and semantic Mustache templates with Java
Stars: ✭ 1,204 (+2461.7%)
Mutual labels:  template-engine, templates
aspect
Aspect is a compiling template engine for Lua and LuaJIT
Stars: ✭ 17 (-63.83%)
Mutual labels:  luajit, resty
Jinja2cpp
Jinja2 C++ (and for C++) almost full-conformance template engine implementation
Stars: ✭ 257 (+446.81%)
Mutual labels:  template-engine, templates
nunjucks-loader
Webpack loader for Nunjucks templates
Stars: ✭ 20 (-57.45%)
Mutual labels:  template-engine, templates
lua-resty-couchbase
Lua couchbase client driver for the ngx_lua based on the cosocket API / 使用cosocket纯lua实现的couchbase的client,已经在爱奇艺重要的服务播放服务稳定运行5年多
Stars: ✭ 77 (+63.83%)
Mutual labels:  luajit, resty
Mikado
Mikado is the webs fastest template library for building user interfaces.
Stars: ✭ 323 (+587.23%)
Mutual labels:  template-engine, templates
cdn-up-and-running
CDN Up and Running - an introduction about how modern CDNs works
Stars: ✭ 131 (+178.72%)
Mutual labels:  luajit, openresty
Email Templates
📫 Create, preview, and send custom email templates for Node.js. Highly configurable and supports automatic inline CSS, stylesheets, embedded images and fonts, and much more!
Stars: ✭ 3,291 (+6902.13%)
Mutual labels:  template-engine, templates
Pongo2
Django-syntax like template-engine for Go
Stars: ✭ 2,111 (+4391.49%)
Mutual labels:  template-engine, templates
Cookie
A Template-based File Generator. Like cookiecutter but works with file templates instead of project templates.
Stars: ✭ 261 (+455.32%)
Mutual labels:  template-engine, templates
nginx-lua
Nginx 1.19+ with LUA support based on Alpine Linux, Amazon Linux, Debian, Fedora and Ubuntu.
Stars: ✭ 112 (+138.3%)
Mutual labels:  luajit, openresty
Lua Resty Jit Uuid
Fast and dependency-free UUID library for LuaJIT/ngx_lua
Stars: ✭ 169 (+259.57%)
Mutual labels:  luajit, openresty
Lua Resty Redis Connector
Connection utilities for lua-resty-redis
Stars: ✭ 186 (+295.74%)
Mutual labels:  luajit, openresty
lua-resty-ipcidr
A simple and very fast function to check against CIDR
Stars: ✭ 17 (-63.83%)
Mutual labels:  luajit, openresty
phi
an api-gateway based on openresty
Stars: ✭ 23 (-51.06%)
Mutual labels:  luajit, openresty

lua-resty-aries

openresty and lua multi-function template, it can correct show your error line.

Licence Build Status Coverage Status

You can use lua-resty-aries to render template and safety run lua code string.The template or code string can be from any kind of data source, such as: file, redis, mysql or mongodb, any you like. And lua-resty-aries can correct postion your template's error line.

support openresty1.0.6+, lua5.1+

lua-resty-aries support linux/ubantu, windows and mac.

you need install lua/luajit first http://www.lua.org/

Install

with luarocks

luarocks install lua-resty-aries

Get Started

local Aries = require("resty.aries")
local aries1 = Aries:new()

local result, err = aries1:compile([=[ 
		<% hello = "welcome to lua-resty-aries" %>
		<h1><center><%= hello %></center></h1>
 ]=])

print(result)	-- <h1><center><%= hello %></center></h1>

Using file template

We create index.html file at {workdir}/tpl/index.html

<!DOCTYPE html>
<html>
<head>
	<title><%= ctx.title %></title>
</head>
<body>
	<% hello = "welcome to lua-resty-aries" %>
	<h1><center><%= hello %></center></h1>
</body>
</html>

We can render the template like this:

local Aries = require("resty.aries")
local aries1 = Aries:new()

local result, err = aries1:render("index", {
	title="lua-resty-aries title"
})

print(result)

A littel complex example

We create tpl/index2.html like this

<!DOCTYPE html>
<html>
<head>
	<title><%= ctx.title %></title>
</head>
<body>
	<% hello = "welcome to lua-resty-aries" %>
	<h1><center><%= hello %></center></h1>
	<% if (ctx.loop or 0) > 0 then %>
		<% for i=1,ctx.loop,1 do %>
			<% include inc/loop %>
		<% end %>
	<% else %>
		<% include inc/noloop %>
	<% end %>
</body>
</html>

create inc/noloop.html

<h1>ctx noloop</h1>
this must be occur an error, undefined function <% ctx.error() %>
ctx.loop=<%= ctx.loop %>

create inc/loop.html

<h1>ctx doloop</h1>
ctx.loop=<%= ctx.loop %>

create render code:

local Aries = require("resty.aries")
local aries1 = Aries:new()

local result, err = aries1:render("index2", {
	title="lua-resty-aries title",
    loop=10,	--change loop to control loop times
})

print(result)

if we change render code like this, this must be occur an error:

local Aries = require("resty.aries")
local aries1 = Aries:new()

local result, err = aries1:render("index2", {
	title="lua-resty-aries title",
    loop=-1,	--not loop make an errror
})

print(err)	-- index2.html: 14 >> inc/noloop.html: 2 have error  attempt to call field 'error' (a nil value)

we can get error msg, correct to postion the error line, even it at include template:

index2.html: 14 >> inc/noloop.html: 2 have error  attempt to call field 'error' (a nil value)

Aries Constructor and Instance

Aries Constructor method. Every time, you call Aries:new(opt) will return a new Aries instance.

local Aries = require("resty.aries")
local ariesInstance = Aries:new(opt)

opt all attribute, all of these attributes are option:

-- below is the default value
opt = {
	-- custom left half tag
	startTag = "<%",

	-- custom right half tag
	endTag = "%>",

	-- custom the template file's suffix 
	fileSuffix = "html",

	-- change root of template folder path
	rootPath = "./tpl",

	-- works fine at linux and windows
	sep = "/",

	-- if occur error, show the error line and msg, this must set off in production to increase performance
	isShowDetailError = true,  

	-- tpl parse cache,default is true, 
	cache = true,

	-- tpl parse cache time, default expire 30min, unit second
	cacheTime = 1800,

	-- template render timeout, unit second
	-- default not timeout
	timeout = -1,

	-- instance level object to put to template
	ctx = {},

	-- change template data source, you can get template from redis or other data source.
	-- default is from file system
	getInclude = function(self, includeName)
					-- simple example get template from redis
					-- local data, err = redis_conn:get(includeName)
					-- return data == ngx.null and "" or data
					return self.readfile(string.format("%s%s%s.%s", self.rootPath, self.sep, includeName, self.fileSuffix))
				end,

	-- before return string ,you can minify the html or xml string
	-- default do nothing
	minify = function(self, renderStr)
					return renderStr
			end,

}

Aries Instance Method

-- get all the include template name by name
local includeNameTable, err = ariesInstance:getIncludesByName(tplName string)

-- get all the include template name by string code
local includeNameTable, err = ariesInstance:getIncludesByContent(tplName string)

-- render a template by name
-- here ctx inherit the ariesInstance.ctx
local renderStr, err = ariesInstance:render(tplName string, [ctx table])

-- render a template by string code
-- here ctx inherit the ariesInstance.ctx
local renderStr, err = ariesInstance:compile(tplName string, [ctx table, tplName string])

Template API (using at template)

We can use include {templateName} to include other template, lua-resty-aries will call ariesInstance:getInclude({templateName}) everytime.(include first we call ariesInstance:render({templateName})). example:

<% include inc/header %>	-- notice,default not to add file suffix

print on template example:

-- will show hello world &lt; &gt; &quot; &apos;
<%= string.format("hello world %s %s %s %s", "<", ">", '"', "'") %>

-- will show hello world < > " '
<%= string.format("hello world %s %s %s %s", "<", ">", '"', "'") %>

ctx table on template:

-- like <%= {str} %>
ctx.print(str string)

-- like <%- {str} %> 
ctx.rawPrint(str string)

-- lock some ctx attribute, before excute ctx.unlock({field}), you can't change such {field} value
-- example:
-- ctx.a = 1
-- ctx.lock(a)
-- ctx.a = 2 -- occur error
ctx.lock({field})


-- unlock field
-- example
-- ctx.a = 1
-- ctx.lock(a)
-- ctx.unlock(a)
-- ctx.a = 2 -- ok
ctx.unlock({field})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].