PHP Templo Bug

26 views
Skip to first unread message

Carlos Delgado

unread,
Dec 20, 2013, 12:07:07 PM12/20/13
to haxe...@googlegroups.com
Hi list,

I think I found a bug when using Templo and php:

Following the advice on the haxe page,
http://haxe.org/com/libs/mtwin/templo
I tried using templo with the following pattern
design.mtt :

<html>
    <head>
        <title>My title</title>
    </head>
    <body>
        <h1>My title</h1>

        <!-- assume the template which will use design.mtt fills the content variable -->
        ::raw content::
    </body>
</html>

mypage.mtt :

::use 'design.mtt'::

    ::fill content::
        <h2>My page</h2>
        some data here
    ::end::

::end::

When using the above templates witn the Loader instance.execute(context), It works, but in the ouptut I get a "null" (a string saying null) at the very top, before the text output of the page. So, it appears before any doctype I put on my page, pushing browsers to quirks mode. Any idea what can be happening? Using haxe 3.1.0, latest Templo from haxelib.

Sample code:
install Templo from haxelib
save the above described templates.

Demo.hx:

import templo.Loader;

class Demo{
public static function main(){
  Loader.BASE_DIR = php.Web.getCwd() + "/tpl/";
  Loader.TMP_DIR = php.Web.getCwd() + "/tmp/";
  Loader.MACROS = null;
  Loader.OPTIMIZED = false;

  var t = new Loader("mypage.mtt");
  var ctx = {x:0};
  php.Lib.print(t.execute(ctx));
}
}

Expected output:

My title

My page

some data here

received output:
null

My title

My page

some data here


Carlos Delgado

unread,
Dec 20, 2013, 1:00:57 PM12/20/13
to haxe...@googlegroups.com
Investigating , I found that the problem lies in the lib/Loader.class.php generated file,
public $buf is not initialized correctly, so it remains null, and in bufferPop it gets returned as 'null' string,

Since I don't know what was the intent, I made a workaround by initializing $buf to an empty string "" on execute method,
on the generated Loader.class.php file. Later will try a more permanent fix.

Carlos Delgado

unread,
Dec 20, 2013, 1:29:24 PM12/20/13
to haxe...@googlegroups.com
Yet another finding.

If the "mypage.mtt" template begins with  ::use design.mtt", I get the null,
if the templates start with even a single space, then the ::use design.mtt", I get an
extra white line before the output, but browsers seem to accept the document as standars mode.

The problem lies if the main template doesn't output anything, relying on ::use ...
and it begins with said ::use ... instruction, without spaces nor characters before.

Easier to modify my templates adding a single space at the beginning than try to hack the generated
php files. I still don't know if this is a bug or by design.

Marc Weber

unread,
Dec 21, 2013, 7:56:20 AM12/21/13
to haxelang
If you're fine with compiling the templates into the haxe application
you can also give
https://github.com/MarcWeber/haxe-macro-html-templates a try.

It allows you to write

var tags = {target: "other_windhow"};
str("
%html
%head

%body
%a(href='foo' tags)
")

Thus when it compiles its guaranteed to be almost valid HTML (all tags
will always close properly - and you get haxe type checking support)

Marc Weber
Reply all
Reply to author
Forward
0 new messages