Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I start when writing a new scripting "language"?

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.

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.

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.


Update

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.

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.

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?

like image 959
EAMann Avatar asked Jan 22 '23 10:01

EAMann


2 Answers

It sounds like what you need is a templating language that supports being extended by custom tokens. Given that PHP itself meets that need, I'm guessing you also want sandboxing of some sort.

For that, I'd suggest TWIG.

By default, it uses the same basic syntax as Django and Jinja2 for Python or Liquid for Ruby (though, while not recommended, that is configurable) and it's compiled to cached PHP for speed.

It supports sandboxing and parameter auto-escaping as well as block substitution and inheritance, you choose what variables it gets access to, and you can set up any combination you want of default and custom tokens and filters.

Smarty might also meet your needs, but I'm not sure whether it has all the aforementioned features, its syntax is, in my opinion, not as elegant, and I'm told it's more pain than it's worth.

Whatever you do, think long and hard before inventing your own templating language. It's generally a huge pain in the long run and tends to end up on on The Daily WTF next to BobX sooner or later.

Update: I get the impression you're obsessed with using namespaced XML for your templating. Is it really worth reinventing an entire templating engine just so your users can use <wpml:header /> rather than {{header}}? TWIG doesn't let users embed arbitrary scripts... just variables and flow-control constructs you've explicitly OKed.

like image 96
ssokolow Avatar answered Feb 05 '23 17:02

ssokolow


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.

like image 33
3 revs Avatar answered Feb 05 '23 17:02

3 revs