在编写新的脚本“语言"时该从哪里开始? [英] Where do I start when writing a new scripting "language"?

查看:91
本文介绍了在编写新的脚本“语言"时该从哪里开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个将在PHP下运行的基本脚本/模板引擎.理想情况下,我可以将自己的标记语言与(X)HTML模板混合,并通过服务器端解析器运行文档,以用数据库外提供的(X)HTML动态替换我自己的标记.

I have a need to write a basic scripting/templating engine that will run under PHP. Ideally, I would be able to mix my own markup language with an (X)HTML template and run the document through a server-side parser to dynamically replace my own markup with (X)HTML served out of a database.

不幸的是,就我对PHP和脚本编写的全部了解,我不太确定从哪里开始.我的第一个直觉是通过某种正则表达式解析器运行整个文档,并将我的自定义标记映射到特定的PHP函数……但这对我来说似乎有点慢且繁琐.

Unfortunately, for all my knowledge of PHP and scripting, I'm not quite sure where to start. My first instinct was to run the entire document through some kind of regex parser and map my custom markup to specific PHP functions ... but that seems a bit slow and heavy-handed to me.

存在哪些资源/教程/示例可以为我指明正确的方向?为了进行比较,我真的很喜欢用于.NET MVC的新型Razor模板引擎.我不想完全将它用于PHP项目,但是构建类似的东西会很棒.

What resources/tutorials/examples exist that can point me in the right direction? For comparison, I really like the new Razor templating engine for .NET MVC ... I don't want to completely knock it off for a PHP project, but building something similar would be great.

好的,让我进一步解释一下……我为WordPress开发网站.我的许多客户都希望自定义他们的网站,但是每当我开始谈论PHP时,他们都会逃之away.这是一种脚本语言,对于外行用户来说,它看起来太复杂了,甚至根本不想引起兴趣.

OK, let me refine my explanation a bit more ... I develop websites for WordPress. A lot of my clients want to customize their websites but run away whenever I start talking about PHP. It's a scripting language that looks too complex for the lay user to even want to get interested.

我想做的是专门为WordPress创建自己的标记形式.因此,与其在主题文件中没有PHP函数调用(get_header()get_footer()if(has_posts()) ...),您将使用命名空间XML(<wpml:header /><wpml:footer /><wpml:loop> ... </wpml:loop>)转换为同样的事情.将模板文件与服务器端脚本分开会做得更好(有几个主题将整个PHP函数直接放置在主题的PHP模板文件中!!!),并使非开发人员更容易开始工作自定义WordPress主题.

What I want to do is create my own form of markup specifically for WordPress. So rather than having PHP function calls (get_header() and get_footer() and if(has_posts())...) in the theme file, you'd have namespaced XML (<wpml:header /> and <wpml:footer /> and <wpml:loop> ... </wpml:loop>) that translates to the same thing. It would do a better job of separating your template files from the server-side script (there are several themes that place whole PHP functions directly in the theme's PHP template files!!!) and would make it easier for non-developers to begin working with an customizing a WordPress theme.

考虑到这一点,已经建议的TWIG和Mackrell解决方案肯定支持在文件中嵌入脚本块"的想法,但是它们并不能真正帮助我将自定义XML/XHTML标记解析为可识别的内容.服务器端代码.

With that in mind, the already suggested solutions of TWIG and Mackrell definitely support the idea of embedding script "nuggets" in the file, but they don't really help me parse the custom XML/XHTML markup into something recognizable by the server-side code.

那么...在构建新的服务器端标记处理器时我应该从哪里开始?

So ... where do I start when building a new server-side markup processor?

推荐答案

另一个选择是将您的模板解析为xml文档,然后将其转换为另一个xml文档,其中您的自定义标签已替换为其他标签(例如,<?php处理指示).在这种情况下,您要查找的是 XSL .

Another option is to parse your template into an xml document and transform it to another xml document, with your custom tags replaced with other tags (e.g. <?php processing instructions). In this case, XSL is what you're looking for.

这篇关于在编写新的脚本“语言"时该从哪里开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