re_tmpl = re.compile('<%(.*?)%>')
def template(s, d):
	return re_tmpl.sub(lambda m: str(eval(m.group(1), globals(), d)), s)
template('<%"ddd" if abc else "eee"%>', {'abc': 1})

限制挺多,只能在<%%>中写一行代码,不能多行。不能用跨区块的if for等控制结构。但是对于功能需求不复杂,需要可变性强,又不希望引入额外库的地方还是非常实用的。