We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5994
    • 46 Posts
    Hi all,

    I can’t figure out how to get this to work. I’m trying to arrange it so that I can use one header chunk for each of my three templates, each of which uses a different css file. So far, though, all I’m getting is nothing where the [[css]] appears (i.e.: <link href="assets/templates/styles/" rel="stylesheet" type="text/css" />)

    Here’s what’s in my [[css]] snippet:

    <?php
    $templateid = $modx->documentObject[’template’];
    if ($templateid=="1") {
    print ’1.css’;
    } elseif ($templateid=="2") {
    print ’2.css’;
    } elseif ($templateid=="3") {
    print ’3.css’;
    }
    // or load default
    else {
    print ’1.css’;
    }
    ?>


    In the header chunk I call the snippet with:

    <link href="assets/templates/leaves/[[css]]" rel="stylesheet" type="text/css" />

    I’m just trying to make it so I can use one header chunk instead of the three I’m using now. Any help, ideas, suggestions, scolding or anything else is appreciated!

    Cris
      • 3749
      • 24,544 Posts
      First, use return instead of print. The [[css]] tag will be replaced by the return value of the snippet. Print will output the string, but not in the location you want it. You’ll also want to call the snippet uncached: [!css!] -- you don’t want the cached output from the last time the snippet ran.

      Be sure to look at the page in "view source" because nothing in the <head> section is visible otherwise.

      Why not just move the line specifying the CSS file out of the chunk and into the template itself? That would be faster, and I don’t think it would make things any harder to maintain.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 5994
        • 46 Posts
        Thanks Bob!

        You’re right; I’ll just move the css into its respective templates and leave it at that. But also, thanks for the insight into how to do it another way if I really wanted to.

        Cris