Handy tips for Perch CMS developers

Posted on
by Philip Gwynne | Code , Web

Below is a list of handy Perch CMS tips and snippets that I’ve found useful over the years.

My aim is to update this list as and when I discover new and lesser-known solutions that could help out other Perch developers. If any of these help you, do let me know!

Setting up a config file to work on multiple servers

Setting up Perch’s config file so that it allows Perch to work on multiple servers and databases is the first thing I always do when starting a project. Once set up you don’t have to remember those pesky passwords or paths when you deploy.

To do this, we need to set up cases in your config.php file. Simply add the following snippet after the PERCH_LICENSE_KEY line:

$http_host = getenv(‘HTTP_HOST’);
{ case(‘mysite.local’) : define(“PERCH_DB_USERNAME”, ‘root’); define(“PERCH_DB_PASSWORD”, ‘xxxxxx’); define(“PERCH_DB_SERVER”, “localhost”); define(“PERCH_DB_DATABASE”, “db-local”); break;

case(‘mysite.my-staging-server.com’) : define(“PERCH_DB_USERNAME”, ‘staging_username’); define(“PERCH_DB_PASSWORD”, ‘staging_password’); define(“PERCH_DB_SERVER”, “localhost”); define(“PERCH_DB_DATABASE”, “db-staging”); break; default : define(“PERCH_DB_USERNAME”, ‘mysite_user’); define(“PERCH_DB_PASSWORD”, ‘mysite_password’); define(“PERCH_DB_SERVER”, “localhost”); define(“PERCH_DB_DATABASE”, “db-live”); break; }

The default case should always be your live server details.

Once that’s in, update the details to your own and remove any references of PERCH_DB_USERNAME, PERCH_DB_PASSWORD, PERCH_DB_SERVER and PERCH_DB_DATABASE after the cases have been defined.

Using Perch ‘if’ conditional tags

You might not have realised that you can test for multiple values when using perch:if tags. This can save you time and reduce the amount of code you need – especially when used in conjunction with the perch:else tag:

<perch:if exists="id_1 AND (id_2 OR id_3)">
  <p>If content exists, show this text.</p>
  <perch:else />
  <p>If content doesn't exist, show this text instead.</p>

Using ‘replace’ to keep links correct

If you have a field for a URL that will be placed in a hyperlink, you can ensure that the link always stays intact, whether or not your client includes http:// or https://. You can do this by using replace like below:

<a href="http://<perch:content id="url" type="text" replace="http://|,https://|" label="URL" />">Link to website</a>

This is also very handy to use with id=”_page” when you’re passing through linked content from another page, and require clean URLs:

<perch:content id="_page" replace=".php|" />

Adding an image with a fallback

You’d be forgiven to think that your only option to implement a fallback image is by using a combination of perch:if and perch:else tags. However, there’s a much simpler way to implement a fallback using the else attribute on the image field itself:

<perch:content id="image" type="image" else="/fallback.jpg" />

Using Perch Content with Forms

You can easily include perch:content within a Perch Form. To give an example, I created a website for Careline Lifestyles where each individual care home page uses the same enquiry form. Regardless of which care home page you view, the form is automatically pre-populated with the appropriate care home’s name and email address, using content from it’s own page – and any submitted enquiries are emailed directly to that care home.

To do achieve this, I already had a Region within the master page template for the homes, called “Home Information”. This stored each care home’s name (id=“home_name”) and email address (id=“home_email”). So to add those fields to a form, I had to create a form template and place each of the perch:content id’s within a perch:input form field, like so:

<perch:input type="hidden" id="home_name" value="<perch:content id="home_name" type="text" label="Home" />" label="Home" />
<perch:input type="hidden" id="home_email" value="<perch:content id="home_email" type="text" label="Home Email" />" label="Home Email" suppress="true" />

Since I was adding content from a Perch Content region, I made sure I saved my form template to the content templates folder ‘domain.com/perch/templates/content/’ – not the form templates folder!

I then added the form to the page, specifying the Region name I was wanting to take the content from, and told Perch to use the form template I’d just created:

    perch_content_custom('Home Information', array(
        'template' => '_home_contact_form.html',

After I submitted a response from the form once, it then displayed in the Forms App in Perch. I selected it and went to Form Options – where I added the id for the care home’s email address ({home_email}) to the email address field it should be sent to. And for the subject, I added ‘Online enquiry for: {home_name}’.

Tagged with , ,

Add a comment.