WBCE CMS Forum

WBCE CMS – Way Better Content Editing.

Du bist nicht angemeldet.

#1 26.09.2017 22:09:36

stefanek
Developer

Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Dazu im Filter
filterDroplets.php

die folgende Zeile einfügen:

	//remove commented droplets 
	// example: [#[Lorem?blocks=6]] 
	// the hash symbol will cause the droplet to be excluded from output 
	$content = preg_replace('/\[\#\[[^]]*]]/', '', $content); 

am besten nach dem str_replace für die p tags, damit keine leeren p-tags stehen bleiben.

Wofür das Ganze?
Manchmal will man Droplets aus welchem Grund auch immer vorübergehend nicht ausgeben lassen. So kann man die einfach auskommentieren und dann, wenn man so will, später wieder einsetzen indem man die Raute wieder rausnimmt.

Beispiel:  [#[Lorem?blocks=6]]
===========================

Hier die ganze Datei:

<?php
/**
 * execute droplets
 * @param string $content
 * @return string 
 */
function doFilterDroplets($content) {
	// check file and include
	$sFile = WB_PATH .'/modules/droplets/droplets.php';
	if(file_exists($sFile)) {
		include_once $sFile;
		
					
		// remove <p> tags that are added by CKE editor every time you 
		// have a droplet in an empty line
  
		if(strpos($content, '<p>[') !== false){
			$content = str_replace('<p>[#[','[#[', $content);
			$content = str_replace('<p>[[','[[', $content);
			$content = str_replace(']]</p>',']]', $content);
		} 

		//remove commented droplets 
		// example: [#[Lorem?blocks=6]] 
		// the hash symbol will cause the droplet to be excluded from output 
		$content = preg_replace('/\[\#\[[^]]*]]/', '', $content); 
		
		// load filter function 
		if(function_exists('evalDroplets')) {
			$content = evalDroplets($content, 'frontend');
		}
	}
	return $content;
}

Edit: noch einmal kurz angepasst. Erweitert um das Ausschneiden der <p> Tags auch bei den auskommentierten Droplets, damit keine leeren <p> Tags bleiben. (Tat schon zuvor, aber nur wenn auch andere, nicht auskommentierte Droplets, irgendwo sonst vorhanden waren.)



Viel Spaß,
Christian

Beitrag geändert von stefanek (26.09.2017 23:01:11)


“Success is the progressive realization of a worthy ideal.” ― Earl Nightingale

Offline

#2 27.09.2017 20:23:15

norhei
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Finde die Idee Super .
Magst Du gleich auch die BE droplets mal anschauen ?
filterDropletsBe.php

Offline

Liked by:

stefanek

#3 27.09.2017 20:25:15

norhei
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Offline

Liked by:

stefanek

#4 27.09.2017 20:31:25

stefanek
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Ich habe es bei mir etwas anders gemacht (Redundanzen sparender).

Statt zwei php Dateien für je FE/BE habe ich nur eine Datei mit folgendem Inhalt:
(siehe Einsatz der $where Variable im Funktionsaufruf.)

<?php 
/**
 * execute droplets
 * @param string $content
 * @param string $where 'frontend' or 'backend'
 * @return string 
 */
function doFilterDroplets($content, $where = 'frontend') {
	// check file and include
	$sFile = WB_PATH .'/modules/droplets/droplets.php';
	if(file_exists($sFile)) {
		include_once $sFile;
		
					
		// remove <p> tags that are added by CKE editor every time you 
		// have a droplet in an empty line
  
		if(strpos($content, '<p>[') !== false){
			$content = str_replace('<p>[#[','[#[', $content);
			$content = str_replace('<p>[[','[[',  $content);
			$content = str_replace(']]</p>',']]', $content);
		} 

		//remove commented droplets 
		// example: [#[Lorem?blocks=6]] 
		// the hash symbol will cause the droplet to be excluded from output 
		$content = preg_replace('/\[\#\[[^]]*]]/', '', $content); 
		
		// load filter function 
		if(function_exists('evalDroplets')) {
			$content = evalDroplets($content, $where);
		}
	}
	return $content;
}

Die Datei filter_routines ruft die Funktion dann folgends auf:

if (OPF_DROPLETS_BE){
                $sFile = $sFilterDirectory.'filterDroplets.php';
		if (file_exists($sFile)) {
			require_once $sFile;
			$content = doFilterDroplets($content, 'backend');
		}
	}

also
$content = doFilterDroplets($content, 'backend'); // für's BE
und
$content = doFilterDroplets($content, 'frontend'); // für's FE


Ich bin da eher der Zen Typ und stehe in dieser Hinsicht auf Minimalismus.  wink

Christian

Beitrag geändert von stefanek (28.09.2017 10:57:09)


“Success is the progressive realization of a worthy ideal.” ― Earl Nightingale

Offline

#5 29.09.2017 17:47:44

norhei
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Dann kannst du Abfragen ob FRONTEND defined ist   , das ist immer gesetzt wenn die Index.php aufgerufen wird  big_smile

Offline

#6 29.09.2017 20:23:02

stefanek
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Könnte man auch. Das oben reicht aber schon.

Was man noch machen könnte ist ein strpos Test bevor die doFilterDroplets Funktion bemüht wird. Gibt ja nicht immer Droplets auf einer Seite.

if(strpos($content, '[[') !== false && function_exists('evalDroplets')){
      // replace droplets with code
      $content = evalDroplets($content, $where);
}

Christian


“Success is the progressive realization of a worthy ideal.” ― Earl Nightingale

Offline

Liked by:

norhei

#7 30.09.2017 23:06:31

norhei
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Eingebaut :-)

