render_any will ignore render extension in file request path

This commit is contained in:
Mateus Cezário Barreto 2025-11-29 21:28:31 +00:00
commit d1f991d8e7

14
app.rb
View file

@ -8,6 +8,10 @@ class TonTonWeb::App < Sinatra::Base
# Renders a view by the filename without the extension (without a predefined render) # Renders a view by the filename without the extension (without a predefined render)
# TODO: pass this to tonton-web code # TODO: pass this to tonton-web code
def render_any template_name, base_dir, options = {} def render_any template_name, base_dir, options = {}
for extension in ['.markdown', '.erb']
template_name.delete_suffix!(extension)
end
views_root = settings.views || "./views" views_root = settings.views || "./views"
# Search for any file with the given name and any extension # Search for any file with the given name and any extension
@ -29,15 +33,7 @@ class TonTonWeb::App < Sinatra::Base
file_path = "#{base_dir}/#{template_name}" file_path = "#{base_dir}/#{template_name}"
extension = File.extname(filename).delete('.') engine = File.extname(filename).delete('.').to_sym
engine = case extension
when 'md', 'markdown' then :markdown
when 'erb' then :erb
else
# Fallback: try to use the extension name as the method
extension.to_sym
end
# Dynamically call the correct Sinatra method (e.g., erb :hello, markdown :hello) # Dynamically call the correct Sinatra method (e.g., erb :hello, markdown :hello)
# We use 'send' to call the method by its name symbol # We use 'send' to call the method by its name symbol