Javascript Logo

Javascript CMS

ButterCMS is an API-based CMS that integrates enables CMS functionality in your app with no database required. Use ButterCMS for your Javascript apps to add CMS-powered blogs, dynamic pages, and more. Butter plays well with Node.js and all view-layers including React, Angular, Jade, and Vue.

Table of Contents

Introduction

Learn how to create dynamic pages using Butter and JavaScript. This guide uses React.js but Butter works with any client-side JavaScript framework including Angular and Vue.js. For server-side integration see our Node.js guide.

Creating dynamic pages with Butter is a two-step process:

1) Setup custom content fields in Butter
2) Integrate the fields into your application

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

Setup content fields

Let's suppose we want to be able to create pages for retail-chain locations using ButterCMS and we're starting out with a basic static page template for a single location:

routes.jsx:

import React from 'react';
import { Router, IndexRoute, Route } from 'react-router';

import Location from 'Location';

const Routes = (props) => (
  <Router {...props}>
    <Route path="/location/:slug" component={Location} />
  </Router>
);

export default Routes;

Location.js:

import React, { Component } from 'react';
import Butter from 'buttercms'

const butter = Butter('');

class Location extends Component {
  render() {
    return (
      <div>
        <h1>Widget Store - Chicago</h1>
        <img width="100%" src="/images/chicago.jpg">
        <p>Our Chicago location is located at 566 Wacker Drive.</p>
      </div>
    );
  }
}

export default Location;

Enabling new pages to be created with Butter is a two-step process:

  1. Setup custom content fields in Butter
  2. Integrate the fields into your application

To setup custom content fields, first sign in to the Butter dashboard.

Create a new workspace or click on an existing one. Workspaces let you organize content fields in a friendly way for content editors and have no effect on development or the API. For example, a real-estate website might have a workspace called "Properties" and another called "About Page".

Once you're in a workspace click the button to create a new content field. Choose the "Collection" type and name the field "Location Pages":

On the next screen, we'll setup the properties for each page in the collection.

Now go back to the work space and create a location:

Integrating your app

To create these pages in our app, we create a dynamic route that fetches content for the page by using a URL parameter. Here's what the code looks like:

Location.js:

import React, { Component } from 'react';
import Butter from 'buttercms'

const butter = Butter('');

class Location extends Component {

  constructor(props) {
    super(props);

    this.state = {
      content: null
    };
  }

  componentWillMount() {
    let slug = this.props.params.slug;

    butter.content.retrieve([`location_pages[slug=${slug}]`]).then((resp) => {
      this.setState({
        content: resp.data.data
      })
    });
  }

  render() {
    if (this.state.content) {
      const location = this.state.content.location_pages[0];

      return (
        <div>
          <h1>Widget Store - {location.name}</h1>
          <img width="100%" src={location.feature_image}>
          <p>{location.description}</p>
        </div>
      );
    } else {
      return (
        <div>
          Loading...
        </div>
      );
    }
  }
}

export default Location;

That's it! If you browse to /locations/chicago, you'll see the content we just entered into Butter.

If you need help after reading this, contact us via email, livechat, or book a time with to pair with one of our developers.

About Javascript

JavaScript is a programming language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else.

Headless CMS for these technologies and more

Ruby, Rails, Node.js, Python, ASP.NET, Flask, Django, Go, PHP, Laravel, Angular, React, Elixir, Phoenix, Meteor, Vue.js, Javascript, Heroku

Get started now

Sign up with Google Sign up with Github
or