Mam taką funkcję:
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); }
$code = sprintf($code,$long_htmls[4
][$id],$long_htmls[4
][$id],$html_code); $content = str_replace($long_htmls[0
][$id],$code,$content); }
foreach ($short_htmls[2] as $id => $action)
{
$code = isset($this->inhtmltag_tpl_actions[$action]) ?
$this->inhtmltag_tpl_actions[$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); }
$code = sprintf($code,$short_htmls[3
][$id],$short_htmls[3
][$id],$html_code); $content = str_replace($short_htmls[0
][$id],$code,$content); }
}
Tu przykładowy kod ($content):
<div id="test" tpl:loop="l_li1" class="test123"> 123
<div id="test" tpl:loop="l_li1" class="test123"><div id="a">123
</div></div>
<img src="{src}" tpl:loop="l_img1" alt="{alt}" />
<li tpl:loop="l_li2"><a href="{href}">{name}
</a></li>
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) { ?>
<img src="
<?php echo $l_img1['src']; ?>" alt="
<?php echo $l_img1['alt']; ?>" />
<?php } ?>
<ul>
<?php foreach ($this->get_block_var('l_li2') as $l_li2) { ?>
<li><a href="
<?php echo $l_li2['href']; ?>">
<?php echo $l_li2['name']; ?></a></li>
<?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?