Smarty templates: How to change the order of blocks in child template?
Asked Answered
C

7

6

I would like to change the order of parent blocks in a child templates while using the content of the parent blocks.

Example:

parent template:

{block outer}
    {block a} ... some long content ...{/block}
    {block b} ... some long content ...{/block}
    {block c} ... some long content ...{/block}
{/block}

child template:

{extends file="parent:parent.tpl"}
{block outer}
    {block c} reuse content of parent block "c" {/block}
    {block b} reuse content of parent block "b" {/block}
    {block a} reuse content of parent block "a" {/block}
{/block}

I tried using {$smarty.block.parent} inside block a, b and c:

{extends file="parent:parent.tpl"}
{block outer}
    {block c} {$smarty.block.parent} {/block}
    {block b} {$smarty.block.parent} {/block}
    {block a} {$smarty.block.parent} {/block}
{/block}

In this case {$smarty.block.parent} contains the content of the parent block "outer".

Is it possible to render the content of the inner blocks a, b and c inside the child template?

Scenario: The contents of blocks a, b and c is really complex and I want to avoid copying and pasting the whole contents from the parent.

Collapse answered 14/7, 2015 at 17:20 Comment(2)
Couldn't you just set each child block to be a child template, then just do one template with the order ABC and another with CBARancor
I am not sure I understand your suggestion. :) Anyway, I cannot modify the parent template, as it is part of ShopWare.Collapse
B
4

Though this is a old post this might be important for the future.
My best attempt at solving this issue is the following:

{extends file="parent:parent.tpl"}

{block a}
    {capture a}
        {$smarty.block.parent}
    {/capture}
{/block}

{block c}
    {capture c}
        {$smarty.block.parent}
    {/capture}
{/block}

{block b}
    {$smarty.capture.c}
    {$smarty.block.parent}
    {$smarty.capture.a}
{/block}
Brat answered 13/2, 2017 at 12:3 Comment(1)
This answer should be marked as the correct anwser!Drawtube
G
1

I stumpled uppon this very old question. I have a similar problem, change order of {block}´s without accessing the parent theme and without copying the whole {block}.

I found a little workaround for this.

Example: your parent theme´s file: frontend/index/index.tpl with blocks

{block name="mainblock"}
   {block name="header"}headercontent{/block}
   {block name="nav"}navcontent{/block}
   {block name="footer"}footercontent{/block}
{/block}

Extending the file

{extends file="parent:frontend/index/index.tpl"}

extending the block

{block name="header"}
    {* define a function with a similar name as the block *}
    {function name="block__header"}
        {* define a new block, so you can extend your own block *}
        {block name="my_header"}
            {*call parent block to inherit the content *}
            {$smarty.block.parent}
        {/block}
    {/function}
{/block}

moving the block

{block name="footer" prepend}
    {call name="block__header"}
{/block}

Note: if you extend the {block name="header"} later on, your extended content will be displayed on the original position, where the original block was. To extend it use {block name="my_header"}.

In Shopware you can use protected $injectBeforePlugins = false; in your Theme.php, than you can override plugins content.

It´s also possible to use capture instead of a function but i thought {call name="block__header"} is more readable then {$block__header}.

I defined the function as a snippet in my editor, so I don´t have to type all this stuff again.

Again: sorry for answering such an old question, but I thought it could be usefull for others.

Gunplay answered 28/2, 2018 at 12:33 Comment(0)
A
1

There is a solution that is still not optimal, but it is cleanest approach I found so far.

parent.tpl

{block name="outer"}
  {block name="a"} ... some long content ...{/block}
  {block name="b"} ... some long content ...{/block}
  {block name="c"} ... some long content ...{/block}
{/block}

Let's assume we want to move a to the bottom of the outer block, our child template should look like this:

child.tpl

{extends file="parent:parent.tpl"}

{block name="a"}
  {capture name="a"}
    {$smarty.block.parent}
  {/capture}
{/block}

{block name="c" append}
  {$smarty.capture.a}
{/block}

In this example block a get's overwritten and because the content is captured there will be no actual output in block a, a is "removed". Finaly we append the captured content of a to the c-block. The content of a now is at the bottom of the outer block.

Antiserum answered 23/1, 2019 at 8:29 Comment(0)
B
0

Have you tried to add the parent block name?

{extends file="parent:parent.tpl"}
{block outer}
    {block c} {$smarty.block.parent.c} {/block}
    {block b} {$smarty.block.parent.b} {/block}
    {block a} {$smarty.block.parent.a} {/block}
{/block}
Bousquet answered 15/7, 2015 at 12:0 Comment(1)
That gives me syntax error: $smarty.block is invalid. Strange error message in this case.Collapse
S
0

You can define blocks as seperate tpl files and include them in any order. Or you can pass blocks as an array from php script and use in any order.

In template inheritance block's just define places that you can change.

Selfcontrol answered 16/7, 2015 at 8:12 Comment(1)
Thanks for your suggestion, but as I mentioned in my previous comment, I cannot modify the parent template, because it is part of ShopWare. Maybe it is just not possible with this template structure.Collapse
Z
0

You could try to capture the output of the blocks and then change the order of them. Give it a try, here is the smarty documentation for capture.

Zamudio answered 23/10, 2015 at 0:51 Comment(0)
D
0

Quite old topic but I´ve the exactly same issue and no idea how to solve it yet. I´ve also tried to use smarty.block.parent with the same result.

Have you already found a pattern which solves that issue?

Dosser answered 13/7, 2016 at 13:20 Comment(1)
Sadly no. I ended up copying those big template parts.Collapse

© 2022 - 2024 — McMap. All rights reserved.