We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    Now, just about everyone loves to see their code highlighted nice ’n purty in their editor of choice. The problem with this is MODx doesn’t allow <? or <?php tags in snippets/plugins/modules. So most of us either copy and paste the snippet into MODx and remove the first line or use the include trick. Well, developers rejoice! I was working on NewsListing late one night and discovered that if you make the first line of your snippet //<? or /* <? and have the closing */ lower down (say for a comment header) your code editor will syntax highlight the code and MODx will still run it fine!

    It has been confirmed to work in the following editors:

    Programmer’s Notepad
    Macromedia Dreamweaver
    PHPEdit
    Crimson Editor
    PHP Eclipse

    Please post if it works or doesn’t work in your editor. Thanks!
      • 18397
      • 3,250 Posts
      Mirrored in the Etomite forums here:

      http://www.etomite.org/forums/index.php?showtopic=4763
        • 25663 MODX Staff
        • 12,272 Posts
        Works in TextMate too... nice deductive reasoning my friend. smiley
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 32241
          • 1,495 Posts
          Cannot we just modify the code to accep t<?PHP ?> tag?

          Try replacing the runSnippet function on document.parser.class.inc.php with this one. I haven’t fully tested this, but it’s suppose to strip down the <?PHP, ?<, and ?> before parsing the snippet. Plan to add this on version 0.9.2?

          <?php
          	function runSnippet($snippetName, $params=array()) {
          		if(isset($this->snippetCache[$snippetName])) {
          			$snippet = $this->snippetCache[$snippetName];
          			$properties = $this->snippetCache[$snippetName."Props"];
          		}
          		else { // not in cache so let's check the db
          			$sql = "SELECT * FROM ".$this->getFullTableName("site_snippets")." WHERE ".$this->getFullTableName("site_snippets").".name='".mysql_escape_string($snippetName)."';";
          			$result = $this->dbQuery($sql);
          			if(!$this->recordCount($result)==1) {
          				$row = $this->fetchRow($result);
          				$snippet = $this->snippetCache[$row['name']] = $row['snippet'];
          				$properties = $this->snippetCache[$row['name']."Props"] = $row['properties'];
          			} else {
          				$snippet = $this->snippetCache[$snippetName] = "return false;";
          				$properties = '';
          			}
          		}
          		// BEGIN - Clean up snippet from <?PHP, <?, or ?> tag - Added by Wendy Novianto
          		$snippet = trim($snippet);
          		$php_open_tag = strtolower(substr($snippet, 0, 10));
          		if(strpos($php_open_tag, '<?php') == 0) $snippet = substr($snippet, 5);
          		else if(strpos($php_open_tag, '<?') == 0) $snippet = substr($snippet, 2);
          		$snippet_length = strlen($snippet);
          		if(substr($snippet, $snippet_length-2) == '?>') $snippet = substr($snippet, 0, $snippet_length-2);
          		// ENDED - Clean up snippet from <?PHP, <?, or ?> tag
          		// load default params/properties
          		$parameters = $this->parseProperties($properties);
          		$parameters = array_merge($parameters,$params);
          		// run snippet
          		return $this->evalSnippet($snippet, $parameters);
          	}
          ?>
          
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media
            • 25663 MODX Staff
            • 12,272 Posts
            Here’s what I think would be slightly tighter code for the same function as it gets rid of having to count the length of the string, and skips it entirely if it doesn’t contain <? to begin with:

            <?php
            if ( strncmp( trim($snippet), "<?", 2 ) == 0 ) {
                $snippet = substr(trim($snippet), 2);
                if ( strncmp( $snippet, "php", 3 ) == 0 ) $snippet = substr($snippet, 3);
                if ( substr($snippet, -2, 2) == '?>' ) $snippet = substr($snippet, 0, -2);
            }
            ?>


            update: actually works now... lol
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 32241
              • 1,495 Posts
              I’m still amaze with all the designers in this forum. I never thought that designer would know about coding at all, but I think you’re one of those exception that know about both side of the world (designer & coder) wink

              Btw, it’s a great idea, but there is a few errors in the code that might cause a logic error. The overhead on the code provided above by me is when the code try to do strtolower, substring, strpos, and strlen. If following the logic on the code, the process will only have 1 strtolower, 1 strlen, 2 strpos, and 4 substr at most, if all the the open tag and closing tag does appeared on the snippet. The least, it will have 1 strtolower, 1strlen, 2strpos, and 2 substr.

              I believe there has to be a better way, if I know each function processing time at the top of my head. I believe the processing overhead will not noticeable unless if we have more than 20 snippets on one page, but I’m not sure, because I haven’t done any benchmark testing yet. grin Anyway, if we can enforce this upon saving the snippet, it will be a lot better. I believe the core coder can grab the code provided above and put it on the snippet save processor for the next release, so every snippets will automatically being stripped down from that php tag.
                Wendy Novianto
                [font=Verdana]PT DJAMOER Technology Media
                [font=Verdana]Xituz Media
                • 25663 MODX Staff
                • 12,272 Posts
                I updated it to actually work, using a different function, and trying to minimize the amount of variables used ... that stuff gets tricky!
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 25663 MODX Staff
                  • 12,272 Posts
                  On further consideration, I think we should just put the function in the save processor and be done with it so Mark doesn’t have to remember to strip his tags out any more tongue
                    Ryan Thrash, MODX Co-Founder
                    Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                    • 32241
                    • 1,495 Posts
                    Quote from: rthrash at Apr 12, 2006, 12:55 PM

                    On further consideration, I think we should just put the function in the save processor and be done with it so Mark doesn’t have to remember to strip his tags out any more tongue

                    Yup...
                    So are we doing this just so we can help Mark forgetting to remove the <?PHP tag everytime he make an update to his resources? Will it be worth the hassle? tongue lol grin Just kidding Mark wink
                      Wendy Novianto
                      [font=Verdana]PT DJAMOER Technology Media
                      [font=Verdana]Xituz Media
                      • 25663 MODX Staff
                      • 12,272 Posts
                      LOL ... why do you think I already committed it and it works really well. smiley
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me