Generating an Add-on with a Custom Template Tag via CLI

In ExpressionEngine, you can use the Command Line Interface (CLI) to quickly generate both the basic add-on files and custom template tags. The combination of only a few commands allows you to build a comprehensive add-on with a custom template tag efficiently. In this guide, we will walk you through the steps of generating the “emoji_reactions” add-on and adding a custom template tag.

The CLI simplifies the add-on development process, enabling users to concentrate on crafting business logic that suits their requirements. This shift in focus allows users to prioritize writing code that meets their needs, rather than getting caught up in the technical intricacies of making an add-on operational within ExpressionEngine. Our new CLI make commands are designed to make it as simple as possible to get up and running with building an add-on.

Access the CLI Environment

Open your terminal or command prompt and navigate to the root directory of your ExpressionEngine installation. The eecli.php file is located in the system/ee directory.

Generate the Basic Add-on

To generate the scaffolding for a new add-on, use the following command:

php eecli.php make:addon "emoji_reactions"

This command will create the basic add-on files, including the upd file, mod file, and lang file, for the “emoji_reactions” add-on. Follow all prompts in the command to complete the creation of the add-on. You can also use the --help parameter to see all available options for the command.

The result of that one command will give you an add-on that can be installed and uninstalled, and contains everything you need to get started.

Generate the Template Tag

To add a custom template tag to your add-on, use the following command:

php eecli.php make:template-tag ListReactions --addon=emoji_reactions

This command will generate the file system/user/addons/emoji_reactions/Tags/ListReactions.php.

Customize Your Template Tag

Open the file system/user/addons/emoji_reactions/Tags/ListReactions.php and implement the functionality of your template tag. When the template tag is called, ExpressionEngine will run the process() function in the newly created ListReactions.php file.

Here is an example of what the process() function of our ListReactions template tag looks like:

    // Example tag: {exp:emoji_reactions:list_reactions entry_id="1"}
    public function process()
    {
        $entry_id = ee()->TMPL->fetch_param('entry_id');
        $entry = ee('Model')->get('ChannelEntry', $entry_id)->first();

        // Retrieve the search_id
        if (! $entry) {
            return ee()->TMPL->no_results();
        }

        $emojis = ee('Model')->get('emoji_reactions:Emoji')->all();

        $reactions = ee('Model')->get('emoji_reactions:EmojiReaction')
            ->with('Emoji')
            ->filter('entry_id', $entry_id)
            ->all();

        foreach($emojis as $emoji) {
            $data[] = [
                'html_entity' => $emoji->unicode,
                'short_name' => $emoji->name,
                'reaction_count' => $reactions->filter('emoji_id', $emoji->emoji_id)->count(),
            ];
        }

        return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $data);

    }

Going Deeper

Matt also gave a presentation at the July 2023 ExpressionEngine meetup. Check out the video of his talk on Building an ExpressionEngine Add-on the Easy Way

Conclusion

By combining only a few commands, you can efficiently generate an add-on with a custom template tag in ExpressionEngine via the Command Line Interface. This enables you to extend the capabilities of your ExpressionEngine website and create a more powerful and feature-rich experience for your users. The CLI streamlines the process of building an add-on in a way that allows the user to focus more on writing business logic to suit their needs, rather than focusing on the specifics of how to get it to run in ExpressionEngine.

Comments 0

Be the first to comment!