Access theme settings from Ruby ERB

AI-generated summary

lcor is seeking a way to access theme settings from a Ruby ERB file. Specifically, they want to retrieve footer links set in the Discourse Easy Footer plugin to make them crawlable by search engines.

david confirms that it’s technically possible to access theme settings in ERB files using Theme.find(id).cached_settings["my_setting_name"]. However, he strongly advises against this approach as it can cause site-breaking errors if the theme is modified or uninstalled.

Instead, david suggests using JavaScript to append footer links to the page, which can be crawled by search engines like Google. This approach involves using a simple <script> tag and including theme JavaScript in the crawler view.

Hello,

Would like to know if it’s possible to access theme settings from a an ERB file?

My use case is following:

I would like to retrieve footer links set in Discourse Easy Footer (GitHub - discourse/Discourse-easy-footer · GitHub) and add it in ERB file for footer in order for crawlers to see footer links as well.

Thanks for the help.

Technically yes… if the theme had id=5 you could do something like

<%= Theme.find(5).cached_settings["my_setting_name"] %>

But I would strongly recommend against making core, or a plugin, depend on a specific theme. If the theme is modified or uninstalled, it could cause server errors that would totally break your site.

What crawler are you targetting? Google? If so, Google does run Javascript when crawling, and theme javascript is included in the crawler view. The key thing is that you need to use a simple <script> tag rather than a <script type="text/discourse-plugin">.

So something along these lines should work in the crawler view:

<script>
  document.body.append(`<a href="${settings.my_link}">footer link</a>`)
</script>