RFC: Additional template includes/if logic

This is derived from two recent support posts


Post upgrade we have had to diff several custom templates that we have and are trying to move as much into custom snippets so that we do not need to maintain as many templates. However, if there is a snippet you want to override (and not unlock the stock snippet) you would need to also override the template(s) using the snippet that apply in your environment. To simplify this I’d like to propose shifting the logic around snippets to be something along the lines of if snippet_exists("custom_<snippet_name>") else snippet("<snippet_name>"). This would allow someone to easily override the snippet without needing to also override the template(s) it is used in.

The second thing I’d like to propose (wish I could remember which tool I’m borrowing this idea from…) is adding a couple more template_name + custom lines to the kickstart templates (not using the others and would need to look at them a little to see if it is also applicable). Using kickstart, this would allow for a “early” and “late” block to exist in sections like %pre and %post giving folks more flexibility in where they can insert commands without needing to override full kickstart template. In our use case, we need the early block to insert custom certificates (working around with the postnochroot snippet).

I’m happy to try and make some of these changes but wanted to put this up for discussion before starting any work there.

1 Like

Hi,
I like the idea with trying to render custom snippets first and if not found then render the default ones, we did something similar in the registration templates and I don’t see reason why to not do it elsewhere.

The question is how to do it? Few ideas:

  • Create new macro snippet_exists?('name') and update the templates like <%= snippet_exists?('custom') ? snippet('custom') : snippet('default') %>
  • Update snippet macro to accept name attribute also as array, where the array would be list of the snippet names. Macro will then iterate over the array and try to find the snippet. First snippet found would be rendered.
  • Create new macro snippet_render_first_found (naming duh!) that would accept array of snippet names and render first snippet found. If nothing found than it will raise an error.

I’m liking your second suggestion. I was trying to come up with a way to do it that would still be backwards compatible with the current API. I also like the array idea if we wanted to allow a generic and template specific option such as snippet_if_exitsts([template_name + 'custom post', 'custom post']) as an example where custom post might be shareable across multiple formats (preseed + kickstart) so you don’t need to have two different snippets.

If there are no strong objections to the approach, I’m thinking add the new macro, snippet_exists. This will be reused internally for now, but no reason it can’t be public. Extend both snippet_if_exists and snippet to accept an array in addition to a string. This will allow snippet_if_exists to iterate a list of optional templates and not trigger an error with minimal change to the function. This will also allow snippet to iterate a list and error out if a default template has been removed.

There are probably documentation and test updates that still need to happen but this is along the lines of what I was thinking. Figured this could be a starting point for further design discussions.