function

最后更新于:2022-04-01 11:03:57

> function指令可以把指定的标签定义为一个函数, 在需要的地方调用
<div class="menu">
    <ul php-function="echo_ul($list)">
        <li php-foreach="$list as $cate">
            <a href="/category/{$cate.name}">{$cate.title}</a>
            <ul php-if="isset($cate['_child'])" php-call="echo_ul($cate['_child'])"></ul>
        </li>
    </ul>
    <ul php-call="echo_ul($list)"></ul>
</div>

解析后

<div class="menu">
    <?php function echo_ul($list) { ?><ul >
        <?php foreach ($list as $cate) { ?><li >
            <a href="/category/<?php echo $cate["name"]; ?>"><?php echo $cate["title"]; ?></a>
            <?php if (isset($cate['_child'])) {  echo_ul($cate['_child']);  } ?>
        </li><?php } ?>
    </ul><?php }  echo_ul($list); ?>
</div>

> 此示例本身就是一个无限级菜单展示的案例, php-call为调用函数传入数据

';