Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
Dynamically Add justing css in <head>...</head>
System
We have a need to dynamically determine which style sheets are loaded to a page. The criteria is to do a device detection and reduce/alter the client payload (i.e. responsive "light" solution). I don't see where this is supported --- but I am absolutely NOT a component expert.
My apologies if this is a trival question answer in the documentation somewhere; if so i would appreciate a link to the reference. If not, please - any ideas?
Does a prefix or external object or controller have access to the structure - for a page that is not transformed yet? did not seem likely.
Current Version is 7.3.2 on Windows 2008 R2 Enterprise Server; Oracle DB; Tomcat Runtime.
Find more posts tagged with
Comments
Rick Poulin
Assuming you want to do this 100% server-side, the solution is to use a precontroller at the site-level. In this case, you wouldn't be using the site's "resources" panel, or at least not for stylesheets that may not be included.
In the precontroller class, you can inspect the request object and make whatever determinations you want.
[PHP]
StringBuffer yourHtml = new StringBuffer();
String stylesheet = isMobile ? "/path/to/mobile.css" : "/path/to/desktop.css";
yourHtml.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(stylesheet).append("\" />");
context.getPageScopeData().put(RuntimePage.PAGESCOPE_HEAD_INJECTION, yourHtml.toString());
[/PHP]
This tacks on yourHtml at the very end of the </head>, right before the closing tag.
Note: if you're going to do anything more complex than that, or using values that may require XML escaping, I suggest building your HTML using a dom4j Document instead of a StringBuffer.
Migrateduser
Rick
Thanks for the excellent response. Your approach sounds exactly like the solution we needed.
xaxis_extraweek.JPG