I come from a "traditional" PHP paradigm where views are mixed with processing logic. And now switching from WordPress to CraftCMS (which so far is a beautiful experience) I am having trouble with building custom logic.

For example, something as easy as integrating a backend for a webhook endpoint or processing a form is easily done when the "view" is mixed with the "controller", as in WordPress themes.

I like the fact that CraftCMS uses twig to avoid this mixup (I see many advantages in this approach), but I haven't found documentation regarding this topic.

We have a folder with all the index.twig, content.twig ... but no "controllers" folder.

How do we add PHP logic to a "template" view?

1 Answer 1


The short answer is ... you don't. There will never be anything like controllers in your templates folder, because that folder should only contain template files.

In order to use custom logic in Craft ('s templates), use a module. You could look at modules as a way to build your own little website-specific plugin, but without all the hassle of building a real plugin (your module will never be published to the plugin store, for instance). In fact, the default installation of Craft comes with a module folder, to get you going.

Modules, just like plugins, can have controllers, services, and more. They can even provide template variables so your module's logic can be used in your templates.

If you'd like to learn more about creating a module, please read the 'building a plugin' documentation and read 'module' when the documentation says 'plugin'. Most things are pretty much the same.

  • Thanks Paul, this is a great answer. Sep 21, 2021 at 15:15
  • 1
    Just a note: building a custom plugin or a module take the same effort (you don't need to submit your plugin to the store) and plugins offer useful features such as the ability to have a settings page in the control panel.
    – gioppe
    Sep 22, 2021 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.