如何解析 django 样式模板标签

2022-01-02 00:00:00 regex django-templates php

在 PHP 中解析 django 样式模板标签的最佳方法是什么?我知道有很多 PHP 模板库,但实际上我只需要在从数据库中检索数据时解析一个标签.

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.

我要解析的标签样式看起来像 {{ form |form_name }}

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

推荐答案

REGEX 是要走的路.

REGEX is the way to go.

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

应该可以很好地解决问题.

should do the trick nicely.

稍微解释一下:

  • {} 将分别匹配 {}.
  • * 将匹配 0 到无限空格字符 ().
  • (S+) 将匹配 1 到无限个非空格字符(a, B, 3, _ etc).
  • { 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).

相关文章