title: How to use templates in strapi
body: <p>I was trying to use the default template in a rest query using this:</p>
  
  <pre><code>/company/somecompanyid?template=default
  </code></pre>
  
  <p>But I still get all the data from my mongodb including fields and collections of related tables that are not in the templates.</p>
  
  <p>This is also defined as the defaultTemplate in the model but it doesn't seems to impact the results in any way.</p>
  
  <ol>
  <li><p>Can someone explain what I am doing wrong and how to apply the template? </p></li>
  <li><p>If I want to include just the object Id rather than a whole related object in the reply, how do I specify it in the template?</p></li>
  </ol>
  
  <p>company.js:</p>
  
  <pre><code>'use strict';
  
  /**
   * Module dependencies
   */
  
  // Node.js core.
  const path = require('path');
  
  // Public node modules.
  const _ = require('lodash');
  const anchor = require('anchor');
  
  // Local dependencies.
  const WLValidationError = require('../../../node_modules/waterline/lib/waterline/error/WLValidationError');
  
  // Settings for the Company model.
  const settings = require('./Company.settings.json');
  
  /**
   * Export the Company model
   */
  
  module.exports = {
  
    /**
     * Basic settings
     */
  
    // The identity to use.
    identity: settings.identity,
  
    // The connection to use.
    connection: settings.connection,
  
    // Do you want to respect schema?
    schema: settings.schema,
  
    // Limit for a get request on the list.
    limit: settings.limit,
  
    // Merge simple attributes from settings with those ones.
    attributes: _.merge(settings.attributes, {
  
    }),
  
    // Do you automatically want to have time data?
    autoCreatedAt: settings.autoCreatedAt,
    autoUpdatedAt: settings.autoUpdatedAt,
  
    /**
     * Lifecycle callbacks on validate
     */
  
    // Before validating value
    beforeValidate: function (values, next) {
      // WARNING: Don't remove this part of code if you don't know what you are doing
      const api = path.basename(__filename, '.js').toLowerCase();
  
      if (strapi.api.hasOwnProperty(api) &amp;&amp; _.size(strapi.api[api].templates)) {
        const template = strapi.api[api].templates.hasOwnProperty(values.template) ? values.template : strapi.models[api].defaultTemplate;
        const templateAttributes = _.merge(_.pick(strapi.models[api].attributes, 'lang'), strapi.api[api].templates[template].attributes);
  
        // Handle Waterline double validate
        if (_.size(_.keys(values)) === 1 &amp;&amp; !_.includes(_.keys(templateAttributes), _.keys(values)[0])) {
          next();
        } else {
          const errors = {};
  
          // Set template with correct value
          values.template = template;
          values.lang = _.includes(strapi.config.i18n.locales, values.lang) ? values.lang : strapi.config.i18n.defaultLocale;
  
          _.forEach(templateAttributes, function (rules, key) {
            if (values.hasOwnProperty(key)) {
              // Check rules
              const test = anchor(values[key]).to(rules);
  
              if (test) {
                errors[key] = test;
              }
            } else if (rules.required) {
              errors[key] = [{
                rule: 'required',
                message: 'Missing attributes ' + key
              }];
            }
          });
  
          // Go next step or not
          _.isEmpty(errors) ? next() : next(new WLValidationError({
            invalidAttributes: errors,
            model: api
          }));
        }
      } else if (strapi.api.hasOwnProperty(api) &amp;&amp; !_.size(strapi.api[api].templates)) {
        next();
      } else {
        next(new Error('Unknow API or no template detected'));
      }
    }
  
    /**
     * Lifecycle callbacks on create
     */
  
    // Before creating a value.
    // beforeCreate: function (values, next) {
    //   next();
    // },
  
    // After creating a value.
    // afterCreate: function (newlyInsertedRecord, next) {
    //   next();
    // },
  
    /**
     * Lifecycle callbacks on update
     */
  
    // Before updating a value.
    // beforeUpdate: function (valuesToUpdate, next) {
    //   next();
    // },
  
    // After updating a value.
    // afterUpdate: function (updatedRecord, next) {
    //   next();
    // },
  
    /**
     * Lifecycle callbacks on destroy
     */
  
    // Before updating a value.
    // beforeDestroy: function (criteria, next) {
    //   next();
    // },
  
    // After updating a value.
    // afterDestroy: function (destroyedRecords, next) {
    //   next();
    // }
  };
  </code></pre>
  
  <p>the template which is a subset of the company attributes (CompanyDefault.template.json):</p>
  
  <pre><code>{
    "default": {
      "attributes": {
        "name": {
          "required": true
        },
        "address": {},
        "phone": {},
        "email": {}
      },
      "displayedAttribute": "name"
    }
  }
  </code></pre>