private function parse_inhtmltag_tpl(&$content) { preg_match_all('#(<([\w]+)[^>]*tpl\:(.*?)\="(.*?)"*>)(.*?)((<\/\\2>)|(/\>))#i',$content,$long_htmls); preg_match_all('#<[a-z]+(.*?)tpl\:(.*?)\="(.*?)"(.*?)/\>#i',$content,$short_htmls); foreach ($long_htmls[3] as $id => $action) { $code = isset($this->inhtmltag_tpl_actions[$action]) ? $this->inhtmltag_tpl_actions[$action] : ''; $html_code = str_replace(' tpl:' . $action . '="' . $long_htmls[4][$id] . '"','',$long_htmls[0][$id]); preg_match_all('#\{[a-zA-Z0-9_\-]+\}#',$html_code,$vars); foreach ($vars[0] as $id2 => $html) { $var_name = str_replace(array('{','}'),'',$html); $html_code = str_replace($html,'<?php echo $' . $long_htmls[4][$id] . '[\'' . $var_name .'\']; ?>',$html_code); } } foreach ($short_htmls[2] as $id => $action) { $html_code = str_replace(' tpl:' . $action . '="' . $short_htmls[3][$id] . '"','',$short_htmls[0][$id]); foreach ($vars[0] as $id2 => $var) { $html_code = str_replace($var,'<?php echo $' . $short_htmls[3][$id] . '[\'' . $var_name .'\']; ?>',$html_code); } } }
Tu przykładowy kod ($content):
Generuje:
<div id="test" tpl:loop="l_li1" class="test123"> <div id="a"> 123 </div> </div> <?php foreach ($this->get_block_var('l_li1" class="test123') as $l_li1" class="test123) { ?> <div id="test"><div id="a">123</div> <?php } ?></div> <?php foreach ($this->get_block_var('l_img1') as $l_img1) { ?> <?php } ?> <ul> <?php foreach ($this->get_block_var('l_li2') as $l_li2) { ?> <?php } ?> </ul>
Błędy:
1. Olało tam, gdzie jest nowa linia (jak dałem po # s, zaczęło w innych tagach pobierać za dużo).
2. Złe są parametry dla foreacha drugiego diva.
3. W drugim divie w foreachu wzięło do pierwszego zamknięcia, a w tym przypadku powinno wziąć z drugim.
Co zrobić, aby to działało? I da się jakoś prościej to zapisać, bo mam wrażenie, że takim czymś morduję server?