Reputation: 426
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:
CSS and JS files inserted via Requirements::
in the parent (Page
)
controller. I can hardcode Requirements in the current
controller, but to use absolute URLs for the inherited Requirements,
I would need to block()
them and add them again with new URLs.
Images uploaded to /assets in the CMS and inserted into the template via $Image.URL
.
$ThemeDir
in the template. Overriding this with my own ThemeDir()
function in the controller doesn't seem to work.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
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:
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$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 URLsget_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