COMMUNITY

Creating a JSX-like DSL that compiles into PHP?

TL;DR: Third paragraph onwards.

Hello everyone,

I have recently found a job where they use PHP. They fortunately use the newest versions of it, but unfortunately they use it in a very old-school way - the templates are just HTML mixed with PHP code. I find it really painful working with code like that, so I have started looking for a way to improve it going forward.

Lately, I have been using React in my free time and I really enjoy how function components and the JSX syntax work. My first attempt at improvement was just cutting up the template I’m working on into separate functions, but I have quickly found the limitations of this approach - many of the “function components” require unwieldy arrays of parameters (which I need to manually document for each function) and passing in child components is extremely clunky too.

By chance, I was reminded of Haxe by a video and I realised that since it can compile into PHP and has metaprogramming, maybe it could be used to create a JSX-like DSL that would compile into PHP function calls similar to React.createElement() (which is what JSX is under the hood).

Well, in the previous paragraph I have used a lot of big words and short acronyms, but I am actually a junior programmer at best, and I don’t have any experience with metaprogramming, I have merely read about it. I would appreciate if someone could tell me if my thoughts have merit or if I’m completely off the mark, whether or not Haxe would be a good language to implement it, and maybe even point me towards some resources I could use.

Thanks!

1 Like

I’m very new, too… just signed-into the forum for the very first time :slight_smile:

I wonder if MassiveInteractive’s React JS project could be helpful in working towards a JSX-like system… It would be really nice for a portable JSX-style thing that supports many/all of the Haxe targets.

There’s coconut.ui by @back2dos, but I’m not sure it supports PHP target.

1 Like

Please take a look at https://github.com/haxetink/tink_hxx. As I know jsx-generated expression can be customized.

Thanks for the responses!

At a glance the libs diddledan and RealyUniqueName have recommended don’t seem to be what I’m looking for - the ReactJS project seems to provide interop with React for Haxe and coconut seems to be a React-style SPA library. I don’t want to actually use React or create an SPA, I just want to use the JSX syntax to make writing “plain” PHP templates more pleasant and I thought Haxe had the correct feature set to implement something like that.

Haxetink seems to have elements of what I had in mind, I’ll need to investigate it further. Thanks for linking it, alex_dja.

You are welcome. Here you can also see the usage of tink.hxx.Parser in haxe-react-next lib but take in mind macros aren’t trivial thing;)