3 ms·
You're querying from templates? Isn't that considered bad practise? Isn't there a potential problem with template designers querying for loads of data and issui
by colinramsay 14y ago
You're querying from templates? Isn't that considered bad practise? Isn't there a potential problem with template designers querying for loads of data and issuing a slow query?
- bryancinman 14y agoIt could be considered bad practice in some cases. But having the template also act as a controller gives theme developers more freedom to make something outside the golden path. I doubt Wordpress would be used for anything other than simple blogs if it weren't for querying from templates. Obviously there is the risk of theme developers loading tons of queries from a template but I think trying to prevent that would be premature optimization. The benefit of greater customizability outweighs the potential performance hit IMO.
- ericingram 14y agoYou said it better than I could have. Forcing template developers to maintain separate controller code prevents a lot of good work from getting done in my experience. If the project expands and the combined templates become a problem (usually due to the make up of skills in the team, i.e. heavy on back-end developers), separating them is pretty straight forward.
- ericingram 14y agoIt can be abused, but I've seen controller heavy systems abused the same way. It depends on your goals. Some people prefer to write controllers, and the framework also contains a controller pattern for those cases. For example, you could write this... # controllers/BlogController.php class BlogController extends AppController { function index () { $this->entries = get("/channels/blogs/entries"); } } # templates/blog/index.html {foreach $entries as $blog} ... {/foreach} Or, as in my example, this... # templates/blog.html {get $entries from "/channels/blogs/entries"} {foreach $entries as $blog} ... {/foreach} I've written substantial apps both ways, and so far I have found that powerful templates are actually easier to write, read, and maintain, perhaps because the entire request flow happens in a single file.