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?
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.
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.
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.
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.