You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes rendering a template to file doesn't have enough flexibility and it is better to render template to string and then put content to file manually via inject_into_file etc.
As a bonus, this method can be easier to use to test templates.
Here is the draft from ChatGPT of what is needed:
# Renders an ERB template from the source path and returns it as a string.## ==== Parameters# source<String>:: the relative path to the source root.# context<Binding>:: binding context for the template (default: instance_eval binding).## ==== Returns# String:: the rendered template content.## ==== Example# render_template("README")#defrender_template(source,context: instance_eval("binding"))source=File.expand_path(find_in_source_paths(source.to_s))capturable_erb=CapturableERB.new(::File.binread(source),trim_mode: "-",eoutvar: "@output_buffer")capturable_erb.tap{ |erb| erb.filename=source}.result(context)end# Gets an ERB template at the relative source, executes it, and makes a copy# at the relative destination. If the destination is not given it's assumed# to be equal to the source removing .tt from the filename.## ==== Parameters# source<String>:: the relative path to the source root.# destination<String>:: the relative path to the destination root.# config<Hash>:: give :verbose => false to not log the status.## ==== Examples## template "README", "doc/README"## template "doc/README"#deftemplate(source, *args, &block)config=args.last.is_a?(Hash) ? args.pop : {}destination=args.first || source.sub(/#{TEMPLATE_EXTNAME}$/,"")content=render_template(source,context: config.delete(:context) || instance_eval("binding"))content=yield(content)ifblockcreate_filedestination,nil,config{content}end
The text was updated successfully, but these errors were encountered:
Sometimes rendering a template to file doesn't have enough flexibility and it is better to render template to string and then put content to file manually via
inject_into_file
etc.As a bonus, this method can be easier to use to test templates.
Here is the draft from ChatGPT of what is needed:
The text was updated successfully, but these errors were encountered: