Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{capture} doesn't working as I expect #135

Closed
yourchoice opened this issue Dec 17, 2015 · 4 comments
Closed

{capture} doesn't working as I expect #135

yourchoice opened this issue Dec 17, 2015 · 4 comments

Comments

@yourchoice
Copy link

{capture} doesn't working as I expect, practically output is null:

Ex: list.tpl

{include 'admin/inc/top.tpl'}
...
{capture 'listingData'}
html...
{/capture}
{include 'admin/inc/listing_data.tpl'}
...
{include 'admin/inc/bottom.tpl'}
<<

and listing_data.tpl:

...
{$smarty.capture.listingData}
....
<<

I mention that in the previous version that worked well

@dytyniuk
Copy link

We have issues with {capture} behavior too.

Template:

{assign var="accountMenu" value=[
    0 => [
        'link' => '/account/',
        'label' => 'Overview'
    ],
    1 => [
        'class' => 'spacer'
    ],
    2 => [
        'link' => '/invoices/',
        'label' => 'Invoices',
        'itemsCount' => $customerDebt,
        'visible' => $customerDebt > 0

    ],
    3 => [
        'link' => '/new-orders/',
        'label' => 'New orders',
        'itemsCount' => $customerNewOrdersCnt
    ],
    4 => [
        'link' => '/orders-history/',
        'label' =>  'Orders history',
        'itemsCount' => $customerOrdersCnt
    ],
    5 => [
        'link' => '/quotes/',
        'label' => 'Quotes',
        'itemsCount' => $customerQuotationsCnt
    ],
    6 => [
        'link' => '/favorites/',
        'label' => 'Favorites',
        'itemsCount' => $customerWishlistCnt
    ],
    7 => [
        'class' => 'spacer'
    ],
    8 =>[
        'link' => '/mailings/',
        'label' => 'Newsletters and notification settings'
    ],
    9 => [
        'link' => '/settings/',
        'label' => 'Personal settings'
    ],
    10 => [
        'class' => 'spacer'
    ],
    11 => [
        'link' => '/account/logout/',
        'label' => '%icon%Logout',
        'icon' => 'icon-logout',
        'class' => 'logout'
    ]
]}
<ul>
{foreach item=menuItem from=$accountMenu}
    {if !empty($menuItem.class)}
        {capture name="itemClass"}class="{$menuItem.class}"{/capture}
    {elseif $menuItem.link eq $currentPageUrl}
        {capture name="itemClass"}class="active"{/capture}
    {else}
        {capture name="itemClass"}{/capture}
    {/if}
    {if !empty($menuItem.linkClass)}
        {capture name="linkClass"}class="{$menuItem.linkClass}"{/capture}
    {else}
        {capture name="linkClass"}{/capture}
    {/if}
    {if !empty($menuItem.icon)}
        {capture name="linkIcon"}<span class="{$menuItem.icon}"></span>{/capture}
    {else}
        {capture name="linkIcon"}{/capture}
    {/if}

    {if !isset($menuItem.visible) || $menuItem.visible} 
    <li {$smarty.capture.itemClass}>
        {if !empty($menuItem.link)}
        <a href="{$menuItem.link}" {$smarty.capture.linkClass}>
            {$menuItem.label|replace:'%icon%':$smarty.capture.linkIcon}
            {if isset($menuItem.itemsCount)}
            <span>({$menuItem.itemsCount|default:0})</span>
            {/if}
        </a>
        {/if}
    </li>
    {/if}
{/foreach}
</ul>

Smarty 3.1.27 renders the following markup:

<ul>
    <li class="active">
        <a href="/account/" >
            Overview
        </a>
    </li>
    <li class="spacer">
    </li>
    <li >
        <a href="/invoices/" >
            Invoices
            <span>(200)</span>
        </a>
    </li>
    <li >
        <a href="/new-orders/" >
            New orders
            <span>(2)</span>
        </a>
    </li>
    <li >
        <a href="/orders-history/" >
            Orders history
            <span>(10)</span>
        </a>
    </li>
    <li >
        <a href="/quotes/" >
            Quotes
            <span>(3)</span>
        </a>
    </li>
    <li >
        <a href="/favorites/" >
            Favorites
            <span>(5)</span>
        </a>
    </li>
    <li class="spacer">
    </li>
    <li >
        <a href="/mailings/" >
            Newsletters and notification settings
        </a>
    </li>
    <li >
        <a href="/settings/" >
            Personal settings
        </a>
    </li>
    <li class="spacer">
    </li>
    <li class="logout">
        <a href="/account/logout/" >
            <span class="icon-logout"></span>Logout
        </a>
    </li>
</ul>

But after update to 3.1.28 the result of any {$smarty.capture}, that came from the {foreach} loop was null:

<ul>   
    <li >
        <a href="/account/" >
            Overview
        </a>
    </li>
    <li >
    </li>
    <li >
        <a href="/invoices/" >
            Invoices
            <span>(200)</span>
        </a>
    </li>
    <li >
        <a href="/new-orders/" >
            New orders
            <span>(2)</span>
        </a>
    </li>
    <li >
        <a href="/orders-history/" >
            Orders history
            <span>(10)</span>
        </a>
    </li>
    <li >
        <a href="/quotes/" >
            Quotes
            <span>(3)</span>
        </a>
    </li>
    <li >
        <a href="/favorites/" >
            Favorites
            <span>(5)</span>
        </a>
    </li>
    <li >
    </li>
    <li >

        <a href="/mailings/" >
            Newsletters and notification settings
        </a>
    </li>
    <li >
        <a href="/settings/" >
            Personal settings
        </a>
    </li>
    <li >
    </li>
    <li >
        <a href="/account/logout/" >
            Logout
        </a>
    </li>
</ul>

Code used to render template:

require_once __DIR__ . '/vendor/autoload.php';

$smarty = new Smarty();

$templateVars = [
    'currentPageUrl' => '/account/',
    'customerDebt' => 200,
    'customerNewOrdersCnt' => 2,
    'customerOrdersCnt' => 10,
    'customerQuotationsCnt' => 3,
    'customerWishlistCnt' => 5,
];
foreach ($templateVars as $variable => $value) {
    $smarty->assign($variable, $value);
}

$smarty->display('capture.tpl');

If I change {capture name="itemClass"} to {capture assign="itemClass"} and use {$itemClass} inside the loop, then everything works normal.

@dytyniuk
Copy link

Ok, I have compared compiled templates of 3.1.27 and 3.1.28. Note the difference in itemClass capture name:

-    <li <?php echo Smarty::$_smarty_vars['capture']['itemClass'];?>
+    <li <?php echo isset($_smarty_tpl->_cache['__smarty_capture']['itemclass']) ? $_smarty_tpl->_cache['__smarty_capture']['itemclass'] : null;?>

Smarty 3.1.28 uses lowercase capture name to echo capture block's content.

@dytyniuk
Copy link

So, the issue can be reproduced using camelCaps name for {capture} block:

{capture name="testMe"}Hello, {$username}{/capture}
{$smarty.capture.testMe|var_dump}

3.1.27 outputs string(14) "Hello, r3ntg3n", 3.1.28 outputs NULL

Compiled templates diff:

-echo var_dump(Smarty::$_smarty_vars['capture']['testMe']);
+echo var_dump(isset($_smarty_tpl->_cache['__smarty_capture']['testme']) ? $_smarty_tpl->_cache['__smarty_capture']['testme'] : null);

@uwetews
Copy link
Contributor

uwetews commented Dec 17, 2015

Thanks for your input. The fix is now in the master branch

@uwetews uwetews closed this as completed Dec 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants