Is it possible to hook Lua function calls? If so, I'd make a template expansion a "call" to a function with that name. That was the interface I envisioned when thinking how I'd do it if making the language from scratch to suit wikitext (I drafted some code, but didn't reach to a barely mature level).