We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8394
    • 24 Posts
    I’m needing to get some variable HTML code written into a "header" chunk... hence some PHP code, hence a snippet. Never tried to do this yet, but I have to start some time!

    Here is the code I put into my snippet:

    <?php
    print ’<style type="text/css">’;
    print ’ /*<![CDATA[*/’;
    print ’ #logo {width:100%;height:100px;min-width:400px;background:#FFF url("assets/templates/mytemplates/myclass/images/custom/grass-hill.jpg")}’;
    print ’ /*]]>*/’;
    print ’</style>’;
    ?>


    However, what appears as a result of this (in my final HTML) is as follows:

    <style type="text/css"> /*<![CDATA]>*/</style>

    which is missing a bunch of my text (including part of the CDATA construction and the entire 3rd line). This code works fine if I put it into a test.php file and call it directly (bypassing modx), so I’m fairly certain that something in modx is treating it differently than I expected.

    I’ve looked up the online documentation about snippets to see what I can find concerning special considerations for characters like #, *, etc., but haven’t been able to find anything other than a few very terse paragraphs about how to call snippets.

    Can anyone suggest what I might be doing wrong, and if there is a section of documentation that covers this (honest I’ve looked!), and if this is even the right approach to what I’m trying to accomplish?

    Thanks,

    - Dave
      • 8394
      • 24 Posts
      Sorry... relevant facts:

      modx 1.0.5
      WAMP
      Windows XP SP 3
      Apache 2.2.17
      MySQL 5.1.54
      PHP 5.3.5
      Firefox 3.6.15
        • 22019
        • 390 Posts
        To answer your question tangentially:

        If you’re wanting to output a different background image depending on conditional input, you can do this using a Template Variable (ie [[*customImage]] with the rest of the style declaration in the chunk, or you can use a snippet to set a placeholder in the chunk:

        $customImage = 'assets/templates/mytemplates/myclass/images/custom/grass-hill.jpg';
        // or some method of retrieving the image, eg from $_POST or a dbquery
        
        $output = $modx->toPlaceholder('customImage',$customImage);
        return;
        


        Would mean you could put [[+customImage]] wherever you wanted on that page.

        Or you could return $customImage and not set a placeholder and call the snippet in a specific place.
          Writer > E-consultant > MODx developer || Salesforce || modx 2.x || PHP 5.2.13 || MySQL client 5.0.86
          • 8394
          • 24 Posts
          Yes, that’s exactly what I’m attempting... a different background image as a result of a dbquery (so that users can "customize" the web site according to their own preferences).

          I haven’t yet gotten into setting Template Variables at run time the way you’re describing, so I’ll have to read up on that a bit more, but it looks like a much more efficient way of accomplishing what I’m wanting.

          Thanks for pointing me in that direction!

          - Dave