PRE; $t = new \org\rhaco\Template(); $t->vars("abc", array(array(array("A", "B")), array(array("C", "D")))); $t->vars("xyz", array(1, 2)); eq($result, $t->get($src)); // nest_table $src = '<table rt:param="object_list" rt:var="obj"><tr><td><table rt:param="obj" rt:var="o"><tr><td>{$o}</td></tr></table></td></tr></table>'; $t = new \org\rhaco\Template(); $t->vars("object_list", array(array("A1", "A2", "A3"), array("B1", "B2", "B3"))); eq('<table><tr><td><table><tr><td>A1</td></tr><tr><td>A2</td></tr><tr><td>A3</td></tr></table></td></tr><tr><td><table><tr><td>B1</td></tr><tr><td>B2</td></tr><tr><td>B3</td></tr></table></td></tr></table>', $t->get($src)); // nest_ul $src = '<ul rt:param="object_list" rt:var="obj"><li><ul rt:param="obj" rt:var="o"><li>{$o}</li></ul></li></ul>'; $t = new \org\rhaco\Template(); $t->vars("object_list", array(array("A1", "A2", "A3"), array("B1", "B2", "B3"))); eq('<ul><li><ul><li>A1</li><li>A2</li><li>A3</li></ul></li><li><ul><li>B1</li><li>B2</li><li>B3</li></ul></li></ul>', $t->get($src)); // nest_ol $src = '<ol rt:param="object_list" rt:var="obj"><li><ol rt:param="obj" rt:var="o"><li>{$o}</li></ol></li></ol>'; $t = new \org\rhaco\Template(); $t->vars("object_list", array(array("A1", "A2", "A3"), array("B1", "B2", "B3"))); eq('<ol><li><ol><li>A1</li><li>A2</li><li>A3</li></ol></li><li><ol><li>B1</li><li>B2</li><li>B3</li></ol></li></ol>', $t->get($src)); // nest_olul $src = '<ol rt:param="object_list" rt:var="obj"><li><ul rt:param="obj" rt:var="o"><li>{$o}</li></ul></li></ol>'; $t = new \org\rhaco\Template(); $t->vars("object_list", array(array("A1", "A2", "A3"), array("B1", "B2", "B3"))); eq('<ol><li><ul><li>A1</li><li>A2</li><li>A3</li></ul></li><li><ul><li>B1</li><li>B2</li><li>B3</li></ul></li></ol>', $t->get($src)); // nest_tableul $src = '<table rt:param="object_list" rt:var="obj"><tr><td><ul rt:param="obj" rt:var="o"><li>{$o}</li></ul></td></tr></table>'; $t = new \org\rhaco\Template(); $t->vars("object_list", array(array("A1", "A2", "A3"), array("B1", "B2", "B3"))); eq('<table><tr><td><ul><li>A1</li><li>A2</li><li>A3</li></ul></td></tr><tr><td><ul><li>B1</li><li>B2</li><li>B3</li></ul></td></tr></table>', $t->get($src));
<?php $src = <<<'PRE' {$abc} ABC {$def} DEF {$ghi} GHI PRE; $result = <<<'PRE' 123 ABC 456 DEF 789 GHI PRE; $t = new \org\rhaco\Template(); $t->vars("abc", 123); $t->vars("def", 456); $t->vars("ghi", 789); eq($result, $t->get($src));
/** * テンプレートから内容を取得しセットする * * テンプレートサンプル * <mail> * <from address="*****@*****.**" name="tokushima" /> * <subject>メールのタイトル</subject> * <body> * メールの本文 * </body> * </mail> * * @param string $template_path テンプレートファイルパス * @param mixed{} $vars テンプレートへ渡す変数 * @return $this */ public function set_template($template_path, $vars = array()) { $resource_path = empty($this->resource_path) ? \org\rhaco\Conf::get('resource_path', \org\rhaco\io\File::resource_path('mail')) : $this->resource_path; $template_path = \org\rhaco\net\Path::absolute($resource_path, $template_path); if (!is_file($template_path)) { throw new \InvalidArgumentException($template_path . ' not found'); } if (\org\rhaco\Xml::set($xml, file_get_contents($template_path), 'mail')) { $from = $xml->f('from'); if ($from !== null) { $this->from($from->in_attr('address'), $from->in_attr('name')); } foreach ($xml->in('to') as $to) { $this->to($to->in_attr('address'), $to->in_attr('name')); } $return_path = $xml->f('return_path'); if ($return_path !== null) { $this->return_path($return_path->in_attr('address')); } $notification = $xml->f('notification'); if ($notification !== null) { $this->notification($notification->in_attr('address')); } $reply_to = $xml->f('reply_to'); if ($reply_to !== null) { $this->reply_to($reply_to->in_attr('address')); } $errors_to = $xml->f('errors_to'); if ($errors_to !== null) { $this->errors_to($errors_to->in_attr('address')); } $subject = trim(str_replace(array("\r\n", "\r", "\n"), '', $xml->f('subject.value()'))); $body = $xml->f('body.value()'); $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->subject($template->get($subject)); $this->message(\org\rhaco\lang\Text::plain("\n" . $template->get($body) . "\n")); $html = $xml->f('html'); if ($html !== null) { $html_path = \org\rhaco\net\Path::absolute($resource_path, $html->in_attr('src')); foreach ($html->in('media') as $media) { $file = \org\rhaco\net\Path::absolute($resource_path, $media->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($media->in_attr('src') . ' invalid media'); } $this->media($media->in_attr('src'), file_get_contents($file)); } $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->html($template->read($html_path)); } foreach ($xml->in('attach') as $attach) { $file = \org\rhaco\net\Path::absolute($resource_path, $attach->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($attach->in_attr('src') . ' invalid media'); } $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file)); } return $this; } throw new \InvalidArgumentException($template_path . ' invalid data'); }
<?php $src = '123<rt:comment>aaaaaaaa</rt:comment>456'; $t = new \org\rhaco\Template(); eq('123456', $t->get($src));
<html> <body> \t<a href="{$link('abc/def.html')}">link</a> \t<img src="{$link('media/xyz.jpg')}" /> </body> </html> PRE; $result = <<<PRE <html> <body> \t<a href="http://localhost/resources/abc/def.html">link</a> \t<img src="http://localhost/resources/media/xyz.jpg" /> </body> </html> PRE; $t = new \org\rhaco\Template(); $t->vars('link', $link); eq($result, $t->get($src)); $result_secure = <<<PRE <html> <body> \t<a href="http://localhost/resources/abc/def.html">link</a> \t<img src="http://localhost/resources/media/xyz.jpg" /> </body> </html> PRE; $t = new \org\rhaco\Template(); $t->secure(true); $t->vars('link', $link); eq($result_secure, $t->get($src)); // httpsには置換されない
\t\t<input type="search" name="search" value="hoge" /> \t\t<input type="tel" name="tel" value="000-000-0000" /> \t\t<input type="url" name="url" value="http://rhaco.org" /> \t\t<input type="email" name="email" value="*****@*****.**" /> \t\t<input type="datetime" name="datetime" value="1970-01-01T00:00:00.0Z" /> \t\t<input type="datetime-local" name="datetime_local" value="1970-01-01T00:00:00.0Z" /> \t\t<input type="date" name="date" value="1970-01-01" /> \t\t<input type="month" name="month" value="1970-01" /> \t\t<input type="week" name="week" value="1970-W15" /> \t\t<input type="time" name="time" value="12:30" /> \t\t<input type="number" name="number" value="1234" /> \t\t<input type="range" name="range" value="7" /> \t\t<input type="color" name="color" value="#ff0000" /> \t\t</form> PRE; $t = new \org\rhaco\Template(); $t->vars("search", "hoge"); $t->vars("tel", "000-000-0000"); $t->vars("url", "http://rhaco.org"); $t->vars("email", "*****@*****.**"); $t->vars("datetime", "1970-01-01T00:00:00.0Z"); $t->vars("datetime_local", "1970-01-01T00:00:00.0Z"); $t->vars("date", "1970-01-01"); $t->vars("month", "1970-01"); $t->vars("week", "1970-W15"); $t->vars("time", "12:30"); $t->vars("number", "1234"); $t->vars("range", "7"); $t->vars("color", "#ff0000"); eq($rslt, $t->get($src));