SalutBarbu
SalutBarbu

Reputation: 426

Prepend all asset and Requirement paths with a custom base in Silverstripe?

I have a scenario where I need to have one of the pages on my site with all CSS, JS, image and SVG references hardcoded with an absolute URL (https). At the moment, they use relative URLs eg. <img src="$ThemeDir/images/image.jpg" />.

Is there a way I can easily prepend a custom base path to these references, possibly leveraging Silverstripe's URL handling features? The solution would need to handle the following uses:

If this isn't possible, I can maintain a completely separate template for the alternate page, I just thought I would see if there was a solution that required less maintenance.

Thanks!

Upvotes: 0

Views: 174

Answers (1)

SalutBarbu
SalutBarbu

Reputation: 426

I don't think there's an easy/DRY solution for this since it's such an edge case. How I did it in the end:

  • Used Requirements::clear() in the controller and then required the parent controller's combined CSS & JS files (generated in Live mode via combine_files) with https protocol. This way I only reference the individual CSS/JS files in the parent controller
  • Added $AbsoluteBaseURL to the start of any links/image references. For the same thing inside includes on the page, added a variable$Base, eg. <% include Footer Base=$AbsoluteBaseURL %> that would be used to prepend URLs in Footer.ss. Other pages would just include the partial without declaring the variable and thus use relative URLs
  • SVG sprites in external files did not work cross-domain so I embedded the SVGs on the page (get_file_contents in PHP) and created a function svgIcon(icon-name) which outputs just the hash to the SVG <symbol> ID on the page or the full path to the SVG sprite file (and ID hash) on pages that reference where the SVG is not embedded.

Hope that makes sense for anyone that finds themselves with the same problem.

Upvotes: 1

Related Questions