0

Is there a way to access config variables from the config files via native EE tags so that I can display it in the templates ?

Or are there none that I have to right a plugin for it?

4 Answers 4

1

We can add variables to the config.php, instead of index.php, by this way:

global $assign_to_config;

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

So, I believe that you can set some config variables by this way:

$assign_to_config['global_vars']['cv_smtp_port'] = $config['smtp_port'] = '587';

On your templates, you'll have early parsed variables to work with:

{cv_smtp_port}

You can use this technique to get some data from global variables in early stages of parsing:

$assign_to_config['global_vars']['cv_site_url'] = $config['site_url'] = "http://domain.com";

{site_url} is parsed at the end of parsing stages, while {cv_site_url} will be parsed at the beginning.

I don't tested this, but I guess that edit edit config.php by the CP can destroy all of this. So, test this and maybe do not allow the users to access the config file editor.

1
  • Good suggestion. I think I will try this later. Commented Nov 12, 2013 at 23:42
0

I don't think so, the only method I'm aware of is using global variables in index.php as per the user contributed notes here - http://ellislab.com/expressionengine/user-guide/templates/globals/user_defined.html

I think otherwise you may need a plug-in though I'm not even sure that would work as the values in the DB which you could pull out with a plug-in are not necessarily going to mirror those defined in config.php.

0

Here's a really quick plugin that would do exactly what you need. Basically, create a folder in your add-ons directory called 'config_var', create a file pi.config_var.php and copy/paste the following in there

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$plugin_info = array(
    'pi_name'           => 'Config Vars',
    'pi_version'        => '1.0',
    'pi_author'         => 'Carl Crawley',
    'pi_author_url'     => 'http://www.madebyhippo.com',
    'pi_description'    => 'Returns Config var based on input',
    'pi_usage'          => config_var::usage()
);

class Config_var
{
    public $return_data = '';

    public function Config_var()
    {
        $this->EE =& get_instance();

        $var = $this->EE->TMPL->fetch_param('var');

        $this->return_data = $this->EE->config->item($var); 

        return; 
    }

    public function usage()
    {
        ob_start();
?>
Simply pass the variable you want to return in the tag {exp:config_var var="xxx"} and it will return the variable from the config.
<?php
        $buffer = ob_get_contents();

        ob_end_clean();

        return $buffer;
    }
    // END
}

Once you've done that, you can now simply called {exp:config_var var=""} passing in the config variable you want and it'll pull the data out onto the page.

2
  • Yeah, this is the alternative method that I was thinking of. There are some things I need to determine first though, like what if I use this in snippets? Does it get parsed correctly? Commented Nov 12, 2013 at 23:43
  • It will be parsed just like any other plugin. I believe this is a realy good idea if you don't mind of have an extra addon just for this or if you don't need this variables being parsed just after the snippets, like on my suggestion.
    – Sobral
    Commented Nov 13, 2013 at 0:40
0

While this is quite old thread, I don't see esy-to-use solution posted to it.

Config Variables Plugin is easy to use and provides access to all config variables (whether coming from config.php or saved in database) with template tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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