Return to Snippet

Revision: 60941
at November 20, 2012 00:17 by halk


Updated Code
/**
 * RxClipboard
 * REGEX CLIPBOARD (MIN-TEMPLATE ENGINE)
 * @package ccn
 * @author hskitts
 * @copyright 2012
 * @version $Id$
 * @access public
 */
class RxClipboard extends stdClass{

    /**
     * RxClipboard::cut()
     * 
     * @param mixed $pattern
     * @param mixed $target
     * @param bool $hold_place
     * @return
     */
    function cut($pattern,&$target,$hold_place=false){
        if(!$hold_place){
            $target = preg_replace($pattern,"",$target);
        }else{
            $target = preg_replace($pattern,$hold_place,$target);
        }
        return $this;
    }
    
    /**
     * RxClipboard::copy_between()
     * 
     * @param mixed $haystack
     * @param mixed $start
     * @param mixed $end
     * @param string $mod
     * @return
     */
    public function copy_between($haystack,$start,$end,$mod='s'){
        $pattern ="/$start(.*)$end/$mod";
        $this->matches=array();
        preg_match_all($pattern,$haystack,$this->matches,PREG_SET_ORDER);
        return $this;
    }
    
    /**
     * RxClipboard::go_until_capture()
     * 
     * @param mixed $start
     * @param mixed $until
     * @param mixed $capture
     * @param mixed $context
     * @return
     */
    public function go_until_capture($start,$until,$capture,$context){
        $reg = "/$start(?:(?!$until).)+($capture)/";
        $this->captures=array();
        preg_match_all($reg,$context,$this->captures,PREG_SET_ORDER);
        return $this;
    }
    
    /**
     * RxClipboard::paste()
     * 
     * @param mixed $target
     * @param mixed $glue_point
     * @param mixed $snippet
     * @return
     */
    public function paste(&$target,$glue_point,$snippet) {
    	$pattern = '/'.$glue_point.'/';
    	$target = preg_replace($pattern,$snippet,$target);
        return $this;
    }
    
    /**
     * RxClipboard::register_repository()
     * 
     * @param mixed $lib_id
     * @param mixed $data
     * @return
     */
    public function register_repository($lib_id,$data){
        //CACHE A LIBRARY OF SNIPPETS TO COPY AND PASTE
        $pattern = '/<!\-\-\scut:(\w+)\s\-\->(.*)?<!\-\-\s\/cut:\1\s\-\->/sU';
        $this->matches = array();
        preg_match_all($pattern, $data, $this->matches);
		$this->{$lib_id} = array_flip($this->matches[1]);
        $this->{$lib_id.'_board'} = $this->matches[2];
        return $this;
    }
    
    /**
     * RxClipboard::inPallet()
     * 
     * @param mixed $lib_id
     * @param mixed $id
     * @return
     */
    private function inPallet($lib_id,$id){
        return (boolean) (isset($this->{$lib_id}[$id]));
    }
   
    /**
     * RxClipboard::get()
     * 
     * @param mixed $lib_id
     * @param mixed $id
     * @return
     */
    public function get($lib_id,$id){
    	if ($this->inPallet($lib_id,$id)) {
			$snippet = $this->{$lib_id.'_board'}[$this->{$lib_id}[$id]];
            return $snippet;
		}
    }
   
    /**
     * RxClipboard::assemble()
     * 
     * @param mixed $lib_id
     * @param mixed $list
     * @return
     */
    public function assemble($lib_id,$list){
    $listItems = explode('|',$list);
		$collection = array();
		foreach($listItems as $item) {
			$collection[] = $this->get($lib_id,$item);
		}
		return $collection;
    }
   
    /**
     * RxClipboard::fill()
     * 
     * @param mixed $slot
     * @param mixed $data
     * @param mixed $tmpl
     * @return
     */
    public function fill($slot, $data,&$tmpl) {
		$slot = "#$slot#";
        $tmpl = str_replace($slot,$data,$tmpl);
		return $this;
	}
    
    /**
     * RxClipboard::fillAll()
     * 
     * @param mixed $array
     * @param mixed $tmpl
     * @return
     */
    public function fillAll($array,&$tmpl) {
		foreach($array as $key=>$value) {
		  $this->fill($key,$value,$tmpl);
		}
		return $this;
	}
    
