We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29030
    • 27 Posts
    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?
      • 28042 ☆ 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.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 4310
        • 2,310 Posts
        <body style="background: #000 url(images/bg_header_[*alias*].jpg) repeat-x 0 0">

        Should do it!
        Look at this thread to see similar question http://modxcms.com/forums/index.php/topic,21464.msg132694.html#msg132694

        David
          • 28042 ☆ 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.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 29030
            • 27 Posts
            Thanks for all your quick replies. You’ve solved my problem.
              • 4310
              • 2,310 Posts
              I was taking swiftouch literally :
              The intent is simply to have a different background for each page based on the documents alias.
              I only offer simple solutions because I’m not able to do otherwise!
                • 27376
                • 576 Posts
                Quote from: bunk58 at Jan 11, 2008, 06:33 PM

                <body style="background: #000 url(images/bg_header_[*alias*].jpg) repeat-x 0 0">

                Should do it!
                Look at this thread to see similar question http://modxcms.com/forums/index.php/topic,21464.msg132694.html#msg132694
                @David,
                This solution would be an ideal use of TVs - [*page_background*]. Then in the Manager make the TV input a combobox display and output plain text.

                I’m a CSS fanatic, so I have my HTML look like this in MODx: [tt]<body class="id-[*id*]">[/tt] and then style it accordingly with any background I choose. This is a more code-based solution (not so user-friendly).

                Either way, I’ve said this before in previous threads: use the ID of the document whenever possible, this number will never change. If you decide to change your alias sometime in the future, you will have to go through and re-code every occurrence of the old alias.
                  • 4310
                  • 2,310 Posts
                  I agree sirlancelot, your solution is far more elegant and durable.
                  Mine was a quick fix and a little rushed!

                  David
                    • 25969
                    • 118 Posts
                    Yep, I’m with sirlancelot. I always give the body an id of "p-[*alias*]" in templates too.