3

I'd like to define snippets dynamically in the index.php file.

Is there a way to do this?

2 Answers 2

7

You 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);
3
  • 1
    Nice! I didn't know you could add a global:disable_default parameter. That's awesome. We use globals for a lot of things, but I never even looked at them for a method of setting defaults. Dec 13, 2012 at 7:56
  • They're called global variables, but they look like they'd have to be parsed at the same time as snippets... I'm confused, but I'll try it. :) Dec 13, 2012 at 17:44
  • 1
    To those reading this later: yes "global_vars" assigned in the config file act like snippets; they are available at the beginning of parsing, not the end. Sep 5, 2013 at 20:15
4

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/';

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.