Sweet.apex

  • Docs
  • Tutorials
Docs Menu
  • Sweet.apex Core
    • Transpilation
    • Grammar
    • Command
    • Config
  • Features
    • Action
    • Apex Doc
    • Array Creation
    • Aspect
    • Cast
    • Default Value
    • Enum
    • File
    • Function
    • Identity
    • Injection
    • Lambda
    • Log
    • Mod
    • Not Null
    • Operator
    • Optional
    • Reflection
    • Rethrow
    • Switch
    • Template String
    • Template
    • Script
    • Tagged String
    • Annotation
    • Nullable
    • Var
    • Val
    • Map Access
    • Constructor
    • Transaction
    • Destructure
    • Import Static
    • Pipeline
    • Varargs
    • Patch
    • Import As
  • Plugin Development
    • Feature
    • Test Case

Template Guide

Template

Feature Overview

This feature converts templates in the source files.

Prerequisite

You need to create template files in the template directory set in the config file.

Sweet Apex Example

@log
public class TemplateDemo {
    public static void main() {
        #debug('Hello World')

        #debug('a\,b', 'c\,d')
    }
}

Transpiled Apex

public class TemplateDemo {
    public static final Log logger = Log.getLogger(TemplateDemo.class);

    public static void main() {

        if(logger.isDebugEnabled()) {
            logger.debug('Hello World');
        }
        if(logger.isDebugEnabled()) {
            logger.debug('a,b', 'c,d');
        }
    }
}

Usage

Here is an example of how to create a new template definition.

// In debug.js
const _ = require('lodash');

const debug = (...values) => `
if(logger.isDebugEnabled()) {
    logger.debug(${_.join(values, ', ')});
}
`;

module.exports = debug;

Place this js file in the template directory set in the config file and Sweet.apex will load it.

Here is how you would invoke this template.

#debug('Hello World')

# starts the template, and anything between the following parenthesis, separated by comma, will be passed in as the arguments.

For example,

#test(abc, def)

Contribute on Github! Edit this section.