0

I'm trying to parse a global variable inside a snippet that represents my page footer, and I want to perform a string function on the content of that variable. The snippet is being called into a template with PHP parsing ON and set to OUTPUT. This is the code I'm having trouble with:

<?php echo trim(str_replace(array("\r\n","\n","\r"), ', ', "{address}")); ?>

The value of the variable is (for example):

Big Company
Main Street
Big Town

And I want it to be:

Big Company, Main Street, Big Town

But PHP is trying to modify the string "{address}" not the actual value of the address variable, so I just end up with the original content (address with line breaks) when the EE parses the result of that function which is {address} in the template.

What am I doing wrong?

EE 2.6.1

  • I'm guessing that this is because snippets are parsed so early in the template, so what's the alternative? It didn't seem to work in an embedded template either :( – Standout Digital Sep 9 '13 at 12:35
2

You can fetch that global variable like:

$address = ee()->db->select('variable_data')->where('variable_name', 'address')->get('global_variables')->row('variable_data');
echo trim(str_replace(array("\r\n","\n","\r"), ', ', $address));

It would work for you.

  • Yeah, I was trying to avoid a direct hack like this but it would be the quickest temporary solution, thanks. – Standout Digital Sep 9 '13 at 14:56
  • I don't think its hack ;) – Bhashkar Yadav Sep 9 '13 at 15:00
1

This is because user-defined global variables are parsed very late - after PHP on input, after all module and plugin tags, and even after PHP on output (see parse order).

Your best bet here is to write a simple plugin which fetches the global variable directly from the database, does its work, then returns it.

  • Sounds handy, a little surprised it needs a workaround though. – Standout Digital Sep 9 '13 at 14: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.