简体   繁体   中英

PHP - Smarty templates - How to create context independant templates

I've started to learn Smarty templating engine hoping it would allow me to do what i have a hard time to do with PHP built-in templates, but i'm encountering a similar problem.

Let's assume that i want to create reusable pieces of HTML code like, for example, an accordion menu. My template would look like :

Accordion.tpl :

<div class="Accordion">

    {foreach from=$entries item=entry}

    <div class="AccordionEntry">
        <div class="AccordionTab">
            {$entry.tab}
        </div>
        <div class="AccordionContent">
            {$entry.content}
        </div>
    </div>

    {/foreach}

</div>

This template will retrieve the variable "entries" assigned in the controller part :

$smarty = new Smarty();

$smarty->assign('entries', [
    ['tab' => 'tab_00', 'content' => 'content_00'],
    ['tab' => 'tab_01', 'content' => 'content_01'],
    ['tab' => 'tab_02', 'content' => 'content_02']
]);

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

This will work fine. However, what if i want to reuse this accordion template in multiple places ? The data could be assigned this way :

$smarty->assign('leftMenuEntries', [
    ['tab' => 'tab_00', 'content' => 'content_00'],
    ['tab' => 'tab_01', 'content' => 'content_01'],
    ['tab' => 'tab_02', 'content' => 'content_02']
]);

$smarty->assign('rightMenuEntries', [
    ['tab' => 'tab_00', 'content' => 'content_00'],
    ['tab' => 'tab_01', 'content' => 'content_01'],
    ['tab' => 'tab_02', 'content' => 'content_02']
]);

Here is my problem : the template "Accordion.tpl" will always retrieve data from the variable "entries", but here i'm using "leftMenuEntries" and "rightMenuEntries", so oviously it will fail. Since the two accordions won't necessarily have the same entries, i'm forced to assign these entries to two different variables.

What can i do to make this work together please ?

Thanks for your help :)

When you {include} one template from another, you can "pass in" variables for use in that template (a bit like function parameters). So if you have an overall template for the page layout, rendered as eg $smarty->display('Homepage.tpl') , you can have multiple accordions within it like so:

{* pull entries out of $entries, as nothing else specified *}
{include file=Accordion.tpl}

{* pull entries out of $leftMenuEntries, which will be named as $entries inside the included file *}
{include file=Accordion.tpl entries=$leftMenuEntries}

{* the same, but this time we "pass in" $rightMenuEntries *}
{include file=Accordion.tpl entries=$rightMenuEntries}

Not sure if I understood correctly, but if you want to use the data with the same templating style multiple times, without assigning new variable and displaying to new template, you can possibly create Accordion.tpl with only this div, and include it in every template where you want the data.

{include file='Accordion.tpl'}

In the case above it will retrieve the variable with the same name, if it's assigned to the main template, no to Accordion.tpl

However, if the problem's core is in the overwriting arrays, I found that in array_merge topic in PHP.net:

<?php
// you have two arrays:

array1 = array (['0'] =>"blahblupp",
                        ['1'] => "bluppblah" );

array2 = array (['0'] =>"tirili",
                        ['1'] => "tralala" );

// and want following as a result:

result = array (['0'] =>"blahblupp",
                      ['1'] => "bluppblah",
                      ['2'] =>"tirili",
                      ['3'] => "tralala" );

// following function does the addition:

function array_add($array1, $array2)
{
    $result = $array1;
    $h = sizeof($array1);
    for ($i = 0; $i < sizeof($array2); $i++)
    {
        $result[$h] = $array2[$i];
        $h++;
    }

    return $result;
}
?> 

If it's still not your case, I will try to find another solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM