senny / sablon

Ruby Document Template Processor based on docx templates and Mail Merge fields.
MIT License
446 stars 128 forks source link

Support for <<NextRecord>> field in Word #138

Closed bdknox closed 4 years ago

bdknox commented 5 years ago

Currently there isn't logic to merge multiple records into a template using Microsoft Word's default fields. Any interest in adding support for NextRecord and filling a template that way? This way a template can be created with Word and used with this gem?

stadelmanma commented 5 years ago

Hi @bdknox I'm not sure I understand your question. The current flow for sablon is to create a template in MS Word using some special syntax in the merge fields depending on what you want to do (see README). Then you process that template against a given context hash either by calling the "sablon" executable with JSON fed into it or via some other ruby code that requires the gem and calls the appropriate methods.

Are you talking about processing the same template multiple times with different values in the context hash (i.e. like you would using mail merge with rows in an excel doc)?

bdknox commented 5 years ago

Hi, sorry for the confusion. I'm requesting to allow the typical Word Syntax in addition to the syntax specific to sablon. So there is an option to pass in a template with either Sablon's syntax or Word's.

To answer your question with this added information, yes processing the template multiple times would be part of the flow similar to Excel. In the case where the context hash has more values than there are records per template it would need to fill out one template and then start a new one from where it previously left off in the context hash.

bdknox commented 5 years ago

@stadelmanma

stadelmanma commented 4 years ago

@bdknox My apologies for the incredibly late response, this one slipped through.

This is possible for simple insertion by adding a new field handler, or more accurately duplicating the insertion handler to not require the leading ='s sign. However, if you do register an insertion handler without a prefix it's likely that none of Sablon's other constructs will work as expected since it will think every mergefield is an insertion point. See field_handlers.rb in the module, essentially all you'd need to do is subclass Sablon::Processor::Document::InsertionHandler and change the initialize method to your desired prefix or no prefix and just use /./ as the pattern.

If you don't use a prefix then you might try modifying the handles? method in your subclass to make it more specific.

stadelmanma commented 4 years ago

As for the <<Next Record>> portion I don't think we want to add that feature. It would be preferable (and easier) to permute/combine the context hash variations in the application code and then just call the render method in a loop for each variant. Thanks for using sablon!