首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Smarty {capture}返回空

Smarty {capture}返回空
EN

Stack Overflow用户
提问于 2015-06-05 03:02:11
回答 1查看 346关注 0票数 0

我试图创建一个导航,同时第一次使用智能。我想显示所有的类别,除了一个,我想捕捉和使用稍后。

我让它开始工作,得到分类,并把它们列在清单上。但是,正在捕获的一个类别,不显示我希望它显示的位置。

我的分类

代码语言:javascript
复制
Soccer      //
    subCat1 //  those categories should be captured
    subCat2 //
    subCat3 //
Rugby
    subCat1
    subCat2
    subCat3
Netball
    subCat1
    subCat2
    subCat3
etc...

我的代码是这样的:

代码语言:javascript
复制
<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}

知道为什么会这样吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-05 04:54:44

我不认为{capture}{if}标记中工作,如果可以,我建议不要使用它,以获得更好的代码可读性。试着做这样的事情:

代码语言:javascript
复制
<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} 

这样做的目的是捕获所有内容,如果类别不是“足球”,则显示捕获的数据;否则,将其分配给另一个捕获,以便在循环外使用它。代码假设存在一个单一的足球类别,正如您在问题中最初所述。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30657701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档