I'm preparing an application that will serve several different sites that have a common admin panel (a page with different themes to simplify).
Each of these "themes" will have different needs. For example while all of them show a list of services, some of them will show an associated image too. For the ones who don't I would prefer to avoid the DB calls to fetch them (different logic to render the pages).
In Laravel (a PHP framework) this would be a perfect use for view composers.
What would be the design of such a system in go?
I was thinking in some kind of "hooks" that each theme can register to run functions to fetch and add data for a specific template. There's a better way to do it?
If you pass a list of service objects to the template, you could easily decide inside the template what you want to show. If you decide to show a service's image, you could call the ServiceObject.ImageURL()
(which would then lead to a database call). If you're not calling this function, there will be no database query issued.
A sample using the pongo2 template engine:
{% for service in services %}
{% if page.theme == "simple" %}
<p>We don't show any image in the simple theme.</p>
{% else %}
{# This would lead to a database call and return the image path #}
<img src="{{ service.ImageURL() }}" />
{% endif %}
{% endfor %}
Another idea would be to simply create an individual template per theme (extending the common base template); you don't have check for the theme inside the template then.