function testBindTemplate() { $tpl = '<div>{name}-{id}</div>'; $tplA = '<div>ALT:{name}-{id}</div>'; $data = $this->getTemplateData(); // basic $rt = Raxan::bindTemplate($data, $tpl); $this->compare(htmlspecialchars($rt), htmlspecialchars('<div>mary-1</div><div>john-1</div>'), 'Basic Template binding'); $rt = Raxan::bindTemplate(array('a', 'b'), '<div>{VALUE}-{INDEX}</div>'); $this->compare(htmlspecialchars($rt), htmlspecialchars('<div>a-0</div><div>b-1</div>'), 'Basic Index Template binding'); // basic alternate $rt = Raxan::bindTemplate($data, array($tpl, $tplA)); $this->compare(htmlspecialchars($rt), htmlspecialchars('<div>mary-1</div><div>ALT:john-1</div>'), 'Alternate Template binding'); // tplAlt option $rt = Raxan::bindTemplate($data, array('tpl' => $tpl, 'tplAlt' => $tplA)); $this->compare(htmlspecialchars($rt), htmlspecialchars('<div>mary-1</div><div>ALT:john-1</div>'), ' Template with tplAlt option'); // tplFirst option $rt = Raxan::bindTemplate($data, array('tpl' => $tpl, 'tplFirst' => '<div>FIRST:{name}-{id}</div>')); $this->compare(htmlspecialchars($rt), htmlspecialchars('<div>FIRST:mary-1</div><div>john-1</div>'), 'Template with tplFirst option'); // tplLast option $rt = Raxan::bindTemplate($data, array('tpl' => $tpl, 'tplLast' => '<div>LAST:{name}-{id}</div>')); $this->compare(htmlspecialchars($rt), htmlspecialchars('<div>mary-1</div><div>LAST:john-1</div>'), 'Template with tplLast option'); }
/** * Attach an event and a callback function to the matched elements events * This method is also used to bind an array or a PDO result set to the matched elements - See Raxan::bindTemplate() * @example Usage:<br /> * <p>$elm->bind($type, $fn);</p> * <p>$elm->bind($type, $data, $fn);</p> * <p>$elm->bind($array, $options); // for use when binding to a datatset or array</p> * @param string $type Event type. Example click * @param mixed $data Optional. Event data to be passed to the callback function * @param mixed $fn Callback function * @return RaxanElement */ public function bind($type, $data = null, $fn = null) { $sel = !$this->_modifiedStack ? $this->_selector : null; if (is_string($type)) { $this->page->bindElements($this->elms, $type, $data, $fn, $sel, false); return $this; } else { // setup template $opt = $data; $data = $type; // swap values; if (!$opt) { $opt = array('tpl' => $this->html()); } else { if (!isset($opt['tpl']) && !isset($opt[0])) { $opt['tpl'] = $this->html(); } } if (isset($this->isUIWidget) && $this->_bindData($data, $opt) === true) { return $this; } // return if handled by ui $rt = Raxan::bindTemplate($data, $opt); // pass rows,options to bindTemplate() return is_string($rt) ? $this->html($rt) : $rt; } }