如何解析django样式模板标签

What's the best way to parse django style template tags in PHP? I know there are plenty of templating libraries for PHP, but I literally just need to parse one tag when I retrieve data from my database.

The style of tag I want to parse looks like {{ form | form_name }}

REGEX is the way to go.

\{\{ *form *\| *(\S+) *\}\}

should do the trick nicely.


A bit of explaining:

  • \{ and \} will each match { and } respectively.
  • * will match for 0 to infinite space characters ().
  • (\S+) will match for 1 to infinite non-space characters (a, B, 3, _ etc).