/** * Set template * @param string $template_path Relative path from resource_path * @param mixed{} $vars bind variables * @return $this */ public function set_template($template_path, $vars = []) { /** * @param string $path Email template resources root */ $resource_path = \ebi\Conf::get('resource_path', \ebi\Conf::resource_path('mail')); $path = \ebi\Util::path_absolute($resource_path, $template_path); if (!is_file($path)) { throw new \ebi\exception\InvalidArgumentException($template_path . ' not found'); } $xml = \ebi\Xml::extract(file_get_contents($path), 'mail'); try { try { $from = $xml->find_get('from'); $this->from($from->in_attr('address'), $from->in_attr('name')); } catch (\ebi\exception\NotFoundException $e) { } foreach ($xml->find('to') as $to) { $this->to($to->in_attr('address'), $to->in_attr('name')); } try { $this->return_path($xml->find_get('return_path')->in_attr('address')); } catch (\ebi\exception\NotFoundException $e) { } /** * @param string $xtc_name xtc query key */ $xtc_name = \ebi\Conf::get('xtc_name', 'xtc'); $xtc = self::xtc($template_path); $this->header['X-T-Code'] = $xtc; $vars['t'] = new \ebi\FlowHelper(); $vars['xtc'] = [$xtc_name => $xtc]; $subject = trim(str_replace(["\r\n", "\r", "\n"], '', $xml->find_get('subject')->value())); $template = new \ebi\Template(); $template->cp($vars); $body_xml = $xml->find_get('body'); $signature = $body_xml->in_attr('signature'); $signature_text = ''; if (!empty($signature)) { $sig_path = \ebi\Util::path_absolute($resource_path, $signature); if (!is_file($sig_path)) { throw new \ebi\exception\InvalidArgumentException($signature . ' not found'); } $sig_xml = \ebi\Xml::extract(file_get_contents($sig_path), 'mail'); $signature_text = \ebi\Util::plain_text(PHP_EOL . $sig_xml->find_get('signature')->value() . PHP_EOL); } $message = $template->get(\ebi\Util::plain_text(PHP_EOL . $body_xml->value() . PHP_EOL) . $signature_text); $this->message($message); $this->subject($template->get($subject)); try { $html = $xml->find_get('html'); $html_path = \ebi\Util::path_absolute($resource_path, $html->in_attr('src', preg_replace('/^(.+)\\.\\w+$/', '\\1', $path) . '.html')); foreach ($html->find('media') as $media) { $file = \ebi\Util::path_absolute($resource_path, $media->in_attr('src')); if (!is_file($file)) { throw new \ebi\exception\InvalidArgumentException($media->in_attr('src') . ' invalid media'); } $this->media($media->in_attr('src'), file_get_contents($file)); } $template = new \ebi\Template(); $template->cp($vars); $this->html($template->read($html_path)); } catch (\ebi\exception\NotFoundException $e) { } foreach ($xml->find('attach') as $attach) { $file = \ebi\Util::path_absolute($resource_path, $attach->in_attr('src')); if (!is_file($file)) { throw new \ebi\exception\InvalidArgumentException($attach->in_attr('src') . ' invalid media'); } $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file)); } return $this; } catch (\ebi\exception\NotFoundException $e) { throw new \ebi\exception\InvalidArgumentException($template_path . ' invalid data'); } }
PRE; $t = new \ebi\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 \ebi\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 \ebi\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 \ebi\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 \ebi\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 \ebi\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));
eq($result, $t->get($src)); $src = '<rt:if param="abc">aa<rt:else />bb</rt:if>'; $result = 'bb'; $t = new \ebi\Template(); $t->vars("abc", []); eq($result, $t->get($src)); $src = '<rt:if param="abc">aa<rt:else />bb</rt:if>'; $result = 'aa'; $t = new \ebi\Template(); $t->vars("abc", true); eq($result, $t->get($src)); $src = '<rt:if param="abc">aa<rt:else />bb</rt:if>'; $result = 'bb'; $t = new \ebi\Template(); $t->vars("abc", false); eq($result, $t->get($src)); $src = '<rt:if param="abc">aa<rt:else />bb</rt:if>'; $result = 'aa'; $t = new \ebi\Template(); $t->vars("abc", "a"); eq($result, $t->get($src)); $src = '<rt:if param="abc">aa<rt:else />bb</rt:if>'; $result = 'bb'; $t = new \ebi\Template(); $t->vars("abc", ""); eq($result, $t->get($src)); $src = '<rt:if param="abc">aa<rt:else />bb</rt:if>'; $result = 'bb'; $t = new \ebi\Template(); eq($result, $t->get($src));
<?php $src = '123<rt:comment>aaaaaaaa</rt:comment>456'; $t = new \ebi\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 \ebi\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 \ebi\Template(); $t->secure(true); $t->vars('link', $link); eq($result_secure, $t->get($src)); // httpsにはならない
<input type="search" name="search" value="hoge" /> <input type="tel" name="tel" value="000-000-0000" /> <input type="url" name="url" value="http://tokushimakazutaka.com" /> <input type="email" name="email" value="*****@*****.**" /> <input type="datetime" name="datetime" value="1970-01-01T00:00:00.0Z" /> <input type="datetime-local" name="datetime_local" value="1970-01-01T00:00:00.0Z" /> <input type="date" name="date" value="1970-01-01" /> <input type="month" name="month" value="1970-01" /> <input type="week" name="week" value="1970-W15" /> <input type="time" name="time" value="12:30" /> <input type="number" name="number" value="1234" /> <input type="range" name="range" value="7" /> <input type="color" name="color" value="#ff0000" /> </form> PRE; $t = new \ebi\Template(); $t->vars("search", "hoge"); $t->vars("tel", "000-000-0000"); $t->vars("url", "http://tokushimakazutaka.com"); $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));