View Helper vs Partial

I have a form I’m creating, to enter data into a table that includes references to two other tables.

Table A is for widgets, includes fields for name, description, and price.
Table B is for groups, and is a relational table with its own index and fields for group name and widget id.

I want to make a form that displays form fields for widgets, but to the side has a table listing of groups for that widget. There are other tables that operate the same way and I’d like to create some reusable code.

  1. I can create the phtml file and reference everything specifically.
  2. I can create a custom view helper (I’ve done previously).
  3. I can create a partial to be used elsewhere (I’ve done this with common fields between multiple forms)

I’m looking for pros & cons, best practices (primary goal is reusable code), and any other recommendations you may have on the topic.

To give you my perspective, I am biased towards partials. All our apps are based on partials, except for the login screen.
Each partial comes with its own JS.
That way, as you mentioned, we can easily “share/reuse” comment boxes, drop-downs, datatables and what not over several screens.

Therefore, in my opinion, it is easier to share/reuse code, modifications such as add or remove “widgets” can be easily done by removing one liners like this one

<?php echo $this->partial('driver-manager/partial/widgeApplicationTypesDataTable.phtml'); ?>

from your index.phtml, or whatever you name it.

1 Like

Partial is a reusable small template.
ViewHelper is a PHP function that is useful to run at View layer.

Consider example at https://docs.zendframework.com/zend-view/helpers/partial/
There is a partial that generates an HTML list from view-bag variables. The list is a template. It is atomic enough to be independent of any viewmodel (that is, a view tied to a controller of business action). So its too generic to vouch a full View/Action relationship, but too big to be included as a function. @Guidofaecke gives a good case for this.

Notice how those examples also use view helper escapeHtml(). That is a function that works on a single variable at a time, and often has access to the rest of the system. UrlHelper is a frequently used view helper which has tightly coupled dependency access to your entire application to figure out how to build an HTML url (a view concept) using your routing table (app logic concept). And its not going to build large elaborate HTML blobs, just little “adjustments” to variables you push into view or partials.