We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4195
    • 398 Posts
    I created an API to help me make my own snippets code a little more compact when it uses the default way of modx templating.
    Maybe you’ll find it usefull (i haven’t posted this yet on the public forums) What do you think? Sorry for the short documentation on this but I just wanted to share this without making it donkey-proof first. Yes this looks like some sort of basic template engine, Modx style wink

    Chunkie Class:
    class CChunkie {
     	var $_tpl;
    	var $_tplvars = array();
    	var $_ds;
    	var $_de;
    	
    	function CChunkie($template = '', $ds = '[+', $de = '+]') {
    		$this->_tpl = $template;
    		$this->_ds = $ds;
    		$this->_de = $de;
    	}
    	
    	function ClearVars() {
    		$this->_tplvars = array();
    	}
    	
    	function CreateVars($value = '', $key = '', $path = '') {
    		$keypath = !empty($path) ? $path . "." . $key : $key;
    	    if (is_array($value)) { 
    			foreach ($value as $subkey => $subval) {
    			 $this->CreateVars($subval, $subkey, $keypath);
                }
    		} else { 
    			$this->_tplvars[$this->_ds.$keypath.$this->_de] = $value;
    		}
    	}
    	
    	function AddVar($name, $value) {
    		$this->CreateVars($value,$name);
    	}
    	
    	function AddPost() {
    		$this->CreateVars($_POST,"post");
    	}
    	
    	function AddGet() {
    		$this->CreateVars($_GET,"get");
    	}
    	
    	function Render() {
    		# print_r($this->_tplvars, true); // remove # for debug
    		return str_replace(array_keys($this->_tplvars), array_values($this->_tplvars),$this->_tpl);
    	}
    	
    
    }
    


    examples:

    Here is a sample that shows all the functionality.

    template
    <h1>[+title+]</h1>
    
    [+txt.string1+]
    
    <form method="post">
       <input type="text" name="text" value="[+post.text+]" />
       <input type="submit" name="submit" value="[+send+]" />   
    </form>
    
    [+txt.string2+]
    
    


    part of snippet code
    
    $title= "This is a title";
    $send = "Click Me";
    
    $array_text = array (
         'string1'=>"this is string one",
         'string2'=>"this is string two"
    )
    
    $objTpl = new CChunkie($template); // $template is a string
    $objTpl->AddVar("send", $send);
    $objTpl->AddVar("txt", $array_text);
    $objTpl->AddPost(); // adds the $_POST object variables, you can use AddGet() to add all the $_GET ones.
    $output = $objTpl->Render(); 
    
      Armand Pondman
      MODx Coding Team
      :: Jot :: PHx
      • 32963
      • 1,732 Posts
      Very nice indeed.
        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Is there an advantage/disadvantage over using $modx->setPlaceholder(’placeholder’, $value) rather than having str_replace() in the snippet code itself?
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 18397
          • 3,250 Posts
          Yes if what your snippet involves something repeating like in Ditto. If you set placeholders and a given document does not have a value of one of them, you get the value of the previous document.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Quote from: Mark at Jul 31, 2006, 09:01 PM

            Yes if what your snippet involves something repeating like in Ditto. If you set placeholders and a given document does not have a value of one of them, you get the value of the previous document.
            This is if you use $modx->setPlaceholder(...)?
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 18397
              • 3,250 Posts
              Yes.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Ouch! Not good! Thank you for that bit of information.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 32241
                  • 1,495 Posts
                  I think there has been several effort in making this type of library, one of them is my chunkTpl library, but a TBS (TinyButStrong) plugin had overtaken the library, and I believe it’s eing used by maximgallery right now, am I right?
                    Wendy Novianto
                    [font=Verdana]PT DJAMOER Technology Media
                    [font=Verdana]Xituz Media
                    • 4195
                    • 398 Posts
                    yes I also created the TBS plugin smiley
                    but to be honest.. i want to stick als close to modx as possible and this is just a simple class to get rid of repeating code when doing the templating.
                      Armand Pondman
                      MODx Coding Team
                      :: Jot :: PHx
                      • 32241
                      • 1,495 Posts
                      Quote from: bS at Aug 02, 2006, 11:54 AM

                      yes I also created the TBS plugin smiley
                      but to be honest.. i want to stick als close to modx as possible and this is just a simple class to get rid of repeating code when doing the templating.

                      It shows that it’s been quite sometime for me to keep track of all the progress in MODx community.
                        Wendy Novianto
                        [font=Verdana]PT DJAMOER Technology Media
                        [font=Verdana]Xituz Media