Support

Account

Home Forums Gutenberg Difference between render_template and render_callback?

Solving

Difference between render_template and render_callback?

  • Is there any reason to choose between using render_template or render_callback in ACF Blocks, or is the difference more down to cosmetics and/or personal preference?

  • I have found it useful in a custom post type to use render_callback if you choose to have more than one template. For example, I have different templates for the single post view and a grid view of all of my posts. The render_callback function can pick which template to use.

  • I have not played with this much, but I think it’s really a matter of coding style.

    As in @koolkatwebdesigns case she is using a combination of styles.

    Templates, like template parts, let you have imaginably chunks of code. Some like template parts because they can look at a theme folder and immediately know what file is doing what to know where to make changes (as long as the template parts are named well that is). Callback functions can be harder to track down.

    Going back to the other commenters example I could also create one render_template and then do multiple things in that single template based on the ACF fields. Or I can do the same thing in a single callback function. The solution of making a choice and loading different templates instead keeps code more manageable and allows for easier extension. This could also be accomplished by creating additional functions that can be called from the main callback function.

    I generally let my goal dictate what style I’d use in each case. But I do like using templates for the reason I mentioned. I name them well and I can look at the template files and know which one to edit. I try to do this with functions and filters I create, but generally I end up with large files that are doing multiple things and it becomes more difficult to find the callback function I need to edit. Given this I would likely go with a template as my first choice.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.