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
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();