    /**
     * RxClipboard::__call()
     * 
     * @param mixed $key
     * @param mixed $params
     * @return void
     */
    public function __call($key,$params){
        if(!isset($this->{$key})) throw new Exception("Call to undefined method ".get_class($this)."::".$key."()");
        $subject = $this->{$key};
        call_user_func_array($subject,$params);
    }
    
    /**
     * RxClipboard::xerox()
     * 
     * @return
     */
    public function xerox() {
	   return clone($this);
    }
    
}

//EXAMPLES OF USE
/**
* Basic Usage
*/
$RxClip = new RxClipboard(); //instantiate the object

//Load A Library Of Snippets
$snippets = file_get_contents("base/pallets/snippets.php");

/**
* The Snippet File I Use Is Formatted Like This
<!-- cut:h1 -->
<h1>#HEADLINE#</h1>
<!-- /cut:h1 -->

<!-- cut:lead -->
<p class="lead">#CONTENT#</p>
<!-- /cut:lead -->

<!-- cut:bodycopy -->
<p>#TEXT1#
#TEXT2#
#TEXT3#
#TEXT4#
</p>
<!-- /cut:bodycopy -->

* Each Snippet Is Enclosed is specialy formatted comments
* Each having a unique id by which it is identifed.
*/

//Get A Snippet From The Repository
$html = $RxClip->get('snippets','h1');

//Or Assemble A Collection Of Snippets In An Array
$clipboard = $RxClip->assemble("snippets","h1|lead|bodycopy");

//Fill Token Patterns Inside A Snippet
$RxClip->fill("HEADLINE","Regex Clipboard Class",$html);

//Or Use An Array Of Values
$plugs = array(
        "TEXT1"=>" ONE ",
        "TEXT2"=>" TWO ",
        "TEXT3"=>" THREE ",
        "TEXT4"=>" FOUR "
);
$RxClip->fill("HEADLINE","COPY AND PASTE WEB DESIGN",$clipboard[0]);
$RxClip->fill("CONTENT","Dynamically Create Page Templates And Render Them",$clipboard[1]);
$RxClip->fillAll($plugs,$clipboard[2]);

//Render The $clipboard Contents
echo implode(" ",$clipboard);

//Copy All Occurences Of Everything Between Two Strings Or Regular Expressions
$RxClip->copy_between($data,"<a","<\/a>");
//returns all anchor tags in $data as a property of #RxClip->matches which is a
//multi-dimensional array

//Go From A Starting String/Regex Until Another String/Regex Then Capture A String/Regex Pattern
//If you have several spans like this : <span class="ln">$115.95</span>
//You can get the numericl value in them like this:
$RxClip->go_until_capture("<span",'\"ln"\">\$',"[1234567890]{3}\.[123456789]{2}",$data);
//The results are saved as a propery $RxClip->captures and would look like this :

Array
(
    [0] => Array
        (
            [0] => $102.99
            [1] => 102.99
        )

    [1] => Array
        (
            [0] => $110.98
            [1] => 110.98
        )

    [2] => Array
        (
            [0] => $109.99
            [1] => 109.99
        )
}

Revision: 60940
at November 18, 2012 08:23 by halk


Initial Code
/**
 * RxClipboard
 * REGEX CLIPBOARD (MIN-TEMPLATE ENGINE)
 * @package ccn
 * @author hskitts
 * @copyright 2012
 * @version $Id$
 * @access public
 */
class RxClipboard extends stdClass{

    /**
     * RxClipboard::cut()
     * 
     * @param mixed $pattern
     * @param mixed $target
     * @param bool $hold_place
     * @return
     */
    function cut($pattern,&$target,$hold_place=false){
        if(!$hold_place){
            $target = preg_replace($pattern,"",$target);
        }else{
            $target = preg_replace($pattern,$hold_place,$target);
        }
        return $this;
    }
    
    /**
     * RxClipboard::copy_between()
     * 
     * @param mixed $haystack
     * @param mixed $start
     * @param mixed $end
     * @param string $mod
     * @return
     */
    public function copy_between($haystack,$start,$end,$mod='s'){
        $pattern ="/$start(.*)$end/$mod";
        $this->matches=array();
        preg_match_all($pattern,$haystack,$this->matches,PREG_SET_ORDER);
        return $this;
    }
    
