From: Sorin Buturugeanu on
Hello,

I am having trouble with a part of my templating script. I'll try to explain:

The template itself is HTML with PHP code inside it, like:

<div><?=strtoupper($user['name']);?></div>

And I have the following code as part of the templating engine:

$template = file_get_contents($file);
$template = "return <<<TEMPLATE\n".$template."\nTEMPLATE;\n";
$template = eval($template);

The problem is that the eval() HEREDOC combination gives the following output:

<?=strtoupper(Array['time']);?>

If in the HTML file (template) I use

<div><?=strtoupper({$user['name']});?></div>

I get <?=strtoupper(username);?> as an output.

I have tried closing the php tag like this:

$template = "return <<<TEMPLATE\n?>".$template."\nTEMPLATE;\n";

but the extra ?> only gets outputed as HTML.

This is my first post to this mailing list, so I great you all and thank you for
any kind of solution to my problem.

Thank you!
From: "Daevid Vincent" on
> -----Original Message-----
> From: Sorin Buturugeanu [mailto:mail(a)soin.ro]
> Sent: Tuesday, July 20, 2010 2:11 PM
> To: php-general(a)lists.php.net
> Subject: [PHP] eval and HEREDOC
>
> Hello,
>
> I am having trouble with a part of my templating script. I'll
> try to explain:
>
> The template itself is HTML with PHP code inside it, like:
>
> <div><?=strtoupper($user['name']);?></div>
>
> And I have the following code as part of the templating engine:
>
> $template = file_get_contents($file);
> $template = "return <<<TEMPLATE\n".$template."\nTEMPLATE;\n";
> $template = eval($template);
>
> The problem is that the eval() HEREDOC combination gives the
> following output:
>
> <?=strtoupper(Array['time']);?>
>
> If in the HTML file (template) I use
>
> <div><?=strtoupper({$user['name']});?></div>
>
> I get <?=strtoupper(username);?> as an output.
>
> I have tried closing the php tag like this:
>
> $template = "return <<<TEMPLATE\n?>".$template."\nTEMPLATE;\n";
>
> but the extra ?> only gets outputed as HTML.
>
> This is my first post to this mailing list, so I great you
> all and thank you for any kind of solution to my problem.

Why are you using HEREDOC to begin with? I personally find them to be ugly
and more trouble than they're worth.

You can write the same thing as this I think (untested):

$template = eval(file_get_contents($file));

But you might also consider using "include_once" or "require_once" instead
of this "eval()" business.

Also note, that a string can span more than one line and have variables in
it. It can even be used with code, so HEREDOC is again useless for most
situations:

$foo = "
Hello $name,\n
\n
Today is ".date('Y-m-d')."\n
\n
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\n
Nulla eros purus, pharetra a blandit non, pellentesque et leo. In augue
metus, mattis a sollicitudin in, placerat vitae elit.
\n
Quisque elit mauris, varius sit amet cursus sed, eleifend a mauris.
";

From: Sorin Buturugeanu on
Hello Vincent and thank you for your reply :).

That's true, I forgot to explain how I got to using HEREDOC, so ..

Using eval(file_get_contents($file)) just outputs the result on the
spot and I need
to get the whole output (without echoing it) and do some more things with it.

require_once() doesn't fit here (from what I can tell), because it
would still just
include the file in echo the output.

I think there must be a solution, but I'm missing something here ..

Thanks again!

--
Sorin Buturugeanu
http://www.soin.ro


On Wed, Jul 21, 2010 at 12:49 AM, Daevid Vincent <daevid(a)daevid.com> wrote:
>
> > -----Original Message-----
> > From: Sorin Buturugeanu [mailto:mail(a)soin.ro]
> > Sent: Tuesday, July 20, 2010 2:11 PM
> > To: php-general(a)lists.php.net
> > Subject: [PHP] eval and HEREDOC
> >
> > Hello,
> >
> > I am having trouble with a part of my templating script. I'll
> > try to explain:
> >
> > The template itself is HTML with PHP code inside it, like:
> >
> > <div><?=strtoupper($user['name']);?></div>
> >
> > And I have the following code as part of the templating engine:
> >
> > $template = file_get_contents($file);
> > $template = "return <<<TEMPLATE\n".$template."\nTEMPLATE;\n";
> > $template = eval($template);
> >
> > The problem is that the eval() HEREDOC combination gives the
> > following output:
> >
> > <?=strtoupper(Array['time']);?>
> >
> > If in the HTML file (template) I use
> >
> > <div><?=strtoupper({$user['name']});?></div>
> >
> > I get  <?=strtoupper(username);?> as an output.
> >
> > I have tried closing the php tag like this:
> >
> > $template = "return <<<TEMPLATE\n?>".$template."\nTEMPLATE;\n";
> >
> > but the extra ?> only gets outputed as HTML.
> >
> > This is my first post to this mailing list, so I great you
> > all and thank you for any kind of solution to my problem.
>
> Why are you using HEREDOC to begin with? I personally find them to be ugly
> and more trouble than they're worth.
>
> You can write the same thing as this I think (untested):
>
> $template = eval(file_get_contents($file));
>
> But you might also consider using "include_once" or "require_once" instead
> of this "eval()" business.
>
> Also note, that a string can span more than one line and have variables in
> it. It can even be used with code, so HEREDOC is again useless for most
> situations:
>
> $foo = "
> Hello $name,\n
> \n
> Today is ".date('Y-m-d')."\n
> \n
> Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> \n
> Nulla eros purus, pharetra a blandit non, pellentesque et leo. In augue
> metus, mattis a sollicitudin in, placerat vitae elit.
> \n
> Quisque elit mauris, varius sit amet cursus sed, eleifend a mauris.
> ";
>
From: lists on



