Utilise Perch Production Modes

In the Multiple Server Config post I discussed how you can use a separate config file for each environment: Development, Staging and Production.

Perch also gives you the option to set the Production Mode you want to use. You can set the production mode in your config file, and if you have a different file for each environment, you can set a different production mode for each environment.

To do this you use the PERCH_PRODUCTION_MODE setting which can have the following values:

Here’s how you set the production mode to development:

define('PERCH_PRODUCTION_MODE', PERCH_DEVELOPMENT);

Regardless of what Perch does under the hood for each mode, we can check for the production mode in our code and do things differently in certain mode(s).

There are a few cases where this is useful. For example, you may want to include Google Analytics scripts in production only. Another possible use is to include some CSS or Javascript in development only to help you debug your front-end.

Or perhaps you want to include a fixed bar at the top when in development or staging to easily distinguish which environment you are in without having to look at the address bar.

You can check for the production mode with an if statement in your PHP files (pages or layouts):

if (PERCH_PRODUCTION_MODE === PERCH_PRODUCTION) {
    // do something in production only
    perch_content('Analytics');
}

Or if you want to include some HTML, you could use the following syntax so you don’t have to echo your HTML:

<?php if (PERCH_PRODUCTION_MODE === PERCH_DEVELOPMENT): ?>
    <div>You are in Development mode.</div>
<?php endif; ?>

If you are not using Pinion to manage your front-end assets, you could do the following specify different CSS files in development mode:

<?php if (PERCH_PRODUCTION_MODE === PERCH_DEVELOPMENT): ?>    
    <link rel="stylesheet" href="/css/styles.css" type="text/css">
    <link rel="stylesheet" href="/css/accessibility.css" type="text/css">
<?php else: ?>
    <link rel="stylesheet" href="/css/styles.min.css" type="text/css">
<?php endif; ?>
link