    /**
     * RxClipboard::go_until_capture()
     * 
     * @param mixed $start
     * @param mixed $until
     * @param mixed $capture
     * @param mixed $context
     * @return
     */
    public function go_until_capture($start,$until,$capture,$context){
        $reg = "/$start(?:(?!$until).)+($capture)/";
        $this->captures=array();
        preg_match_all($reg,$context,$this->captures,PREG_SET_ORDER);
        return $this;
    }
    
    /**
     * RxClipboard::paste()
     * 
     * @param mixed $target
     * @param mixed $glue_point
     * @param mixed $snippet
     * @return
     */
    public function paste(&$target,$glue_point,$snippet) {
    	$pattern = '/'.$glue_point.'/';
    	$target = preg_replace($pattern,$snippet,$target);
        return $this;
    }
    
    /**
     * RxClipboard::register_repository()
     * 
     * @param mixed $lib_id
     * @param mixed $data
     * @return
     */
    public function register_repository($lib_id,$data){
        //CACHE A LIBRARY OF SNIPPETS TO COPY AND PASTE
        $pattern = '/<!\-\-\scut:(\w+)\s\-\->(.*)?<!\-\-\s\/cut:\1\s\-\->/sU';
        $this->matches = array();
        preg_match_all($pattern, $data, $this->matches);
		$this->{$lib_id} = array_flip($this->matches[1]);
        $this->{$lib_id.'_board'} = $this->matches[2];
        return $this;
    }
    
    /**
     * RxClipboard::inPallet()
     * 
     * @param mixed $lib_id
     * @param mixed $id
     * @return
     */
    private function inPallet($lib_id,$id){
        return (boolean) (isset($this->{$lib_id}[$id]));
    }
   
    /**
     * RxClipboard::get()
     * 
     * @param mixed $lib_id
     * @param mixed $id
     * @return
     */
    public function get($lib_id,$id){
    	if ($this->inPallet($lib_id,$id)) {
			$snippet = $this->{$lib_id.'_board'}[$this->{$lib_id}[$id]];
            return $snippet;
		}
    }
   
    /**
     * RxClipboard::assemble()
     * 
     * @param mixed $lib_id
     * @param mixed $list
     * @return
     */
    public function assemble($lib_id,$list){
    $listItems = explode('|',$list);
		$collection = array();
		foreach($listItems as $item) {
			$collection[] = $this->get($lib_id,$item);
		}
		return $collection;
    }
   
    /**
     * RxClipboard::fill()
     * 
     * @param mixed $slot
     * @param mixed $data
     * @param mixed $tmpl
     * @return
     */
    public function fill($slot, $data,&$tmpl) {
		$slot = "#$slot#";
        $tmpl = str_replace($slot,$data,$tmpl);
		return $this;
	}
    
    /**
     * RxClipboard::fillAll()
     * 
     * @param mixed $array
     * @param mixed $tmpl
     * @return
     */
    public function fillAll($array,&$tmpl) {
		foreach($array as $key=>$value) {
		  $this->fill($key,$value,$tmpl);
		}
		return $this;
	}
    
    /**
     * RxClipboard::__call()
     * 
     * @param mixed $key
     * @param mixed $params
     * @return void
     */
    public function __call($key,$params){
        if(!isset($this->{$key})) throw new Exception("Call to undefined method ".get_class($this)."::".$key."()");
        $subject = $this->{$key};
        call_user_func_array($subject,$params);
    }
    
    /**
     * RxClipboard::xerox()
     * 
     * @return
     */
    public function xerox() {
	   return clone($this);
    }
    
}

Initial URL

                                

Initial Description
https://gist.github.com/4098493
(examples of use below the class code)
This class allows you to load a file of snippets that are really mini-templates copy them from your repository fill the token slots in them the with your data and render them as part of the view.  See example below the code for the format of the snippet file.

Initial Title
Regex Clipboard Class (Mini-Template Engine)

Initial Tags
regex, class, php, template

Initial Language
PHP