We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44891
    • 35 Posts
    I'm trying to get the Syntaxhighlighter to work but I'm not very succesfull. This is the example code I've got in a resource:

    <!DOCTYPE html>
    <html>
    <body>
    [[SyntaxHighlighter? &brushes=`css,php`]]
    <h2>Example PHP</h2>
    
    <pre class="brush: php"><fixedpre>
    <?php
    $t = date("H");
    echo "<p>The hour (of the server) is " . $t; 
    echo ", and will give the following message:</p>";
    
    if ($t < "10") {
        echo "Have a good morning!";
    } elseif ($t < "20") {
        echo "Have a good day!";
    } else {
        echo "Have a good night!";
    }
    ?>
    </fixedpre></pre>
    </body>
    </html>
    


    I just use an empty template for simplicity because all the code is in the resource page. My first try was enabling the plugin but that didn't do the job. I disabled it and used the snippet (as you can see in the above code). No highlighting however. Just plain text.


    I read the tutorial from bobsguides and followed the instructions. What am I missing?

    Using: MODX Revolution 2.5.7-pl
    php version: 7.1
      • 17301
      • 932 Posts
      I think I remember having some issues also with this. I remember I replaced the css and js for syntaxhighlighter with the latest versions from a cdn and you also need a capital P when calling php in your syntaxhighlighter snippet. [ed. note: lkfranklin last edited this post 6 years, 4 months ago.]
        ■ email: [email protected] | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • 44891
        • 35 Posts
        I got it working. Thanks LK, for your good memory and that you had some issues with it. Your suggestion did the trick. I removed the plugin (sorry Bob!) and created a template with the latest version from the CDN https://cdnjs.com/libraries/SyntaxHighlighter. Hereby the template and the resource.

        The template:
        <!DOCTYPE html>
        <html lang="en">
        	<head>
        		<meta charset="[[++modx_charset]]">
        		<meta content="IE=edge" http-equiv="X-UA-Compatible">
        		<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport'>
        		<title>Syntax highlighter</title>
        		<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shThemeDefault.css" />
        		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shCore.css" />
        		<script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.js"></script>
        		<script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shAutoloader.js"></script>
        		<script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushJScript.js"></script>
        		<script src="https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushPhp.js"></script>
        <script type="text/javascript">
        SyntaxHighlighter.all()
        </script>	
        </head>
        
        <body>
        
        [[*content]]
        
        </body>
        </html>
        


        In my resource some examples:

        The resource:
        <h2>Example javascript highlighter with script-tag</h2>
        
        <script type="syntaxhighlighter" class="brush: js"><![CDATA[
          /**
           * SyntaxHighlighter
           */
          function foo()
          {
              if (counter <= 10)
                  return;
              // it works!
          }
        ]]></script>
        
        <h2>Example php highlighter with pre-tag AND the modx fixedpre-plugin</h2>
        <pre class="brush: php"><fixedpre>
        <?php
        $t = date("H");
        echo "<p>The hour (of the server) is " . $t; 
        echo ", and will give the following message:</p>";
         
        if ($t < "10") {
            echo "Have a good morning!";
        } elseif ($t < "20") {
            echo "Have a good day!";
        } else {
            echo "Have a good night!";
        }
        ?>
        </fixedpre></pre>
        
        <h2>Example php highlighter with script-tag</h2>
        
        <script type="syntaxhighlighter" class="brush: php"><![CDATA[
        <?php
        // define variables and set to empty values
            $name = $email = $message = "";
            if ($_SERVER["REQUEST_METHOD"] == 'POST')
                {
                    $name = test_input($_POST["name"]);
                    $email = test_input($_POST["email"]);
                    $message = test_input($_POST["message"]);
                }
         
            // Check fields with function test_input()
            function test_input($data)
                {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
                }
        ?>
        ]]></script>
          • 17301
          • 932 Posts
          Glad you got it got sorted. You could still use the plug in but place the cdn links into the chunks that way you can still call the snippet as and when needed rather than sing a dedicated template for it. Whichever best fits your purpose though smiley
            ■ email: [email protected] | ■ website: https://alienbuild.uk

            The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
            • 3749
            • 24,544 Posts
            FWIW, I'm still using the un-modified Syntax Highlighter plugin at Bob's Guides. AFAIK, it's still working, though I actually prefer the Crayon Syntax Highlighter plugin for WordPress, but I've been too lazy to convert it for MODX.

            With Crayon, when you hover over the code, it displays any code that's cut off on the right side.
              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
              • 44891
              • 35 Posts
              Thanks for thinking with me guys. Always nice to get help with good suggestions.

              @Bob, I use Crayon on my wordpress site. Very nice syntax highlighter but it's slowing down the website a little bit so I'm having a look at the gisthub gist to use syntax highlightning.
                • 3749
                • 24,544 Posts
                Any JS Syntax Highlighter will slow down page loads, especially if there's a lot of code to be highlighted. Crayon may be worse than others, I've never checked.

                FYI -- If you're posting code, be sure it can be cut and pasted without editing. I've been burned by this with some syntax highlighters.
                  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
                  • 17301
                  • 932 Posts
                  On a side note from this does anyone know how to stop syntax highlighter from removing it's style on double click? Works fine here on the forum as double clicking does nothing. Not really an issue just more of an annoyance smiley
                    ■ email: [email protected] | ■ website: https://alienbuild.uk

                    The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
                    • 3749
                    • 24,544 Posts
                    As the author of the MODX version, I'm embarrassed to admit that I didn't know it did that. wink

                    I see that you can restore the highlighting by clicking outside the code.

                    There may be certain situations where that would be useful for cutting and pasting. Gmail will display the color highlighting, but not the line numbers. You might not always want that.



                      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
                      • 17301
                      • 932 Posts
                      Ah I'm just a double click fiend. So if I wanted to select a certain segment of the code i may double click then triple click to select that line but doing that in syntax highligher selects all and removes the styling and if its a lengthy text of code it'll scroll you to the bottom and you won't be able to scroll up. As say it's not really a problem though just an annoyance for people like me who click about too much ;P
                        ■ email: [email protected] | ■ website: https://alienbuild.uk

                        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.