精华
1分钟搭配,HTML模板引擎 -- 瞬间 -- SQL模板引擎 [ hogan.js for mustache ]
4 回复
多个表关联呢 还有查询逻辑条件嵌套的情况呢
var tpl = [
'SELECT',
'tba.field_a, tbb.field_b, tbc.field_c, tbd.field_d',
'FROM tba',
'INNER JOIN tbb ON tbb.id = tba.b_id',
'INNER JOIN tbc ON tbc.id = tba.c_id',
'INNER JOIN tbd ON tbd.id = tbc.d_id',
'WHERE 1 = 1',
'{{#field_m}}AND tba.field_m = {{field_m}}{{/field_m}}',
'{{#field_m}}AND tbb.field_n = {{field_n}}{{/field_n}}',
'{{#field_x}}OR tbc.field_x > {{field_x}}{{/field_x}}',
'{{^field_x}}OR tbc.field_x = 0{{/field_x}}'
].join('\n');
QHOGAN.compile(tpl).render({
field_m: 'aaa',
field_n: ''
});
// =>
// SELECT ...
// WHERE 1 = 1
// AND `tba`.`field_m` = 'aaa'
// OR `tbc`.`field_x` = 0;