The intent is simply to have a different background for each page based on the documents alias.
This is the code I’m using in a snippet, that I put into the main template.
$bg = "[*alias*]";
switch($bg) {
case "index":
echo "<body style=\"background: #000 url(images/bg_header_main.jpg) repeat-x 0 0\">";
break;
case "news":
echo "<body style=\"background: #000 url(images/bg_header_news.jpg) repeat-x 0 0\">";
break;
default:
echo "<body style=\"background: #000 url(images/bg_header_main.jpg) repeat-x 0 0\">";
break;
}
The problem is, it parses over index and news, and goes straight to the default.
I’ve echo’d $bg to the browser screen, to make sure $bg is showing the right alias page.
Where am I going wrong and is there a better way of doing this?
-
☆ A M B ☆
- 24,524 Posts
Inside of snippet code you have to use the $modx->documentObject[’alias’] element rather than the [*alias*] tag, which is for using in HTML code.
Rather than writing the body tag, I would have a <style> section in the document head, and return (not echo) the background style:
<style type=text/css>
body {
background: #000 url(images/[!setBackground!]) repeat-x 0 0;
}
</style>
<?php
$bg = $modx->documentObject['alias'];
switch($bg) {
case "index":
return "images/bg_header_main.jpg";
break;
case "news":
return "bg_header_news.jpg";
break;
default:
return "bg_header_main.jpg";
break;
}
return '';
?>
I have used TVs as well when I wanted each section to be styled differently, using a TV with a default of @INHERIT and setting the style I wanted for a section in each parent.
-
☆ A M B ☆
- 24,524 Posts
If not every page has a unique background image that wouldn’t work, as it would be looking for an image for each alias.
Thanks for all your quick replies. You’ve solved my problem.
I agree sirlancelot, your solution is far more elegant and durable.
Mine was a quick fix and a little rushed!
David
Yep, I’m with sirlancelot. I always give the body an id of "p-[*alias*]" in templates too.