Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steven Applegate 18 posts 178 karma points
    Nov 17, 2017 @ 00:05
    Steven Applegate
    0

    What is the best practice for a template containing a lot of custom logic

    Right now my document types are pretty simple, with only a couple lines of custom code in my views that are retrieving some data and storing them in variables.

    But I need to build a pretty complicated template that will be doing a lot of things: calling an external API, send an email using SendGrid, create some objects to store data in my own database, possibly do some stuff in Azure, do some logging, etc.

    I'd rather not just have all that logic sitting in my view - is there a best practice for how to have a lot of custom functionality that's doing several things in different places?

    I'm imagining that I could put all that code in custom classes in my project, and then my view would just call a DoSomething() function, but I don't know the best way to go about doing that with Umbraco.

    I found this article, but it's 4 years old now, and I'm not sure if it's outdated: https://24days.in/umbraco-cms/2013/how-and-why-we-do-umbraco-mvc/

  • Matt Darby 28 posts 391 karma points c-trib
    Nov 17, 2017 @ 04:37
    Matt Darby
    101

    That's correct, it's best practice to put the logic into a Controller rather than the View just like a regular MVC application.

    Check out SurfaceControllers. There is a great tutorial on Umbraco.TV here which demonstrates implementing a contact form.

  • Ben Palmer 176 posts 842 karma points c-trib
    Nov 17, 2017 @ 06:34
    Ben Palmer
    2

    Just to build on Matt's answer - if it's for a document type template I'd suggest looking at render controllers and route hijacking - https://our.umbraco.org/documentation/reference/routing/custom-controllers

    Render controllers allow you to perform logic between Umbraco and the view. So, even with a small amount of logic, I would tend to create a render controller and create a custom view model with the data that you need. You can then pass that through to the view from your controller method.

  • Steven Applegate 18 posts 178 karma points
    Nov 17, 2017 @ 14:43
    Steven Applegate
    0

    Thanks Matt and Ben! Both of those are very helpful.

Please Sign in or register to post replies

Write your reply to:

Draft