douqiaotong8682 2015-06-05 03:02
浏览 17
已采纳

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?

  • 写回答

1条回答 默认 最新

  • dppx9253 2015-06-05 04:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误