I’m having trouble embedding PHP into a template and having anything on the page render, and I assume it’s because I’m working with a Relationship field.

Just as an example, this works:

<span class="date"><?php echo("Client A"); ?></span>

and this works:

<span class="date">{portfolio_projects:client_name}</span>

but this does not (I just get a blank page with empty html and body tags):

<span class="date"><?php echo({portfolio_projects:client_name}); ?></span>

So I reckon I’m doing something wrong. Help?! (BTW, I’ll be needing to use PHP on my template to do a regex/preg_replace on strings.)


A couple things here:

1) You don't have any quotes surrounding the template variable in your PHP echo function. This is likely causing a PHP error, because the string is not enclosed in quotes.

2) It's a really really really bad idea to use PHP in templates. What kind of replacement are you trying to do? Have you looked at add-ons that could do this instead, such as Low Replace? Failing that, writing a custom plugin is pretty trivial and would be a much better way to accomplish your needs.

  • Out of curiosity, why is it a terrible idea to use PHP in templates? (And why does Ellis open that window if it's bad news?) – Gregir Jul 7 '14 at 0:34
  • Spot on, by the way. Have been staring at this all day and didn't see I was missing quotes per the documentation. As for writing a plugin...well, before today, I've never written a single line of PHP, so if I can, I'll opt for downloading someone else's! Thanks for the suggestion. – Gregir Jul 7 '14 at 0:48
  • It is bad news because anyone with template access has the ability to do anything to your EE install. Add/delete data from the DB, modify core files anything that can be done in PHP can be achieved if you have php enabled in your templates. – Justin Long Jul 7 '14 at 3:09
  • From the EE docs: IMPORTANT Enabling PHP in a template will enable anyone with editing rights for that template to become a de-facto Super Admin since they can execute any PHP they want in that template, including PHP that can reveal information about your system, PHP that can delete data from your database, etc. Exercise extreme caution before enabling this option if you permit others to edit your templates. – Justin Long Jul 7 '14 at 3:10
  • Oh, I glossed over that since I'm the only admin/person with editing rights of the sites I've built to date. Thanks for the follow up. – Gregir Jul 7 '14 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.