mirror of
https://github.com/cupcakearmy/cometa.git
synced 2025-03-12 14:27:28 +00:00
Express support
This commit is contained in:
parent
4e0745c146
commit
c37f82b27e
16
app.ts
Normal file
16
app.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import * as express from 'express'
|
||||
|
||||
const app = express()
|
||||
|
||||
app.set('views', './views')
|
||||
app.set('view engine', 'blitz')
|
||||
app.engine('blitz', require('./src/blitz')._express)
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.render('new', {
|
||||
myVar: 'whuup whuup',
|
||||
arr: [{ test: true }, { test: false }],
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(3000, () => console.log('Example app listening on port 3000!'))
|
69
src/app.ts
69
src/app.ts
@ -1,69 +0,0 @@
|
||||
import * as path from 'path'
|
||||
import * as util from './util'
|
||||
import * as parser from './parser'
|
||||
import * as compiler from './compiler'
|
||||
import { Compiled, LOG_TYPE, options } from './options'
|
||||
|
||||
const cache: Map<string, Compiled> = new Map()
|
||||
|
||||
function logger(type: LOG_TYPE, msg: object | string): void {
|
||||
if (typeof msg === 'object')
|
||||
msg = JSON.stringify(msg)
|
||||
|
||||
let typeString: string = ''
|
||||
|
||||
switch (type) {
|
||||
case LOG_TYPE.Info:
|
||||
typeString = 'Info'
|
||||
break
|
||||
case LOG_TYPE.Warning:
|
||||
typeString = 'Warning'
|
||||
break
|
||||
case LOG_TYPE.Error:
|
||||
typeString = 'Error'
|
||||
break
|
||||
}
|
||||
|
||||
console.log(`${typeString}:`, msg)
|
||||
}
|
||||
|
||||
async function compile(html: string): Promise<Compiled> {
|
||||
return {
|
||||
template: compiler.process(html),
|
||||
hash: await util.checksum(html, true),
|
||||
time: Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
async function render(template_name: string, data?: any): Promise<string> {
|
||||
// const compiled_path = path.join(options.compiled_dir, `${template_name}.${options.compiled_ext}`)
|
||||
const template_path = path.join(options.template_dir, `${template_name}.${options.template_ext}`)
|
||||
|
||||
// Compile Template if is not in cache
|
||||
if (options.caching && !cache.get(template_name)) {
|
||||
const html = await util.readFile(template_path)
|
||||
if (html !== undefined)
|
||||
cache.set(template_name, await compile(html))
|
||||
else {
|
||||
logger(LOG_TYPE.Error, 'No file found')
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
// Render the template and return the html
|
||||
const compiled = cache.get(template_name)
|
||||
if (compiled)
|
||||
return parser.computeParts(compiled.template, data)
|
||||
else
|
||||
return ''
|
||||
}
|
||||
|
||||
async function go() {
|
||||
const ret = await render('new', {
|
||||
arr: [{ test: true }, { test: false }]
|
||||
})
|
||||
|
||||
ret.log()
|
||||
}
|
||||
|
||||
go()
|
55
src/blitz.ts
Normal file
55
src/blitz.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import * as path from 'path'
|
||||
import * as util from './util'
|
||||
import * as parser from './parser'
|
||||
import * as compiler from './compiler'
|
||||
import { Compiled, options } from './options'
|
||||
|
||||
const cache: Map<string, Compiled> = new Map()
|
||||
|
||||
function compile(html: string): Compiled {
|
||||
return {
|
||||
template: compiler.process(html),
|
||||
time: Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
export function renderFile(file: string, data: any, callback: (err: any, render: string) => void): void {
|
||||
util.readFile(file).then(html => {
|
||||
util.checksum(html, true).then(hash => {
|
||||
// Compile Template if is not in cache
|
||||
if (options.caching && !cache.get(html))
|
||||
cache.set(html, compile(html))
|
||||
|
||||
// Render the template and return the html
|
||||
const compiled = cache.get(html)
|
||||
if (compiled)
|
||||
callback(null, parser.computeParts(compiled.template, data))
|
||||
else
|
||||
callback('Error: Chache not found', '')
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export async function render(template_name: string, data?: any): Promise<string> {
|
||||
const template_path = path.join(options.template_dir, `${template_name}.${options.template_ext}`)
|
||||
|
||||
// Compile Template if is not in cache
|
||||
if (options.caching && !cache.get(template_name)) {
|
||||
const html = await util.readFile(template_path)
|
||||
if (html !== undefined)
|
||||
cache.set(template_name, compile(html))
|
||||
else {
|
||||
'No file found'.log()
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
// Render the template and return the html
|
||||
const compiled = cache.get(template_name)
|
||||
if (compiled)
|
||||
return parser.computeParts(compiled.template, data)
|
||||
else
|
||||
return ''
|
||||
}
|
||||
|
||||
export const _express = renderFile
|
@ -21,7 +21,6 @@ export type ActionFunction = (part: Render) => ActionReturn
|
||||
|
||||
export interface Compiled {
|
||||
template: Part[]
|
||||
hash: string
|
||||
time: number
|
||||
}
|
||||
|
||||
|
2
views/index.blitz
Normal file
2
views/index.blitz
Normal file
@ -0,0 +1,2 @@
|
||||
#title#
|
||||
#message#
|
@ -1,5 +1,5 @@
|
||||
<ul>
|
||||
{{* i in arr}}
|
||||
<li>{{? i.test }} {{i}} {{/?}}</li>
|
||||
{{/*}}
|
||||
{{/*}} {{# some comment #}} {{myVar}}
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user