Where do I start writing a new scripting language?

I need to write a basic script / template engine that will work under PHP. Ideally, I could 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 HTML code from the database.

Unfortunately, despite all my knowledge of PHP and scripts, I'm not quite sure where to start. My first instinct was to run the entire document through some sort of regular expression parser and display my own markup for certain PHP functions ... but this seems a bit slow and hard for me.

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


Update

OK, let me clarify my explanation a bit more ... I am developing websites for WordPress. Many of my clients want to set up their websites, but run away when I start talking about PHP. It is a scripting language that looks too complicated for a simple user, even to be interested.

I want to create my own markup form specifically for WordPress. So instead of having calls to PHP functions ( get_header() and get_footer() and if(has_posts()) ...) in the theme file, you would have to have an XML namespace ( <wpml:header /> and <wpml:footer /> and <wpml:loop> ... </wpml:loop> ), which translates to the same thing. It would be better to separate the template files from the server-side script (there are several themes that place entire PHP functions directly in the PHP template files!), And itโ€™s easier for non-developers to get started with setting up a WordPress theme.

With this in mind, the already proposed solutions TWIG and Mackrell definitely support the idea of โ€‹โ€‹embedding script "nuggets" in a file, but they really do not help me parse custom XML / XHTML markup into something recognizable by server code.

So ... where to start when I create a new server-side markup processor?

+4
scripting xml php markup templates
source share
4 answers

Another option is to parse your template in an XML document and convert it to another XML document, replacing your tags with other tags (for example, <?php processing instructions). In this case, XSL is what you are looking for.

+4
source share

It looks like you need a template language that supports custom token extensions. Given that PHP itself meets this need, I assume that you also want to use the sandbox.

For this, I suggest TWIG .

By default, it uses the same basic syntax as Django and Jinja2 for Python or Liquid for Ruby (although not recommended, this is customizable), and it is compiled to cache PHP for speed.

It supports the sandbox and automatic escaping of parameters, as well as block replacement and inheritance, you choose which variables it accesses, and you can configure any combination that you want to use by default and custom markers and filters.

Smarty can also satisfy your needs, but I'm not sure if it has all of the above features, its syntax, in my opinion, is not so elegant, and they tell me that the pain is more than worth it.

Everything that you do, think long and hard before inventing your own template language. This is generally a huge pain in the long run and usually ends sooner or later at The Daily WTF next to BobX .

Update: I get the impression that you're obsessed with using XML with a name extension for templates. Is it really worth inventing the whole template engine so that your users can use <wpml:header /> rather than {{header}} ? TWIG does not allow users to embed arbitrary scripts ... just the variables and flow control constructs that you explicitly specified.

+5
source share

For custom XML, you can use the XML XML parser , preferably SAX for execution.

Smarty is a very good PHP template engine with built-in tags, blocks and functions. You can expand them to create your own and even remove the built-in ones (for Smarty 3).

If you need to create your own script, I suggest you check out a language parser like Lex and Yacc . You will need to define your language so that those SQLite images are simply not graphical, but textual. There are other grammar language parsers . Those I gave are among the oldest and most famous, but this was done for C ++.

You probably want to avoid this yourself (for example, using RegExp). You will have many inconsistencies in your script very soon. Despite the fact that RegExp is the language itself, interpreted by automation.

You can mix two: an XML parser and a common parser. Check State Machine (FSM) .

+1
source share

I would start with XML by specifying what a typical page layout would look like, and then move on to decrypting the XML in your chosen language, and then take it and create the HTML.

xml should be a link of nodes that describe your specific language.

So...

 <MyPage> <MyElement id="myid" type="MyType1"> <MyElement id="myid" type="MyType1" Text="Some text"/> </MyElement> etc... 

I would look more closely on the Internet to find out if there is something remodeled that meets your needs before embarking on something like that that has real potential to become one of those things that go out of control and impossible to maintain. .

-one
source share

All Articles