4

I'm assessing Craft's suitability for my next project, coming from using HMVC in CodeIgniter with a custom CMS.

In CI all requests go via a controller action which is responsible for inspecting request parameters, loading and filtering data and passing that data to a view. Pretty typical MVC.

From what I've seen so far of Craft and plugins developed for it, the requests are typically forwarded directly to Twig templates and those templates are responsible for analysing query strings and request data and fetching data from exposed template variables or built in "entries".

I'm thinking of a use case where data would need to be displayed and filtered by query string and session criteria and it strikes me that the template would be handling too much of this logic and become bloated.

Most of the website I will be developing will get its data in JSON format from a REST API and passing filter criteria from user interaction with forms and UI controls will come up a lot.

My question is, is this a typical and recommended approach to using Craft, or should I be using controllers to handle tasks such as filtering and fetching data and passing it to a view?

Are there any plugins that I could look at for reference that could help me to understand the flow and process?

Thanks

Jamie

| improve this question | | | | |
  • 1
    My general rule of thumb is, a little bit of logic works great in a Twig template, a big batch of logic belongs in PHP. Remember, Twig is a templating language... it's possible to overuse logic in there, but not recommended. – Lindsey D Feb 26 '15 at 20:24
  • Hi @Jamie, welcome to Stack Exchange! I had to reject your edit because it was better served as a reply to Brad, or as its own spin-off question. If you choose to post it as a fresh question, feel free to link back to this thread for reference! – Lindsey D Feb 27 '15 at 21:41
  • I don't think I was able to reply, can you do it since I now don't have the conent – Jamie Feb 28 '15 at 17:44
  • Just sent you the original content. – Lindsey D Feb 28 '15 at 22:56
5

From what I've seen so far of Craft and plugins developed for it, the requests are typically forwarded directly to Twig templates and those templates are responsible for analysing query strings and request data and fetching data from exposed template variables or built in "entries".

Not necessarily required. You can register a custom CP route in your plugin and have a request routed directly to your plugin's controller action.

public function registerCpRoutes()
{
    return array(
        'path' => array('action' => 'pluginHandle/controllerAction')
    );
}

And have that controller render a the template directly, passing in whatever variables it needs:

$this->renderTemplate('pluginHandle/template', array('foo' => 'bar');
| improve this answer | | | | |
4

My question is, is this a typical and recommended approach to using Craft, or should I be using controllers to handle tasks such as filtering and fetching data and passing it to a view?

This is a difficult question to respond to, because there is no definite answer and any advice would be coloured by opinion. I'll share my thoughts on the matter, for what its worth.

Typical – probably, I guess. Twig is a powerful templating language, and with Craft's own extensions on top its quite possible – and often effective – to build fairly complex stuff using only templates.

At a glance, the official docs may even give the impression that Craft is primarily a template driven platform, given that they place a certain emphasis on templating as a starting point for building your site. If this is true, I'd guess that it has something to do with the fact that for many people, templating would be the easier approach to building a website, compared to writing plugins implementing custom services and controllers (though Craft makes the latter quite easy, as well). A lower bar is nice, and unlike certain other platforms, this approach isn't necessarily a messy one, either. Your templating structure, filenames, whether you want to use controller templates, wrappers, partials etc. is totally up to you. Craft makes few assumptions.

Recommended – it all depends. Not only on how you like to structure your applications and code, but also, of course, on the nature of what you're building.

I totally agree that having too much business logic in your views isn't really best practice, and at some point you'll definitely be better off shifting some or all of your code to a plugin. Even if you're not uncomfortable with doing logic based tasks in your views, Twig does have its limits – it's a templating language, after all. From your question, it sounds to me like you'll quickly feel at home with Craft's plugin framework, which enables you to create an application complete with all the services, controllers and whatever else you'll need. Craft's codebase (built on Yii, as you probably know) is well thought out and super consistent, and in my opinion a joy to work with.

| improve this answer | | | | |

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.