Smarty {捕获} returns空

Smarty {capture} returns empty

我是第一次尝试使用 smarty 创建导航。我想显示所有类别,但我想稍后捕获和使用的类别除外。

我让它工作并获取类别并将它们放入列表中。但是,正在捕获的那个类别没有显示在我希望它显示的位置。

我的分类

Soccer      //
    subCat1 //  those categories should be captured
    subCat2 //
    subCat3 //
Rugby
    subCat1
    subCat2
    subCat3
Netball
    subCat1
    subCat2
    subCat3
etc...

所以我的代码是这样的:

<div>
    <!-- deal with category levels -->
    {if empty($category_level)}
        {assign var="category_level" value=1}
    {else}
        {math equation="x + 1" x=$category_level assign="category_level"}                   
    {/if}

    {assign var="captured" value="false"}
    {foreach from=$categories item="category"}

        {if (strstr($category.name, 'Soccer') == true)}

            {assign var="captured" value="y"}
            {assign var="capItem" value="soccerCats"} <!-- used it to see whether the condition was met and this code has been run -- this has been assigned corerctly --> 
            {capture name="soccerCats" assign="soco"} <!-- start capturing if above condition was met -->

        {/if}

        {if $category.level == $category_level && $category.is_visible == "Yes"}
        <ul>
            <li class="{$category.name|htmlspecialchars}Cat"><a href="{$category.category_url}">{$category.name|htmlspecialchars}</a><a class="mobileOnly"><i class="fa fa-angle-down fa-fw"></i></a>
                {if !empty($category.children)}

                        <ul>
                            {assign var="categories" value=$category.children}
                            {if empty($category_level)}
                                {assign var="category_level" value=1}
                            {else}
                                {math equation="x + 1" x=$category_level assign="category_level"}
                            {/if}

                            {foreach from=$categories item="category"}
                                {if $category.level == $category_level && $category.is_visible == "Yes"}
                                    <li><a href="{$category.category_url}">{$category.name|htmlspecialchars}</a></li>
                                {/if}
                            {/foreach}
                            {math equation="x - 1" x=$category_level assign="category_level"}
                            {assign var="category" value=0}
                        </ul>                                                  
                {/if}
            </li>
        </ul>

    {/if}

    {if $captured == "y"}
        {/capture} <!-- end the capture -->
        {assign var="captured" value="n"}
    {/if}  

    {/foreach}
    {math equation="x - 1" x=$category_level assign="category_level"}
    {assign var="category" value=0}
</div>

<!-- I tried to use both ways shown below to display the captured item -->
{$soco}
{$smarty.capture.soccerCats}

知道为什么会这样吗?

我不认为 {capture}{if} 标签内有效,如果有效,我建议不要这样做,以获得更好的代码可读性。尝试这样的事情:

<div>
    <!-- deal with category levels -->
    {if empty($category_level)}
        {assign var="category_level" value=1}
    {else}
        {math equation="x + 1" x=$category_level assign="category_level"}                   
    {/if}

    {foreach from=$categories item="category"}
        {capture name="code"}
        <ul>
            <li class="{$category.name|htmlspecialchars}Cat"><a href="{$category.category_url}">{$category.name|htmlspecialchars}</a><a class="mobileOnly"><i class="fa fa-angle-down fa-fw"></i></a>
                {if !empty($category.children)}

                        <ul>
                            {assign var="categories" value=$category.children}
                            {if empty($category_level)}
                                {assign var="category_level" value=1}
                            {else}
                                {math equation="x + 1" x=$category_level assign="category_level"}
                            {/if}

                            {foreach from=$categories item="category"}
                                {if $category.level == $category_level && $category.is_visible == "Yes"}
                                    <li><a href="{$category.category_url}">{$category.name|htmlspecialchars}</a></li>
                                {/if}
                            {/foreach}
                            {math equation="x - 1" x=$category_level assign="category_level"}
                            {assign var="category" value=0}
                        </ul>                                                  
                {/if}
            </li>
        </ul>    
        {/capture}

        {if (strstr($category.name, 'Soccer') != true)}
            {$smarty.capture.code}
        {else}
            {$soccercats=$smarty.capture.code} {*Updated with @Borgtex's comment*}
        {/if}
    {/foreach}
</div>
{$soccercats} 

想法是捕获所有内容,如果类别不是 'soccer',则显示捕获的数据;否则,您将它分配给另一个捕获,最后在循环外使用它。该代码假定有一个 单一 足球类别,正如您最初在问题中所述。