HHP - php-like templating system.

205 views
Skip to first unread message

Alexander Kuzmenko

unread,
Apr 15, 2015, 3:54:31 AM4/15/15
to haxe...@googlegroups.com
Hello.

Since i came to Haxe from PHP i always missed php abilities as templating engine. So i decided to create my own templating system for Haxe (yep, yet another one) which will have the same features as PHP, but with some bonus advantages of Haxe.
It's called HHP - Haxe Hypertext preprocessor :)
Install:
haxelib install hhp
Readme: https://github.com/RealyUniqueName/HHP#hhp

Here is some features demonstration.

You can use any valid Haxe code in templates:
<h1>Project: <?=this.title?></h2>
<b>Todo:</b>
<ul>
    <?hhp
        for (i in 0...this.taskAmount) {
            this.echo('<li>Task $i</li>');
        }
    ?>
</ul>

Any variables mentioned in template with `this` will be used as template variables:
var content : String = hhp.Hhp.render('todo.html', {
    title : 'My cool project',
    taskAmount : 10
});
And compiler will notify you if you try to pass unused parameters to template.

Or you can make some or all of template variables type-safe and assign default values:
@:hhp('todo.html')
class MyTemplate extends hhp.Template {
    /** `taskAmount` will only accept integer values and have default value */
    public var taskAmount : Int = 5;
    /** `title` stays Dynamic because its type is not specified here */
}

class Main {
   static public function main() {
       var tpl = new MyTemplate();
       tpl.taskAmount = 10;
       tpl.| //code completion: title, taskAmount

       var content : String = tpl.execute();
   }
}

Also you can add some additional logic to your templates:
<!-- hello.tpl -->
Hello, <?=this.selectPerson()?>

<!-- bye.tpl -->
Goodbye, <?=this.selectPerson()?>


@:hhp('hello.tpl')
class MikeHello extends hhp.Template {
   public function selectPerson() {
      return 'Mike';
   }
}

@:hhp('bye.tpl')
class MikeBye extends MikeHello { }


@:hhp('hello.tpl')
class BillHello extends hhp.Template {
   public function selectPerson() {
      return 'Bill';
   }
}

@:hhp('bye.tpl')
class BillBye extends BillHello { }


class Main{
    static public function main() {
       var tpl = new MikeHello();
       trace(tpl.execute()); //Hello, Mike

       var tpl = new MikeByte();
       trace(tpl.execute()); //Bye, Mike

           var tpl = new BillHello();
       trace(tpl.execute()); //Hello, Bill

       var tpl = new BillByte();
               trace(tpl.execute()); //Bye, Bill   
    }
}



Juraj Kirchheim

unread,
Apr 15, 2015, 8:43:50 AM4/15/15
to haxe...@googlegroups.com
Welcome to the club! ;)

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Simon Krajewski

unread,
Apr 15, 2015, 9:19:10 AM4/15/15
to haxe...@googlegroups.com
Am 15.04.2015 um 14:43 schrieb Juraj Kirchheim:
Welcome to the club! ;)

I also treaded that path a while ago and made a small macro which does something similar: https://gist.github.com/Simn/80956db41a278e88229e

What made me give up on it was that I couldn't find any way to get error reporting right. Errors in the template would give incomprehensible compiler messages where the positions were all over the place. Does hhp address this?

Simon

Alexander Kuzmenko

unread,
Apr 15, 2015, 9:33:39 AM4/15/15
to haxe...@googlegroups.com
Yes, compiler points the right lines in template files in case of errors.

среда, 15 апреля 2015 г., 16:19:10 UTC+3 пользователь Simon Krajewski написал:

Philippe Elsass

unread,
Apr 15, 2015, 10:34:30 AM4/15/15
to haxe...@googlegroups.com
I like the approach.

I'd love something like that in the browser to generate DOM structures, and even with, I dare suggest, data binding. Or it could leverage react.js.
Philippe

Alexander Kuzmenko

unread,
Apr 15, 2015, 10:50:06 AM4/15/15
to haxe...@googlegroups.com
Well, there is bindx for data binding: https://github.com/profelis/bindx2

среда, 15 апреля 2015 г., 17:34:30 UTC+3 пользователь Philippe Elsass написал:

Tarwin Stroh-Spijer

unread,
Apr 15, 2015, 9:06:04 PM4/15/15
to haxe...@googlegroups.com
Very awesome!



Tarwin Stroh-Spijer
_______________________

phone: +1 650 842 0920

