Skip to content

Create your template

François Benaiteau edited this page May 20, 2014 · 5 revisions

As of version 1.0.0, metaJSON use a template engine to generate code. This means changes of generated code are easier and do not require some Python knowledge.

This also means that you can override the default templates in specifying your own template directory with --template option.

Structure

Here's the template files for the iOS code:

├── APIParser
│   ├── _PREFIX_APIParser.h
│   └── _PREFIX_APIParser.m
├── AbstractInterfaceFiles
│   ├── __CLASSNAME_.h.mustache
│   ├── __CLASSNAME_.m.mustache
│   ├── array_property.mustache
│   ├── multiple_property.mustache
│   ├── multiple_property_implementation.mustache
│   ├── number_property.mustache
│   └── string_property.mustache
├── Utilities
│   ├── NSString+RegExValidation.h
│   └── NSString+RegExValidation.m
├── _CLASSNAME_.h.mustache
└── _CLASSNAME_.m.mustache

In the above structure, you can distinguish 3 kind of files:

  1. Files with two extensions : .mustache and another extension: These files are processed on each meta Object you defined in your json description. Exemple: CLASSNAME.h.mustache

  2. Partials: they have only one extension .mustache. There are here to help you write your template and reduce duplication of code.

  3. Other files will also be processed but only with replacement of the following placeholders in filename or content:

    • CLASSNAME
    • PREFIX
    • YEAR
    • DATE

Language

Template files use mustache syntax. Here's a list of keys and functions you can use in the template which refers to metaJSON keys:

Template Keys metaJSON Comments
prefix - Options passed by commandline
date - year of current date
baseTypeIsObject base-type false if inherits from another metaObject
baseClassName base-type classname of base class
custom_classes base-type go through base-type of properties, list of Classe names
numberProperties property properties with base-type number
stringProperties property properties with base-type string
booleanProperties property properties with base-type boolean
dataProperties property properties with base-type data
dateProperties property properties with base-type date
arrayProperties property properties with base-type array
objectProperties property properties with base-type object
undefinedProperties property properties with base-type multi and any
description description
varName name processed name to avoid clash generated code. i.e. : id to {{prefix}}Id
name name
required required
maxValue maxValue
minValue minValue
regex regex
maxLength maxLength
minLength minLength
maxCount maxCount
minCount minCount
hasOneSubtype subType
hasMultipleSubtypes subType
hasNoBasetypes subType
hasCustomType base-type
hasAnyType base-type
hasStringType base-type
hasNumberType base-type
hasDateType base-type
hasDataType base-type
hasBooleanType base-type
subtypes subType
subtypes.subtype subType
subtypes._subtype subType reference full object definition
subtypes.className subType
types base-type
types.type base-type
types._type base-type reference full object definition
types.className base-type and also name

NOTE: Template keys with dot notation means the second part of the key is accessible only inside the first part key.

Template Functions Comments
_upper_camelcase fonction to transform a string. i.e. : sender_list -> SenderList
_camelcase fonction to transform a string. i.e. : sender_list -> SenderList
_lowercase fonction to transform a string. i.e. : sender_list -> SenderList
_uppercase fonction to transform a string. i.e. : product -> PRODUCT
_capitalize fonction to transform a string. i.e. : product -> Product
_snakecase fonction to transform a string. i.e. : SenderList -> sender_list
_upper_snakecase fonction to transform a string. i.e. : SenderList -> SENDER_LIST
Clone this wiki locally