Offline

Liked by:

stefanek

#8 01.10.2017 12:33:23

stefanek
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Ich habe grad überlegt: manchmal will man die Droplets im HTML ausgeben lassen, dass also auf der Seite ein Droplet-Aufruf (wie [[Lorem]] z.B.) dargestellt wird.

Sie werden ja immer durch den Code ersetzt und dafür gibt's auch noch keine Handhabung.

Die Lösung wäre nicht besonders schwer, nur wie wäre es denn praktisch, so dass man es sich merken kann und es einfach ist?

Wenn ich das ausgeben will: [[Lorem]]
wäre es einfach, wenn ich im Editor oder sonst wo für die Ausgabe das schreibe:
[\[Lorem]]

oder

[![Lorem]]

oder

[~[Lorem]]

Mir ist die Schreibweise egal, denke aber, dass es sinnvoll ist, sowas zu haben?
Meinungen? Ideen?

Christian


“Success is the progressive realization of a worthy ideal.” ― Earl Nightingale

Offline

#9 01.10.2017 20:28:48

norhei
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

\ default regex escape charakter

Offline

#10 01.10.2017 23:44:39

stefanek
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

OK,

hier ist der Code noch einmal, komplett überarbeitet:

<?php 
/**
 * execute droplets
 * @param string $content
 * @param string $where 'frontend' or 'backend'
 * @return string 
 */
function doFilterDroplets($content, $where = 'frontend') {
	// check file and include
	$sFile = WB_PATH .'/modules/droplets/droplets.php';
	if(file_exists($sFile)) {
		include_once $sFile;
		
		  
		if(strpos($content, '<p>[') !== false){
			// remove <p> tags that are added by CKE editor every time you 
			// have a droplet in an empty line
			$arrReplace = array(
				'<p>[#[' => '[#[', 
				'<p>[['  => '[[',
				']]</p>' => ']]',
			);
			$content = strtr($content, $arrReplace);			
		} 

		if(defined('FRONTEND') && strpos($content, '[#[') !== false){
			//remove commented droplets 
			
			// example FE: [#[Lorem?blocks=6]]
			// the hash symbol will cause the droplet to be excluded from output 
			$content = preg_replace('/\[\#\[[^]]*]]/', '', $content); 
		}
		
		if(strpos($content, '[[') !== false){
			// load evalDroplets function 
			// and replace Droplet calls with their functions
			if(function_exists('evalDroplets')) {
				$content = evalDroplets($content, $where);
			}
		}		
						
		
		if(defined('FRONTEND') && strpos($content, '[\[') !== false){
			// replace escaped droplets and display them 
			// in as correct Droplet calls
			
			// example FE: [\[Lorem?blocks=6]]
			// the backslash symbol will cause the droplet to be shown as is
			$content = preg_replace('/\[\\\\\[(\S+)]*]]/', '[[${1}]]', $content); 
				
		}
	}
	return $content;
}

Ich musste die FRONTEND Konstante jetzt doch benutzen, aber in einem ganz anderen Zusammenhang, nämlich, haben die Backend-Filter die auskommentierten Droplets aus dem Editor "geschluckt"   big_smile

Diese Extras (comment und escape Droplets) gibt es somit nur für das Frontend. Es sei denn jemand hat Lust noch weiter ausgetüftelte RegEx zu schreiben ... doch ich denke nicht, dass jemand oft Droplets im Backend verwendet oder den Wunsch hat, sie zu escapen oder auszukommentieren also übersteigt der Einsatz bei weitem den Nutzen.


Bei diesem Code Ersatz hier:

$arrReplace = array(
	'<p>[#[' => '[#[', 
	'<p>[['  => '[[',
	']]</p>' => ']]',
);
$content = strtr($content, $arrReplace);			

Hab mir gedacht besser einmal strtr des Contents auf das ganze Array als 3 mal ein str_replace...

Auf jeden Fall gut durchgetestet jetzt.

Neue Funktionalität:

escaped Droplets: Droplets im Frontend darstellen.
commented Droplets: Droplets vorübergehend auskommentieren.

Viel Spaß,
Christian

Beitrag geändert von stefanek (02.10.2017 11:11:41)


“Success is the progressive realization of a worthy ideal.” ― Earl Nightingale

Offline

#11 02.10.2017 21:38:51

norhei
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Bin leider grade etwas müde , schau ich mir später nochmal an , wegen der Regex  ein Paar Beispiele in einer Readme.md wären sehr schön.

Oder noch besser im Doxygen Format im Code, das wär ein Traum .

Offline

#12 02.10.2017 21:43:05

stefanek
Developer

Re: Droplets Filter um eine Funktion des Auskommentierens von Droplets erweitern

Kann ich leider nicht mit dienen.
Bin momentan auch ziemlich ausgelastet.
Ich hinterlasse manchmal solche Einfälle nur (wie mit den Droplets), damit sie mir nicht wieder entfallen. Sowas kommt eher spontan als geplant.

Christian


“Success is the progressive realization of a worthy ideal.” ― Earl Nightingale

Offline

Fußzeile des Forums

up