Answers.Body: <blockquote>
    <p>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".</p>
  </blockquote>
  
  <p>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.</p>
  
  <pre><code>public function registerCpRoutes()
  {
      return array(
          'path' =&gt; array('action' =&gt; 'pluginHandle/controllerAction')
      );
  }
  </code></pre>
  
  <p>And have that controller render a the template directly, passing in whatever variables it needs:</p>
  
  <pre><code>$this-&gt;renderTemplate('pluginHandle/template', array('foo' =&gt; 'bar');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-26T22:00:20.373Z
Answers.LastActivityDate: 2015-02-26T22:00:20.373Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <blockquote>
    <p>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?</p>
  </blockquote>
  
  <p>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.</p>
  
  <p><em>Typical</em> – 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.</p>
  
  <p>At a glance, <a href="https://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">the official docs</a> 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 <em>totally</em> up to you. Craft makes few assumptions.</p>
  
  <p><em>Recommended</em> – 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.</p>
  
  <p>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 <a href="https://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">Craft's plugin framework</a>, 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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-26T23:27:27.820Z
Answers.LastActivityDate: 2015-02-26T23:33:47.910Z
Answers.LastEditDate: 2015-02-26T23:33:47.910Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I'm assessing Craft's suitability for my next project, coming from using HMVC in CodeIgniter with a custom CMS.</p>
  
  <p>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.</p>
  
  <p>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".</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Are there any plugins that I could look at for reference that could help me to understand the flow and process?</p>
  
  <p>Thanks</p>
  
  <p>Jamie</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-26T20:24:54.117Z
Comments.Text: 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.
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-27T21:41:16.213Z
Comments.Text: 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!
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-28T17:44:51.160Z
Comments.Text: I don't think I was able to reply, can you do it since I now don't have the conent
Comments.UserDisplayName: Jamie
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-28T22:56:58.653Z
Comments.Text: Just sent you the original content.
Comments.UserDisplayName: Lindsey D
Tags: <templating><controller>
Title: Is it typical that data is fetched from within templates rather than in controllers?
ThreadText: # Is it typical that data is fetched from within templates rather than in controllers?
  
  (Asked by: username_0 on 2015-02-26)
  
  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
  
  username_0
  
  **Tags:** templating, controller
  
  > **Comments:**
  > **username_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.  
  > **username_1**: Hi @username_0, welcome to Stack Exchange! I had to reject your edit because it was better served as a reply to username_2, 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!  
  > **username_0**: I don't think I was able to reply, can you do it since I now don't have the conent  
  > **username_1**: Just sent you the original content.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-02-26. Score: 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');
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2015-02-26. Score: 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](https://buildwithcraft.com/docs/plugins/introduction) 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](https://buildwithcraft.com/docs/plugins/introduction), 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.
  
  
  ---
  
  