1

I'm using Hack, which can be found at hacklang.org.

Why doesn't the following code :

<?hh
    $message = "Hey, lol.";
?>
<!doctype html>
<html lang="en-us">
    <head>
        <title>title</title>
    </head>
    <body>
        <?= $message ?>
    </body>
</html>

output the following text?

Hey, lol.


There are no errors in the error.log file. And when I "View source", it's blank, just like the page itself.

Edit:

Guys, please see the code below in response to your answers. If you visit hacklang.org (see hack tag in this question :) and see the tutorial section on the main page, click through to the Exercise 3 of.. and it clearly shows that you're supposed to use hh and not php to define a hack document:

<?hh

// Hack functions are annotated with types.
function my_negation(bool $x): bool {
  return !$x;
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
jay_t55
  • 11,362
  • 28
  • 103
  • 174

4 Answers4

4

This should work:

<?php
    $message = "Hey, lol.";
?>
<!doctype html>
<html lang="en-us">
    <head>
        <title>title</title>
    </head>
    <body>
        <? echo $message; ?>
    </body>
</html>

EDIT: Hack cannot be used as a mix with html the way above. Try this in stead:

<?hh
    $message = "Hey, lol.";
    echo '
<!doctype html>
<html lang="en-us">
    <head>
        <title>title</title>
    </head>
    <body>
        ' . $message . '
    </body>
</html>
';
Gjermund Dahl
  • 1,410
  • 17
  • 25
  • I've updated my question in response to "use " and to just use "= $message ?>" Is there an official documentation that does actually address the basics of teh language? Because their code in the manual doesn't work. – jay_t55 Jul 16 '14 at 22:11
  • What the fuc* is this ^hit? Ah f*ck this I'm not using Hack. That's just filthy. I can't write code like that - it's horrible! Thanks for your answer and help, @gdahl. – jay_t55 Jul 16 '14 at 22:28
  • 1
    While the latter example is how you *can* output HTML with Hack, it's far from the recommended way. XHP is our preferred method at Facebook; it's also what the cookbook examples are written in: http://cookbook.hacklang.org/. If you don't want to use XHP, I've heard folks have had great luck with templating engines such as Twig. – Josh Watzman Jul 17 '14 at 16:47
1

You are using the wrong syntax for Hack. Even examples on their own website imply to use normal php constructs for what you are trying to do. Your code should be:

<?php
    $message = "Hey, lol.";
?>
<!doctype html>
<html lang="en-us">
    <head>
        <title>title</title>
    </head>
    <body>
        <?php echo $message; ?>
    </body>
</html>

Edit: It looks like Hacklang may require a separate way to produce the results. Try the following:

<?hh 
    $message = "Hey, lol.";
    echo '
        <!doctype html>
        <html lang=\"en-us\">
            <head>
                <title>title</title>
            </head>
            <body>
                ' . $message . '
            </body>
        </html>';
ihgann
  • 505
  • 3
  • 11
  • 1
    Note: There seems to be some confusion amongst members in this thread (and apparently whoever downvoted me). Hack integrates with PHP. php tags are acceptable here, and seem to be the standard from what I'm finding on the internet. However, *ideally* the code should work just the same if he replaces ` – ihgann Jul 16 '14 at 22:21
  • I didn't downvote you, but I'm beginning to regret choosing to try out Hack. So far, it feels like I've done nothing but _hack_ it all together to get something to work. But it's still not working - even when I use php stuff. – jay_t55 Jul 16 '14 at 22:23
  • 1
    I'm not a huge fan of it, and I would recommend sticking with pure php for the time being if you are doing development in that scope. What do you mean it's not working? – ihgann Jul 16 '14 at 22:25
  • The examples on their own website www.hacklang.org (see exercise 3 on the main page tutorial section) tell you to use – jay_t55 Jul 16 '14 at 22:26
  • Mmm, they have some mixed documentation across the net. My example would be for a standard PHP document. It looks like Hack requires some special information. Will edit answer. – ihgann Jul 16 '14 at 22:29
  • Looks like @gdahl beat me to it. Oh well, hope some of this helped. – ihgann Jul 16 '14 at 22:33
0

You might be better off using XHP in hack. See here for an example: https://github.com/hhvm/hack-example-site/blob/master/HomeController.php

ndavison
  • 185
  • 7
  • Nah its all good but thanks for your answer I do appreciate it. Ive already decided to give something else a go. – jay_t55 Jul 17 '14 at 09:39
-1

Does Hack support short tags? <?= ?>

That might be your issue.

JRL
  • 840
  • 6
  • 18