How to parse django style template tags

若如初见. 提交于 2019-12-04 05:03:17

问题


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 }}


回答1:


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).


来源:https://stackoverflow.com/questions/7650833/how-to-parse-django-style-template-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!