PHP code example of redcatphp / templix

1. Go to this page and download the library: Download redcatphp/templix library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

redcatphp / templix example snippets


$templix->onCompile(function($templix){  
    $templix->find('footer a')->addClass('footer-link');  
});  
            

// re-compile each time  
// add indentation and line feed for code readability  
$templix->setDevTemplate(true); //default true  
  
// add time suffix parameter to local css links to avoid browser caching  
$templix->setDevCss(true); //default true  
  
// add time suffix parameter to local js scripts avoid browser caching  
$templix->setDevJs(true); //default true  
  
// add time suffix parameter to local images to avoid browser caching  
$templix->setDevImg(true); //default false  
  
// add clean callback when switch from prod to dev  
$templix->setCleanCallback(function(){  
      
});  
            

//set compile directory  
$templix->setDirCompile('.tmp/templix/compile/'); //by default  
  
//set cache directory  
$templix->setDirCache('.tmp/templix/cache/'); //by default  
  
//add mtime directory registry to use when you use "cacheSync" attribute  
$templix->setDirSync('.tmp/sync/'); //by default  
  
//set current working directories in order you want templix look for  
$templix->setDirCwd(['template/','redcat/template/']); //by default  
  
//add directories to stack of current working directories  
$templix->addDirCwd(['template/','redcat/template/',]);  
  
//the template file to display on call of $templix->display();  
$templix->setPath($file);  
            

$prefixDefault = Templix::getPluginPrefixDefault();  
// Templix\\MarkupX\\ , Templix\\MarkupHtml5\\ , Templix\\  
$templix->setPluginPrefix($prefixDefault); //by default  
  
$templix->addPluginPrefix($prefix,$prepend=true); //equivalent to prependPluginPrefix  
$templix->appendPluginPrefix($prefix);  
$templix->prependPluginPrefix($prefix);  
            

$node->closest('form');  
//will find the enclosing form if there is one  
            

$index = $node->searchNode($searchingNode);  
$index = $node->searchNode($searchingNode,2);  
            

$node->match('#node-id');  
$node->match('a.my-class');  
$node->match('a[href="templix"]');  
            

foreach($node->find('img') as $img){  
    if(!$img->alt)  
        $img->alt = basename($img->src);  
}  
  
$node->find('a',0); //will get the first node of results  
$node->find('a',1); //will get the second node of results  
  
$allA = $node->find('ul>li',true)->find('a');  
$node->find('ul>li',true)->remove();  
            

foreach($node->children('img') as $img){  
    if(!$img->alt)  
        $img->alt = basename($img->src);  
}  
  
$node->children('a',0); //will get the first node of 'a' children  
$node->children('a',1); //will get the second node of 'a' children  
  
$childrenA = $node->children('ul>li',true)->children('a');  
$node->children('ul>li',true)->remove();  
            

$node->merge($otherNode);  
$node->merge('</li>unique element</li>');  
            

$node->premerge($otherNode);  
$node->premerge('</li>unique element</li>');  
            

$node->submerge($otherNode);  
$node->submerge('<ul></li>unique element</li></ul>');  
            

var\_dump( $node===$node ); //true  
var\_dump( $node->isSameNode($node) ); //true  
  
var\_dump( $node===$otherNode ); //false  
var\_dump( $node->isSameNode($otherNode) ); //true  
            

$attributes = $node->getAttributes();  
            

if($node->hasAttribute('title')){  
      
}  
            

$divs = $node->getElementsByTagName('div');  
            

$node->write($nodes);  
$node->write('<span>New inner content</span>');  
$node->write('New inner content');  
            

$node->append($nodes);  
$node->append('<span>New appending content</span>');  
$node->append('New appending content');  
            

$node->prepend($nodes);  
$node->prepend('<span>New prepending content</span>');  
$node->prepend('New prepending content');  
            

$node->find('a',true)->each(function($a){  
    echo $a->href."<br>";  
});  
            

$node->replaceWith($otherNode);  
$node->replaceWith('<div>replaced</div>');  
            

$node->remove();  
            

$node->applyFile($filename);  
            

$node->before($nodes);  
$node->before('<span>Before content</span>');  
$node->before('Before content');  
            

$node->after($nodes);  
$node->after('<span>After content</span>');  
$node->after('After content');  
            

$index = $node->getIndex();  
            

$string = $node->getInnerMarkups();  
            

$string = $node->getInner();  
            

$string = $node->\_\_toString();  
$string = (string)$node;  
$string = $node.'';  
            

$node->clear();  
            

$node->clearInner();  
            

