2

Is there a place in EE to store custom config variables and access them in the templates? I need to be able to use these variables in conditionals as well as output them. Furthermore, I need to have different sets of config values depending on which domain EE is on (e.g. if site_url = 'foo.com' ...).

Currently, I'm using globals, but it's not really ideal.

3

If by config variables, you mean the ones on config.php, it's pretty easy to do.

On your config.php add the following code:

global $assign_to_config;
if(!isset($assign_to_config['global_vars']))
$assign_to_config['global_vars'] = array();

Now, you can create early parsed template variables.

Let's say you set the mail protocol used by a System Config Override:

$config['mail_protocol'] = 'sendmail';

If you need to access this info on templates, you need to create a template variable:

$config['mail_protocol'] =
$assign_to_config['global_vars']['cv-mail_protocol'] = 'sendmail';

On templates, you can use {cv-mail_protocol}:

{if cv-mail_protocol == 'sendmail'}
  The emails are being send by SendMail
{/if}

You can use a PHP conditional too to set them:

if ($_SERVER['HTTP_HOST'] == 'www.domain.com')

These variables are parsed even before Preload Text Replacements, which gives you a lot of flexibility.

{if cv-language == "en"}
    {preload_replace:pr-name_label="Name"}
    {preload_replace:pr-tel_label="Phone number"}
[...]
{/if}

<label for="name">{pr-name_label}
  <input type="text" id="name" name="required[]" size="50" />
</label>
[...]
| improve this answer | |
  • Thanks. Is there a way to use these config variables in embeds? For example, {embed="common/.footer-{cv_layout-footer}"}? – ExpressionEngineNewbie Oct 12 '15 at 23:31
  • Sure! Config variables are parsed earlier than the embeds. But maybe you should use an layout. Try to keep everything simple. – Robson Sobral Oct 13 '15 at 0:12
  • What do you mean "use an layout"? – ExpressionEngineNewbie Oct 19 '15 at 1:46
  • Instead of have multiples footers and header, you can use layouts. Of course, I don't know your situation or your code. I'm guessing and trying to suggest alternatives. – Robson Sobral Oct 19 '15 at 11:14
1

I tend to use index.php for this.

On about line 72 you can add Custom Config Variables as an associative array.

$assign_to_config['global_vars'] = array('pv_country_id' => 3);

You can now use this in your template as {pv_country_id} (I prefix with pv_ so I can identify them when I view my templates in the future.)

The reason I use this method is that if you're a MSM user ('Multi Site Manager' for the uninitiated) you can override your variables on a per site basis.

| improve this answer | |
  • I never remember I can use the index.php, because I prefer to keep everything in one single file. But I think you can update my answer by adding yours. – Robson Sobral Sep 25 '15 at 13:00

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.