I'd like to define snippets dynamically in the index.php file.
Is there a way to do this?
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It only takes a minute to sign up.
Sign up to join this communityYou can't set up Snippets, but you can set up Global Variables in your config.php file.
Below is a code example from this blog post titled ExpressionEngine config.php.
$default_global_vars = array(
// Tag parameters - Short hand tag params
'global:disable_default' => 'disable="categories|pagination|member_data"',
'global:disable_all' => 'disable="categories|custom_fields|member_data|pagination"',
'global:cache' => 'cache="yes" refresh="10"',
'-global:cache' => '-cache="yes" refresh="10"', // disable by adding a '-' to the front of the global
// Date and time - Short hand date and time
'global:date_time' => '%g:%i %a',
'global:date_short' => '%F %d, %Y',
'global:date_full' => '%F %d %Y, %g:%i %a',
'global:isipad' => (bool) strpos($user_agent,'iPad'),
'global:isiphone' => (bool) strpos($user_agent,'iPhone')
);
// Make this global so we can add some of the config variables here
global $assign_to_config;
if(!isset($assign_to_config['global_vars'])) {
$assign_to_config['global_vars'] = array();
}
$assign_to_config['global_vars'] = array_merge($assign_to_config['global_vars'], $default_global_vars, $env_global_vars);
No, you can't generate them dynamically but you can use Snippet Sync or Libraree to use snippets as files and port them into other projects.
I use Libraree as part of my base install with a couple base snippets for a header and footer that I carry through projects. Using config overrides (Focus Lab's Master Config) I set where to store the files and edit away when I set up new projects
// Libraree
$config['libraree_basepath'] = $base_path . '/../snippets/';