Roots Discourse

Understanding controllers, directives, and filters

sage9
blade
#1

Hi all, I’m reading the 3rd edition of “Theme development with Sage”, trying to adopt Sage 9 (I was already using 8 up till now), but I’m a bit confused about the amount of options we have to do essentially the same things.

Let me give you some examples:

  1. If I need to pass the value of custom field to a template, what’s the more appropriate and efficient way to do it? With a filter or with a controller?

  2. If I just need to display that field in the HTML, should I just create a directive?

  3. If I need to execute some custom PHP logic inside a post, should I use a controller or a directive?

In conclusion, what are the most “appropriate” use for controllers, directives, and filters?
In my first project I’m doing most of what I needed to do with controllers, but I was wondering it other options are equally or more valid and efficient.

Thank you in advance :wink:

1 Like

#2

Hey @ste83! Some quick thoughts below. Hope they help!

Neither; it’s just circumstances and preference. I personally prefer using controllers, but both are fine options.

If you just need to display a value in your template, assuming you’ve already passed the data to the template via a filter or controller, just output it like: {{ $my_value }}.

A directive would be handy if you needed to repetitively do something more complex than just displaying a value, such as if you find yourself constantly checking if the value is defined before echoing it, but it’s not needed to just display the value.

See here for more on how to use Blade for templating (displaying values, directives, etc.): https://laravel.com/docs/5.6/blade

It kind of depends on the specific case, again. To oversimplify, if it’s a larger or more complex task, then I’d say a controller, but if you just want to create your own bit of syntactic sugar to write something more cleanly in a template, then a directive. This package has some examples of things you might use directives for: https://github.com/Log1x/sage-directives. You’ll note that they’re mostly ways to clean up your templates by hiding some more verbose PHP behind shorter and simpler directives, not attempts to perform any significant business logic.

The lines blur for smaller tasks, and there are plenty of things you could do either in a controller or as a directive without any meaningful difference. One way to look at it is to think of directives as ways to extend the templating language, and controllers as a place to prepare your data and put your application logic.

Don’t worry too much about it, and just go with the solution that makes the most sense at the time. You’ll learn from experience, and you can always refactor things if you find a better way.

2 Likes

#3

Thank you, your answer makes a lot of sense :slight_smile:

And I’m happy to hear that one way isn’t necessarily better than another.

If it all depends of preferences (and of course the specific logic involved in each case) I feel a bit more confident using Sage 9 on my next projects.

1 Like

closed #4

This topic was automatically closed after 42 days. New replies are no longer allowed.

0 Likes