在smarty中处理递归的最佳方法是什么? [英] What is the best way to handle recursion in smarty?

查看:112
本文介绍了在smarty中处理递归的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Smarty中发现了几种处理递归的方法,主要是基于将模板包含到自身中,这似乎是对资源的荒谬浪费.我在Smarty上找到了Messju提出的一种解决方案,该解决方案似乎是正确的-但不支持该解决方案,并且在最新版本的smarty中也失败了:

对于那些问的人:我想巧妙地打印出一个讨论线程,该讨论线程由一组条目定义.如果某个条目具有一个或多个答案,则这些答案将作为该条目的子项列在数组中,依此类推.

array(
    array(
        'id'=>0,
        'headline'=>"My parent headline",
        'body'    =>"My parent body",
        'children'=>array(
            array(
                'id'=>1,
                'headline'=>"My firstChild headline",
                'body'    =>"My firstChild body",
                'children'=>array()
            ),
            array(
                'id'=>2,
                'headline'=>"My secondChild headline",
                'body'    =>"My secondChild body",
                'children'=>array()
            )
        )
    ),
);

嵌套数组具有任意深度,并且每个条目将具有任意数量的子级.对我来说,这是我想在模板范围内做的事情,因为我认为它是纯显示逻辑.我不想在模板之外处理HTML或某些类型的HTML占位符.

我想聪明地将其打印为嵌套列表:

<ul>
    <li>
        <h1>My parent headline</h1>
        <p>My parent body</p>
        <ul>
            <li>
                <h1>My firstChild headline</h1>
                <p>My firstChild body</p>
            </li>
            <li>
                <h1>My secondChild headline</h1>
                <p>My secondChild body</p>
            </li>
        </ul>
    </li>
</ul>

我开始意识到这可能是一个非常个案的问题,所以我想我会写一个聪明的插件来专门处理这个问题,尽管我宁愿有一个全方位的解决方案. /p>

有办法吗?

解决方案

要了解递归,您必须先了解递归..."

开个玩笑.这应该做您想要的:

<?php
/*
* Smarty plugin
* ————————————————————-
* File:     function.recurse_array.php
* Type:     function
* Name:     recurse_array
* Purpose:  prints out elements of an array recursively
* ————————————————————-
*/

function smarty_function_recurse_array($params, &$smarty)
{

if (is_array($params['array']) && count($params['array']) > 0) {
   $markup = '';

   $markup .= '<ul>';

   foreach ($params['array'] as $element) {
      $markup .= '<li>';

      $markup .= '<h1>' . $element['headline'] . '</h1>';
      $markup .= '<p>' . $element['body'] . '</p>';

      if (isset($element['children'])) {
         $markup .= smarty_function_recurse_array(array('array' => $element['children']), $smarty);
      }

       $markup .= '</li>';
   }

   $markup.= '</ul>';

   return $markup;

} else {
   return 'not array';
}
}

将文件放入您的smarty/plugins文件夹.将数组分配给Smarty,然后在模板中调用它,如下所示:

{recurse_array array=$data}

这是制作自定义Smarty函数的不错的教程:

创建自定义Smarty函数

请注意,此示例对您的基础数据结构具有依赖性.另外,请记住,一组异常长或深度嵌套的数据可能真的很慢.管理您的复杂性,妥善记录所有内容,您应该会很好.祝你好运!

I found a couple of ways to handle recursion in Smarty, mostly based on including templates into themselves, which seems like ridiculous waste of resources. I found one solution, by Messju over at Smarty that seemed to be just right - but it is not supported and fails in the latest version of smarty :(

For people asking: What I want smarty to print out is a discussion thread that is defined by an array of entries. If an entry has one or more answers, those are listed as children to said entry in an array, and so on.

array(
    array(
        'id'=>0,
        'headline'=>"My parent headline",
        'body'    =>"My parent body",
        'children'=>array(
            array(
                'id'=>1,
                'headline'=>"My firstChild headline",
                'body'    =>"My firstChild body",
                'children'=>array()
            ),
            array(
                'id'=>2,
                'headline'=>"My secondChild headline",
                'body'    =>"My secondChild body",
                'children'=>array()
            )
        )
    ),
);

The nested array has an arbitrary depth, and each entry will have an arbitrary number of children. To me this is something I want to do with within the scope of the template, as I consider it pure display logic. I do not want to have to handle HTML or some type of HTML placeholders outside of the template.

I want smarty to print this as nested lists:

<ul>
    <li>
        <h1>My parent headline</h1>
        <p>My parent body</p>
        <ul>
            <li>
                <h1>My firstChild headline</h1>
                <p>My firstChild body</p>
            </li>
            <li>
                <h1>My secondChild headline</h1>
                <p>My secondChild body</p>
            </li>
        </ul>
    </li>
</ul>

I'm starting to realize this might be a very case-by-case problem, so I figure I'll just write a smarty plugin to handle this specifically, although I'd rather have an all-around solution.

Is there a way?

解决方案

"In order to understand recursion, you must first understand recursion..."

Just kidding. This should do what you want:

<?php
/*
* Smarty plugin
* ————————————————————-
* File:     function.recurse_array.php
* Type:     function
* Name:     recurse_array
* Purpose:  prints out elements of an array recursively
* ————————————————————-
*/

function smarty_function_recurse_array($params, &$smarty)
{

if (is_array($params['array']) && count($params['array']) > 0) {
   $markup = '';

   $markup .= '<ul>';

   foreach ($params['array'] as $element) {
      $markup .= '<li>';

      $markup .= '<h1>' . $element['headline'] . '</h1>';
      $markup .= '<p>' . $element['body'] . '</p>';

      if (isset($element['children'])) {
         $markup .= smarty_function_recurse_array(array('array' => $element['children']), $smarty);
      }

       $markup .= '</li>';
   }

   $markup.= '</ul>';

   return $markup;

} else {
   return 'not array';
}
}

Place the file into your smarty/plugins folder. Assign your array to Smarty then call it in your template like so:

{recurse_array array=$data}

Here's nice tutorial for making custom Smarty functions:

Creating Custom Smarty Functions

Be aware of the dependency that this example has on your underlying data structure. Also, keep in mind that an unusually long or deeply nested set of data could be really slow. Manage your complexity, keep things well documented, and you should be fine. Good luck!

这篇关于在smarty中处理递归的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