$node->head(' echo time();

$node->head(' echo time();

$node->innerHead(' echo time();

$node->innerFoot(' echo time();

$node->attr('src','images/logo.png');  
$src = $node->attr('src');  
            

$node->tmpAttr('meta-data',$data);  
$meta = $node->tmpAttr('meta-data');  
            

$node->removeAttr('class');  
            

class Mynode extend \\RedCat\\Templix\\Markup{  
    function load(){  
        $this->remapAttr('src');  
        $this->remapAttr('async',1);  
        $src = $this->src;  
        $async = $this->async;  
    )  
}  
            

$node->data('meta-data',$data);  
$meta = $node->data('meta-data');  
            

$height = $node->css('height');  
if(substr($height,-2)=='px')  
    $height = substr($height,0,-2);  
$node->css('height',($height+120).'px');  
            

$node->removeClass('my-class');  
            

$node->addClass('my-class');  
            

$node->wrap($wrapperNode);  
$node->wrap('<div class="my-wrapper" />');  
            

$node->unwrap();  
$node->unwrap('div.my-wrapper');  
            

$div = $node->createChild('<div />');  
            

$node->recurse(function($subnode,&$break){  
    if($subnode->attr('data-find')){  
        $break = true; //break the recursion  
    }  
});  
            

$node->arecurse(function($subnode,&$break){  
    if($subnode->attr('data-find')){  
        $break = true; //break the recursion  
    }  
});  
            

foreach($node as $childNode){  
      
}  
            

$node[] = $appendNode;  
$node[] = '<a href="http://redcatphp.com">RedCat Framework</a>';  
$node[5] = $indexedNode;  
            

$node->append($appendNode);  
$node->append('<a href="http://redcatphp.com">RedCat Framework</a>');  
$node->append($indexedNode,5);  
            

$childNode4 = $node[4];  
            

$childNode4 = $node->children('\*',4);  
            

if(isset($node[0])){  
      
}  
            

if(!is\_null($node->children('\*',0))){  
      
}  
            

unset($node[1]);  
            

$node[1]->remove();  
            

$src = $node->src;  
            

$src = $node->attr('src');  
            

$node->src = $src;  
            

$node->attr('src',$src);  
            

if(isset($node->src)){  
      
}  
            

if($node->hasAttribute('src')){  
      
}  
            

unset($node->src);  
            

$node->removeAttr('src');  
            

$node('ul>li>a')->addClass('my-class');  
            

$node->find('ul>li>a',true)->addClass('my-class');  
            

class Mymarkup{  
    function loadSrc($v){  
        var\_dump( $v ); //will output 'path.ext'  
    }  
    function loadData\_meta($v){  
        var\_dump( $v ); //will output 'Some metadata'  
    }  
    function loadData\_($v,$k){  
        var\_dump( $v ); //will output 'Some metadata' and 'Other metadata'  
        var\_dump( $k ); //will output 'meta' and 'other'  
    }  
}  
            

touch('.tmp/sync/tracks.sync');  


$templix->setDirSync('.tmp/sync/'); //by default  
            

$templix['outsideVar'] = true;  
            

$templix->addPluginPrefix('RedCat\\Plugin\\Templix\\Markup\\');  
            

namespace RedCat\\Plugin\\Templix\\Markup;  
class Markdown extends \\RedCat\\Templix\\Markup {  
    protected $hiddenWrap = true;  
    protected $noParseContent = true;  
    function load(){  
        $this->remapAttr('file');  
        if($this->file)  
            $text = file\_get\_contents($this->file);  
        else{  
            $text = $this->getInnerMarkups();  
            $x = explode("\\n",$text);  
            foreach($x as &$v)  
                $v = ltrim($v);  
            $text = implode("\\n",$x);  
        }  
        $this->clearInner();  
          
        $md = new Parsedown();  
        $this->innerHead[] = $md->text($text);  
    }  
}  
            

namespace RedCat\\Plugin\\Templix\\Markup; //here is the namespace prefix  
class My\_markup extends \\RedCat\\Templix\\Markup {}  
        

class Mymarkup extends \\RedCat\\Templix\\Markup {  
    function load(){  
        var\_dump( $this->getAttributes() );  
    }  
    function loaded(){  
        var\_dump( $this->closest('div')->getAttributes() );  
    }  
}  
            

class Mymarkup extends \\RedCat\\Templix\\Markup {  
    protected $hiddenWrap = true;  
    protected $noParseContent = true;  
}  
            
html
<?$name = 'Jo';
html
<ul cacheSync="tracks">  
<foreach "$model->getTracks() as $k=>$v">  
    <li><?=$k
html
<ul cacheStatic>  
<foreach "$model->getTracks() as $k=>$v">  
    <li><?=$k
html
<extend>  
    <after main>  
        <img src="img/signature.png">  
    </after>  
    <after "main>article:nth-child(2)">  
        <article>Bla bla bla ...</article>  
    </after>  
</extend>  
            
html
<extend>  
    <append main>  
        <img src="img/signature.png">  
    </append>  
    <append "body>main">  
        <article>Bla bla bla ...</article>  
    </append>  
</extend>  
            
html
<after selector="input[name][type!=checkbox][type!=hidden], select[name], textarea[name]">  
    <?  
        if(isset($formErrors)&&isset($formErrors["{{compile:  
            rtrim(str\_replace(array('[',']'),array('.',''),'{{this:name}}'),'.')  
        }}"])){  
            
html
<extend>  
    <before main>  
        <img src="img/signature.png">  
    </before>  
    <before "main>article:nth-child(2)">  
        <article>Bla bla bla ...</article>  
    </before>  
</extend>  
            
html
<if "$cond" />  
  
<end>  
      
<switch "$switcher" />  
  
<end>  
  
<foreach "$var as $v" />  
  
<end>  
            
html
<?$insideVar = true;
html
<extend>  
    <main>  
        <h1>Hi</h1>  
    </main>  
</extend>  
            
html
<extend "header.xtml">  
    <append "title"> - RedCat</append>  
</extend>  
<extend "footer.xtml">  
    <prepend "footer">RedCat -</prepend>  
</extend>  
            
html
<foreach "$var as $k=>$v">  
     echo $k;
html
<extend>  
    <merge "body>main">  
        <article>Bla bla bla ...</article>  
    </merge>  
</extend>  
            
html
<extend>  
    <submerge "body>main">  
        <article>Bla bla bla ...</article>  
        <article>And Bla bla bla ...</article>  
    </submerge>  
</extend>  
            
html
<?$name = 'Jo';
html
<extend>  
    <write main>  
        <article>Bla bla bla ...</article>  
    </write>  
</extend>