On Wed, 21 Jul 2010 01:04:12 +0300, Sorin Buturugeanu <mail(a)soin.ro> wrote:
> Hello Vincent and thank you for your reply :).
>
> That's true, I forgot to explain how I got to using HEREDOC, so ..
>
> Using eval(file_get_contents($file)) just outputs the result on the
> spot and I need
> to get the whole output (without echoing it) and do some more things with
> it.
>
> require_once() doesn't fit here (from what I can tell), because it
> would still just
> include the file in echo the output.
>
> I think there must be a solution, but I'm missing something here ..


Check out the ob_* functions

You could do this

ob_start();

include "/your/file.php";

$output = ob_get_clean();

echo $output;

Jim

>
> Thanks again!
>
> --
> Sorin Buturugeanu
> http://www.soin.ro
>
>
> On Wed, Jul 21, 2010 at 12:49 AM, Daevid Vincent <daevid(a)daevid.com>
> wrote:
>>
>> > -----Original Message-----
>> > From: Sorin Buturugeanu [mailto:mail(a)soin.ro]
>> > Sent: Tuesday, July 20, 2010 2:11 PM
>> > To: php-general(a)lists.php.net
>> > Subject: [PHP] eval and HEREDOC
>> >
>> > Hello,
>> >
>> > I am having trouble with a part of my templating script. I'll
>> > try to explain:
>> >
>> > The template itself is HTML with PHP code inside it, like:
>> >
>> > <div><?=strtoupper($user['name']);?></div>
>> >
>> > And I have the following code as part of the templating engine:
>> >
>> > $template = file_get_contents($file);
>> > $template = "return <<<TEMPLATE\n".$template."\nTEMPLATE;\n";
>> > $template = eval($template);
>> >
>> > The problem is that the eval() HEREDOC combination gives the
>> > following output:
>> >
>> > <?=strtoupper(Array['time']);?>
>> >
>> > If in the HTML file (template) I use
>> >
>> > <div><?=strtoupper({$user['name']});?></div>
>> >
>> > I get  <?=strtoupper(username);?> as an output.
>> >
>> > I have tried closing the php tag like this:
>> >
>> > $template = "return <<<TEMPLATE\n?>".$template."\nTEMPLATE;\n";
>> >
>> > but the extra ?> only gets outputed as HTML.
>> >
>> > This is my first post to this mailing list, so I great you
>> > all and thank you for any kind of solution to my problem.
>>
>> Why are you using HEREDOC to begin with? I personally find them to be
> ugly
>> and more trouble than they're worth.
>>
>> You can write the same thing as this I think (untested):
>>
>> $template = eval(file_get_contents($file));
>>
>> But you might also consider using "include_once" or "require_once"
> instead
>> of this "eval()" business.
>>
>> Also note, that a string can span more than one line and have variables
> in
>> it. It can even be used with code, so HEREDOC is again useless for most
>> situations:
>>
>> $foo = "
>> Hello $name,\n
>> \n
>> Today is ".date('Y-m-d')."\n
>> \n
>> Lorem ipsum dolor sit amet, consectetur adipiscing elit.
>> \n
>> Nulla eros purus, pharetra a blandit non, pellentesque et leo. In augue
>> metus, mattis a sollicitudin in, placerat vitae elit.
>> \n
>> Quisque elit mauris, varius sit amet cursus sed, eleifend a mauris.
>> ";
>>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

From: David Robley on
Sorin Buturugeanu wrote:

> Hello,
>
> I am having trouble with a part of my templating script. I'll try to
> explain:
>
> The template itself is HTML with PHP code inside it, like:
>
> <div><?=strtoupper($user['name']);?></div>
>
> And I have the following code as part of the templating engine:
>
> $template = file_get_contents($file);
> $template = "return <<<TEMPLATE\n".$template."\nTEMPLATE;\n";
> $template = eval($template);
>
> The problem is that the eval() HEREDOC combination gives the following
> output:
>
> <?=strtoupper(Array['time']);?>
>
> If in the HTML file (template) I use
>
> <div><?=strtoupper({$user['name']});?></div>
>
> I get <?=strtoupper(username);?> as an output.
>
> I have tried closing the php tag like this:
>
> $template = "return <<<TEMPLATE\n?>".$template."\nTEMPLATE;\n";
>
> but the extra ?> only gets outputed as HTML.
>
> This is my first post to this mailing list, so I great you all and thank
> you for any kind of solution to my problem.
>
> Thank you!

Possibly your php environment has short-tags turned off.


Cheers
--
David Robley

To save trouble later, Joe named his cat Roadkill Fred
Today is Boomtime, the 56th day of Confusion in the YOLD 3176.

 |  Next  |  Last
Pages: 1 2
Prev: MySQL select matching
Next: php array in different OS