Developer at Fanplayr Inc. (Palo Alto)
Original at Touch My Pixel (touchmypixel.com)
_______________________

Confidant

unread,
Apr 16, 2015, 11:40:54 AM4/16/15
to haxe...@googlegroups.com
This isn't any sort of criticism of your project, I'm just reflecting on my own journey as a coder. I had more experience with Actionscript 3 before I got more heavily into PHP, and I dislike how PHP often ends up being a complex mixture of HTML, CSS, JavaScript and PHP logic. Obviously good programming practises can avoid some of this, but even having to switch back and forth between HTML and PHP modes in a document takes a mental toll. The more abstracted approaches to templating (e.g. Ithril) are appealing to me more lately. It still looks like PHP is going to be a source of bread-and-butter for some time yet, but I keep searching for the "ultimate solution". :)

Alexander Kuzmenko

unread,
Apr 16, 2015, 12:01:16 PM4/16/15
to haxe...@googlegroups.com
I agree PHP itself spawned some really bad practices like that hell of unreadable php+html+css+js mess. But it's up to dveloper to decide whether to create such a mess or to organize some better architecture :)
Also solutions like ithril (as far as i can see from readme) targets some specific use-cases (e.g. html generation), while with HHP you can use templates for any purpose. And HHP library code is really small because i don't need to implement any template scripts parsing (like loops or `if`s) since everything is already implemented in Haxe language :)

четверг, 16 апреля 2015 г., 18:40:54 UTC+3 пользователь Confidant написал:

Juraj Kirchheim

unread,
Apr 16, 2015, 12:04:30 PM4/16/15
to haxe...@googlegroups.com
I cannot really follow this argument. Ithril and similar projects embed markup in haxe, HHP and similar projects embed haxe in markup.
Either way you open an opportunity for people to not separate their responsibilities right. But I rather trust my fellow programmers to find a way that works for them, then providing opinionated approaches that prescribe one.

I think this is neat. I would have preferred a contribution for tink_template of course, but where would be the fun in that :D

On Thu, Apr 16, 2015 at 5:40 PM, Confidant <al...@alteredegg.com> wrote:
This isn't any sort of criticism of your project, I'm just reflecting on my own journey as a coder. I had more experience with Actionscript 3 before I got more heavily into PHP, and I dislike how PHP often ends up being a complex mixture of HTML, CSS, JavaScript and PHP logic. Obviously good programming practises can avoid some of this, but even having to switch back and forth between HTML and PHP modes in a document takes a mental toll. The more abstracted approaches to templating (e.g. Ithril) are appealing to me more lately. It still looks like PHP is going to be a source of bread-and-butter for some time yet, but I keep searching for the "ultimate solution". :)

--

Confidant

unread,
Apr 17, 2015, 12:05:09 PM4/17/15
to haxe...@googlegroups.com
I don't think anybody's arguing here. Alexander has a fine tool and I agree it could be useful for a number of things.

My main point in my previous comment is that my ideal is reading a single language. I think both Ithril and Haxe-dom are good that way; I don't feel like they are "embedding" anything, but are rather "representing" the final output. There's no echoing of HTML strings, or "switching modes" within a document, which is what I would like to avoid.

Tarwin Stroh-Spijer

unread,
Apr 17, 2015, 4:22:12 PM4/17/15
to haxe...@googlegroups.com
I come from completely the opposite viewpoint. Having HTML (or any other output) created line by line in a second language just makes it incomprehensible to me.

To me a template should be as close to the original as possible, with tag outputs and some control structure/filters. Hence I love this. Not saying it is the "right" way, just that this is how I like it.



Tarwin Stroh-Spijer
_______________________

phone: +1 650 842 0920

Developer at Fanplayr Inc. (Palo Alto)
Original at Touch My Pixel (touchmypixel.com)
_______________________

On Fri, Apr 17, 2015 at 9:05 AM, Confidant <al...@alteredegg.com> wrote:
I don't think anybody's arguing here. Alexander has a fine tool and I agree it could be useful for a number of things.

My main point in my previous comment is that my ideal is reading a single language. I think both Ithril and Haxe-dom are good that way; I don't feel like they are "embedding" anything, but are rather "representing" the final output. There's no echoing of HTML strings, or "switching modes" within a document, which is what I would like to avoid.

--

Confidant

unread,
Apr 18, 2015, 6:00:26 PM4/18/15
to haxe...@googlegroups.com
I totally respect where you're coming from, and reserve the right to take that route if I ever need to do so. :)
Reply all
Reply to author
Forward
0 new messages