PHP 嵌套模板解析器

发布于 2024-11-25 12:52:45 字数 627 浏览 2 评论 0原文

我需要一个能够解析以下格式的 PHP 模板解析器:

{users}
    {name}
    {age}
    {posts}
        {title}
        {body}
        {comments}
            {comtitle}
            {combody}
        {/comments}
    {/posts}
{/users}

我研究过的所有模板解析器...要么递归性不够,要么带有不必要的模板构造,这些模板构造看起来与 PHP 语法如此相似,以至于不值得。这是 CodeIgniter 模板的默认工作方式,但它们挖掘得不够深入。

不管你是否相信,这篇博文中的解析器似乎都无法做这个。有些可以实现它,但使用的语法接近 PHP 的本机 foreach 函数。

你知道解析器是否存在吗? 或者您是否创建了一个可以读取此类结构的系统? 如果提供了更多嵌套的数组,所需的解析器应该能够无限地进一步挖掘。

具体来说,我正在寻找 CodeIgniter 解决方案,但此时任何 PHP 解决方案都足够了,因为我已经对此进行了几天的研究。

谢谢!

I need a PHP template parser that is capable of parsing the following format:

{users}
    {name}
    {age}
    {posts}
        {title}
        {body}
        {comments}
            {comtitle}
            {combody}
        {/comments}
    {/posts}
{/users}

All template parsers I've looked into... are either not recursive enough or come with unnecessary template constructs that look so similar to PHP syntax that it's not worth it. This is the default way CodeIgniter templates work, but they do not dig down deep enough.

Believe it or not, none of the parsers in this blog post appear to be able to do this. Some can achieve it, but with a syntax that approaches PHP's native foreach function.

Do you know if a parser exists?
Or have you created one that reads this type of structure?
The desired parser should be able to dig down infinitely further provided that an even more nested array was provided to it.

Specifically, I'm looking for a CodeIgniter solution, but any PHP solution will suffice at this point as I've been investigating this for a few days now.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情域 2024-12-02 12:52:45

正如所写,该格式看起来无法解析。无法区分具有匹配 {/tag}(例如 {posts})的 {tag} 和获胜的 {tag} 't(如 {body})。

不要试图重新发明轮子。除了 PHP 本身之外,还有很多优秀的模板解析器。除非您真的优秀,否则您提出的任何内容都可能不会像现有的那样功能强大,也不会那么快。

As written, that format looks impossible to parse. There's no way to distinguish between a {tag} which will have a matching {/tag} (like {posts}) and one which won't (like {body}).

Don't try to reinvent the wheel. There are a lot of good template parsers out there, along with PHP itself. Unless you're really good, anything you come up will probably not be as featureful, nor as fast, as what's out there already.

撩发小公举 2024-12-02 12:52:45

我决定完全放弃第三方模板解析器并选择使用 PHP 的本机支持。与兼容性、深层嵌套、条件、循环相关的头痛,所有这些都在我自己的规范内,这是不值得的。

从现在开始,我将简要教我的设计师复杂视图的循环和条件。对于任何使用 CodeIgniter 的人,我强烈建议启用短标签并利用 CodeIgniter 的可选语法用于回显数据:

而不是:

人们可以写:

看看这与标准模板语言有多接近:{variable}

开始使用PHP 的控制结构的替代语法,这样您的代码就不会散布着echo 语句和 HTML 的长 PHP 字符串。

IMO 这是 模板解决方案。尽管你的设计师会讨厌你引入:

ifelseifendifforeachendforeach,与他们进行一个小时的讨论,解释他们如何使用示例,这将为您的观点提供最大的灵活性。

发疯吧!

I've decided to completely give up on third-party template parsers and opt for using PHP's native support. The headache associated with compatibility, deep nesting, conditionals, loops, all within my own specification is not worth it.

From this point forward, I will be briefly teaching my designers loops and conditionals for complicated views. For anybody using CodeIgniter, I strongly recommend enabling short-tags and taking advantage of CodeIgniter's Optional Syntax for echoing out data:

Instead of: <?php echo $variable; ?>

One can write: <?=$variable?>

Look how close this approaches a standard template language: {variable}

Start making use of PHP's Alternative Syntax For Control Structures so that your code is not littered with echo statements and long PHP strings of HTML.

IMO this is the templating solution. Though your designers will hate you for introducing:

if, elseif, endif, foreach, and endforeach, an hour discussion with them explaining how they work with examples will give you the most flexibility in your views.

Go nuts!

内心旳酸楚 2024-12-02 12:52:45

“我没有测试来支持这一点,但我认为这对计算机来说是一个笑话。” @乔丹阿塞诺

乔丹,我同意。尽管如果您同时要解析相当多的文件,那么它们加起来可能会非常糟糕。如果您有驱动器/特殊需求,我同意您拥有自己的模板解析器。尽管使用稍微不同的语法可以在解析过程中节省大量处理时间...

而不是使用这个版本:

{users}
    {name}
    {age}
    {posts}
        {title}
        {body}
        {comments}
            {comtitle}
            {combody}
        {/comments}
    {/posts}
{/users}

您也可以使用类似的东西:

{foreach: users}
    {name}
    {age}
    {foreach: posts}
        {title}
        {body}
        {foreach: comments}
            {comtitle}
            {combody}
        {endforeach: comments}
    {endforeach: posts}
{endforeach: users}

这样在查找 {foreach:; } 模式,您就会知道有一个匹配的标签,并且您不必尝试为所有模式找到一个匹配的标签。然后,您可以轻松地使用 preg_match() 来完成任务。

(注意:您仍然可以对您的样式执行相同的操作,尽管您必须向后解析代码,我想这会变得非常不方便。)

"I don't have tests to back this up but I would assume this is a joke for a computer." @Jordan Arseno

Jordan, I agree. Although if you have quite a few files to parse at the same time, it can all add up pretty badly. I agree with you having your own template parser if you have the drive / special needs for that. Although using a little bit different syntax can save you a lot of processing time during parsing...

Instead of using this version:

{users}
    {name}
    {age}
    {posts}
        {title}
        {body}
        {comments}
            {comtitle}
            {combody}
        {/comments}
    {/posts}
{/users}

You could just as well use something like:

{foreach: users}
    {name}
    {age}
    {foreach: posts}
        {title}
        {body}
        {foreach: comments}
            {comtitle}
            {combody}
        {endforeach: comments}
    {endforeach: posts}
{endforeach: users}

This way when finding the {foreach:<array>} pattern, you will KNOW that there is a matching tag, and you won't have to TRY to find one for all patterns. You can then easily use preg_match() to make your way through.

(Note: you can still do the same with your style, although you will have to parse the code backwards which would make it really inconvenient I guess.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文