tags:

views:

172

answers:

2

I use PHP's EOF string to format HTML content without the hassle of having to escape quotes etc. How can I use the function inside this string?

<?php
    $str = <<<EOF
    <p>Hello</p>
    <p><?= _("World"); ?></p>
EOF;
    echo $str;
?>
+5  A: 

As far as I can see in the manual, it is not possible to call functions inside HEREDOC strings. A cumbersome way would be to prepare the words beforehand:

<?php

    $world = _("World");

    $str = <<<EOF
    <p>Hello</p>
    <p>$world</p>
EOF;
    echo $str;
?>

a workaround idea that comes to mind is building a class with a magic getter method.

You would declare a class like this:

class Translator
{
 public function __get($name) {
  return _($name); // Does the gettext lookup
  }
 }

Initialize an object of the class at some point:

  $translate = new Translator();

You can then use the following syntax to do a gettext lookup inside a HEREDOC block:

    $str = <<<EOF
    <p>Hello</p>
    <p>{$translate->World}</p>
EOF;
    echo $str;
?>

$translate->World will automatically be translated to the gettext lookup thanks to the magic getter method.

To use this method for words with spaces or special characters (e.g. a gettext entry named Hello World!!!!!!, you will have to use the following notation:

 $translate->{"Hello World!!!!!!"}

This is all untested but should work.

Update: As @mario found out, it is possible to call functions from HEREDOC strings after all. I think using getters like this is a sleek solution, but using a direct function call may be easier. See the comments on how to do this.

Pekka
Great answer, cheers.
FFish
@FFish you're welcome. It's untested so I can't give you a total guarantee but as far as I can see, this will work. See my latest update (regarding spaces and special characters) for how to deal with more complex gettext identifiers
Pekka
Wow, pretty tricky/scheming use of getters.
mario
Turns out you can use function calls. With `$_="_";` and `{$_('text')}`. But the getter syntax is +1.
mario
@mario oh wow, didn't know that! That would be worth a separate answer IMO.
Pekka
Mario, {$_("World")} does not work for me??
FFish
Can someone use Mario's syntax in my example please. I don't get it..
FFish
Thanks for the reply Mario. Here is what was missing: $_ = "gettext"; // assigns function reference for HEREDOC use. Than {$_("World")} works inside the HEREDOC string!
FFish
A: 

As far as I can see, you just added heredoc by mistake
No need to use ugly heredoc syntax here.
Just remove it and everything will work:

<p>Hello</p>
<p><?= _("World"); ?></p>
Col. Shrapnel
I don't think "Hello World" is an excerpt from the OP's actual production code.
Pekka
@Pekka it doesn't matter. Heredoc is still useless no matter of size
Col. Shrapnel
To me, there is a number of situations where using Heredoc is a fine alternative to using a template engine of some sort. The only big downside to it in my opinion is that the end marker can't be indented, which tends to screw up indented code classes
Pekka
What are your arguments against Heredoc apart from that it is ugly? I can't see any devastating downside to it, but I'm always open to learn
Pekka
@Pekka look at the code. it has PHP tags in it. I think heredoc were added by accident. And while heredoc can be an *alternative*, plain and native HTML is always preferred. isn't it obvious?
Col. Shrapnel
@Col I totally agree, but the code the OP presented is merely an *example* to explain the question, isn't it?
Pekka
@Col You know I mostly agree with you on the issue of [bad practice answers](http://meta.stackoverflow.com/questions/64227/bad-practice-answers-why-closed) (mostly, not entirely, because I think people are entitled to know how to use Na and Cl separately) but in this specific question, there is no indication of what the OP is actually using Heredoc for. So if you are saying "Heredoc is *always* bad", I'm open to learn if you have good arguments. If you're not saying that, I don't see how your answer has any value to the question.
Pekka
Especially since the OP explicitly says he wants to use Heredoc: `I use PHP's EOF string to format HTML content without the hassle of having to escape quotes etc` ... but it's no big deal either way, I'm just splitting hairs on a sunday afternoon :)
Pekka
@Pekka i see no point in your argues. Downvote my answer and leave me alone. Unlike you I am reading not only *statement* `use PHP's EOF string` but also *reasoning* `without the hassle of having to escape quotes`. And Plain HTML is even better because it lets you format HTML content without the hassle of heredoc either. Large blocks of HTML are always better as HTML, not PHP code. If you don't see it its up to you.
Col. Shrapnel
@Col I do see that and agree, but it's not the OP's question. Anyway - it doesn't matter either way.
Pekka