CodeMirror as editor + PHP Code

Post Reply
User avatar
lsmithx2
Posts: 3
Joined: Mon Dec 05, 2016 3:23 pm

CodeMirror as editor + PHP Code

Post by lsmithx2 »

Hello,

I am trying to add CodeMirror to the installation of WonderCMS I have setup a plugin file for it but it does not seem to work still. Also I am wondering how to include PHP in the CMS? Is this possible?

The code I have to include the script and css files is:

Code: Select all

<?php
// CODEMIRROR PLUGIN

defined('INC_ROOT') OR die('Direct access is now allowed');

fibreCMS::addListener('js', 'loadCMJS');
fibreCMS::addListener('css', 'loadCMCSS');

function loadCMJS($args) {
	$args = [];
	array_push($args, '<script src="'.fibreCMS::url('plugins/codemirror/codemirror.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/xml-fold.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/xml.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/javascript.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/css.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/php.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/active-line.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/matchbrackets.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/matchtags.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/fullscreen.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/simplescrollbars.js').'"></script>', '<script src="'.fibreCMS::url('plugins/codemirror/scrollpastend.js').'"></script>');
	$script = <<<'EOT'
<script> var editor;
		
	function activateCM()
	{
		if (typeof(editor) == "undefined")
		{
			editor = CodeMirror.fromTextArea(document.getElementsById("content"), {
			lineNumbers: true,
			scrollbarStyle: "simple",
			theme: "brackets",
			styleActiveLine: true,
			mode: "text/html",
			matchBrackets: true,
			matchTags: {bothTags: true},
			extraKeys: {
				"F11": function(cm) {
				  cm.setOption("fullScreen", !cm.getOption("fullScreen"));
				},
				"Esc": function(cm) {
				  if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
				}
			}
			});
		}
	}
		
	function cmFullscreen()
	{
		var button = document.getElementById('enlargeId');
		
		if (editor.getOption("fullScreen"))
		{
			editor.setOption("fullScreen",false);
			
			button.className = button.className.replace(" shrinkClass","");
		}
		else
		{
			editor.setOption("fullScreen",true);
			
			button.className += " shrinkClass";
		}
	}
 </script>
EOT;
	array_push($args, $script);
	return $args;
}

function loadCMCSS($args) {
	array_push($args[0], '<link rel="stylesheet" href="'.fibreCMS::url('plugins/codemirror/css/codemirror.css').'">', '<link rel="stylesheet" href="'.fibreCMS::url('plugins/codemirror/css/fullscreen.css').'">', '<link rel="stylesheet" href="'.fibreCMS::url('plugins/codemirror/css/brackets.css').'">', '<link rel="stylesheet" href="'.fibreCMS::url('plugins/codemirror/css/simplescrollbars.css').'">');
	return $args;
}

Regards,
LSmithx2
User avatar
wiz
Posts: 707
Joined: Sat Oct 30, 2010 12:23 am

Re: CodeMirror as editor + PHP Code

Post by wiz »

Welcome to the forums LSmithx.

You can add PHP code by developing your own plugin as Yassine has given two great examples of how this can be easily achieved.

Another way to include PHP to WonderCMS is to create a functions.php file where your WonderCMS is installed and WonderCMS will include your functions.php file automatically.
User avatar
wiz
Posts: 707
Joined: Sat Oct 30, 2010 12:23 am

Re: CodeMirror as editor + PHP Code

Post by wiz »

EDIT: Adding link to Yassines WYSIWYG Trumbowyg implementation: https://wondercms.com/forum/viewforum.php?f=30
User avatar
lsmithx2
Posts: 3
Joined: Mon Dec 05, 2016 3:23 pm

Re: CodeMirror as editor + PHP Code

Post by lsmithx2 »

Ok I just wondered if there was a way to include a php script in the page itself. CodeMirror allows it to in PHP via {{php}} echo("test"); {{/php}} but then it wont display the php which is in the page.
User avatar
wiz
Posts: 707
Joined: Sat Oct 30, 2010 12:23 am

Re: CodeMirror as editor + PHP Code

Post by wiz »

I'm not familiar with CodeMirror, but there are ways to include actual PHP code into WonderCMS itself. One way is the functions.php file (put your code in a functions.php file in your WonderCMS installation) and WonderCMS will automatically include your functions file.

Depending on your needs, you could also include PHP in the theme.php directly. Would that help?
User avatar
lsmithx2
Posts: 3
Joined: Mon Dec 05, 2016 3:23 pm

Re: CodeMirror as editor + PHP Code

Post by lsmithx2 »

Sort of its just I am trying to write a some php code in different pages.

I have tryed <?php echo("test"); ?> but that does not show and rewrites itself to <!--?php echo("test"); ?-->

I have also tried <? echo("test"); ?> but that didnt work either. I was just trying to implement CodeMirror because it is a text based editor which allows php code. I have installed the other editor but that doesnt allow me to use PHP.
User avatar
wiz
Posts: 707
Joined: Sat Oct 30, 2010 12:23 am

Re: CodeMirror as editor + PHP Code

Post by wiz »

Understood. Unfortunately, executing PHP code in the editable area itself isn't possible, as you've noticed the weird escaping of your opening and closing PHP tags yourself.

Let's think how this can be solved.

EDIT:

To run PHP scripts, you have to save the file as a .php file. You will also need to execute it on a server. You can't run PHP directly from your browser, since PHP is a HTML preprocessor - your browser has nothing to do with PHP, it only gets the HTML that is generated by the server.

So because PHP tags are not valid in HTML files, when not preprocessed by the server, the browser doesn't recognise it, so it automatically converts it to comments since it doesn't know what else to do with it.

The text you're editing in your website is static and fetched from the database.js (JSON database saved in.js format).

Hope this guides you in the right direction.
Post Reply