简体   繁体   English

PHP-Smarty模板-如何创建上下文独立模板

[英]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. 我已经开始学习Smarty模板引擎,希望它可以让我做我很难用PHP内置模板做的事情,但是我遇到了类似的问题。

Let's assume that i want to create reusable pieces of HTML code like, for example, an accordion menu. 假设我想创建可重用的HTML代码,例如手风琴菜单。 My template would look like : 我的模板如下所示:

Accordion.tpl : 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 : 该模板将检索在控制器部分中分配的变量“ entries”:

$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. 这是我的问题:模板“ Accordion.tpl”将始终从变量“条目”中检索数据,但是这里我使用的是“ leftMenuEntries”和“ rightMenuEntries”,因此显然它将失败。 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). 当您从另一个模板中{include}一个模板时,可以“传入”在该模板中使用的变量(有点像函数参数)。 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: 因此,如果您有一个页面布局的整体模板,例如$smarty->display('Homepage.tpl')呈现,则其中可以包含多个手风琴,如下所示:

{* 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. 不知道我是否理解正确,但是如果您想多次使用具有相同模板样式的数据,而又不分配新变量并将其显示到新模板,则可以仅使用此div创建Accordion.tpl,并将其包含在每个您想要数据的模板。

{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 在上述情况下,它将检索具有相同名称的变量(如果已将其分配给主模板,则将其分配给Accordion.tpl)

However, if the problem's core is in the overwriting arrays, I found that in array_merge topic in PHP.net: 但是,如果问题的核心在于覆盖数组,我在PHP.net的array_merge主题中发现了这一点:

<?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 如果仍然不是您的情况,我将尝试寻找其他解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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