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

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 :( Commented Sep 9, 2013 at 12:35

2 Answers 2

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.

2
  • Yeah, I was trying to avoid a direct hack like this but it would be the quickest temporary solution, thanks. Commented Sep 9, 2013 at 14:56
  • I don't think its hack ;) Commented Sep 9, 2013 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.

1
  • Sounds handy, a little surprised it needs a workaround though. Commented Sep 9, 2013 at 14:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.