Smarty {capture}返回空

I'm attempting to create a navigation while using smarty for the first time. I want to display all categories except one which I want to capture and use later on.

I got it to work and get the categories and put them in list. However that one category which is being captured, does not display where I want it to display.

my categories

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

so my code goes like this:

<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}

any idea why is this happening?

I don't think {capture} works inside {if} tags, and if does, I'd advise against it, for better code readability. Try something like this:

<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} 

The idea is that you capture everything and if the category is not 'soccer', you display the captured data; otherwise, you assign it to another capture, to use it in the end, outside the loop. The code assumes that there is a single soccer category, as you initially stated in your question.