Jump to content

PHP Not evaluated in TemplateFile


Gazley
 Share

Recommended Posts

Hi there,

I think I'm going a bit crazy! I have a string value like the following:

<p><a href="<?=$pages->get('/smart-devices/')->url?>">Mobile Application Development</a> for Android, BlackBerry and iOS. Auto-generate native code without any user knowledge required</p>
 
This value is part of the value in $outBody (see following)
 
I set as a template value like so:
 
    $partial = new TemplateFile(wire('config')->paths->templates . 'partials/text_area.inc');
    $partial->set('text_area', $outBody);
    $pages = wire('pages');
    $page->outBody = $partial->render();
 
The actual last line of the real template file says:
 
include("{$config->paths->templates}main.php");
 
The main.php file includes a layout file that says:
 
<?= $page->outHeader ?>
<section class="<?= $page->sectionClass ?>">
  <?
  $class_name = 'static';
  if ($page->static_page !== 1) {
    echo $page->list_items;
    $class_name = 'content';
  }
  ?>
  <div class="<?=$class_name?>">
    <?= $page->outBody ? $page->outBody : $page->body ?>
  </div>
</section>
<?= $page->footerBand ?>
 

You can see that it directly references the $page->outBody variable. The problem is that in main.php, the href of the link specified at the start  resolves to <?=$pages->get('/smart-devices/')->url?> and not the actual URL!

What am I doing wrong here? It seems that PHP is just not evaluating the expression despite the PHP on/off directives that wrap it.

Any suggestions to stop me going more crazy! :)

Thanks

--Gary

Link to comment
Share on other sites

This all seems a bit confusing, so I'm not really sure if I'm even on the right track here, but still: is that first piece of markup + PHP you posted actually a variable value, ie. your variable contains stuff like "<p><?= ... ?></p>" etc.?

In that case the problem would be that just echoing out a string containing PHP won't (and shouldn't) evaluate that PHP as code. You'd have to use eval() for that, which on the other hand is something I'd strongly recommend against.

Anyway, if that's the case I'm sure there are a ton of better options -- but before going any further I'd like to confirm if my guess was anywhere close to reality? :)

Link to comment
Share on other sites

Hi teppo - the markup that I've suggested is part of the variable is actually content from a number of fields on a page that are merged together into a partial file. The markup string is built up and then set() into a property in a TemplateFile based on the partial. I then call render() on the TemplateFile instance.

Does that make sense?

Link to comment
Share on other sites

I may be a bit tired here, but I can't see how that was even related to what I asked above :)

Let's try this instead: insert <?= var_dump($outBody) ?> right before your "$partial->set('text_area', $outBody)" row and paste it's result here. This should pretty much show if I was on the right track or not.

Link to comment
Share on other sites

$partial->set('text_area', $outBody) is a line in a method, not a template file, so I echoed out the value:

string '<h1>GXflow™ Lets you Model, Automate, Manage & Optimize Business Processes in <em>any</em> Company!</h1>


<p class="lead">GXflow is a tool that allows modeling, automating, managing and optimizing the business processes in a company for the creation of mission-critical applications in a simple and effective way.</p>

<div class="gxp-kf well wb mb-40">
<h2>Key Features For GXflow™</h2>

<div class="features">
<div>
<div class="one_col">
'... (length=3954)

Link to comment
Share on other sites

OK, that didn't work quite as planned -- too much content there obviously. Was it actually cut in the middle like that or did the part with that URL you're trying to output show up too? If it did, it'd be nice to see that and a few lines around it, just to make sure.. :)

What I was looking for was what your $outBody variable exactly looks like at that point -- ie. if it has PHP code, such as <?= ...whatever... ?>, in it. In that situation simple echo wouldn't eval the code and it would print out as-is.

@Soma: that shouldn't be needed. PHP echo and short echo can both handle ternary operator just fine.

Link to comment
Share on other sites

Hi teppo,

You'll have to believe me on this :) $outVal contains the embedded <?=$pages->get('/smart-devices/')->url?>  between the quotations in the link's href.

I'm not actually echoing out the whole string. The string is ultimately being built up and specified against a $page variable called $page->outVal - in my layout file that is part of the final include'd master "main.php" file, $page->outVal is referenced there. I guess I assumed that it would "eval" any PHP content but it doesn't seem to do that. This means that I would actually have to specify the actual URL in the href.

Link to comment
Share on other sites

It would be a BIG security issue if you could. Nothing strange here, as in all programming languages...

But then I also dont see the benfit of doing this. You could with eval() though but not recommended. You could just echo the url there into the string in the first place and pass that after to template.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...