symfony项目中标题中的动态数据

时间:2021-03-25 07:27:26

I have a Symfony 1.4 project. As you know the Template layout is defined independently, in the apps' templates' folder and then it is universally applied to all other templates. My layout is very simple, something like this:

我有一个Symfony 1.4项目。如您所知,模板布局是独立定义的,在应用程序的模板文件夹中,然后它普遍适用于所有其他模板。我的布局非常简单,如下所示:

<div id = "header">
</div>
<div id = "content">
<?php echo $sf_content ; ?>
</div>
<div id = "footer">
</div>

$sf_content, as most symfonians would know, essentially spits out the template for whatever web page is being viewed at the moment. If I needed some specific data for my header, such as logout, logo etc, I would simply include it within my header. THis works great because it is static in nature. The challenge I am facing is how I can include data that is dynamic in nature and specific to a page within the header tag because the UI demands that I include it there.

正如大多数symfonians所知,$ sf_content实际上是为当前正在查看的任何网页吐出模板。如果我需要一些特定的数据用于我的标题,例如注销,徽标等,我只需将其包含在我的标题中。这很有效,因为它本质上是静态的。我面临的挑战是如何在头标记中包含动态本质且特定于页面的数据,因为UI要求我将其包含在那里。

For instance, one of my webpages requires user specific data to be loaded in a dropdown/select menu. This is dynamic and could range from 0 to 100 and is specific to each user. To create this dropdown menu is not an issue, and I already have that part done. The challenge is, how do I load it in the header, given that my data becomes part of $sf_content and that is spit out in my content div.

例如,我的一个网页要求在下拉/选择菜单中加载用户特定数据。这是动态的,范围从0到100,并且特定于每个用户。创建此下拉菜单不是问题,我已完成该部分。挑战是,如果我的数据成为$ sf_content的一部分并且在我的内容div中吐出,我如何将其加载到标题中。

Is there a way for me to move a specific part of my $sf_content into the header div ?

有没有办法让我将$ sf_content的特定部分移动到标题div中?

2 个解决方案

#1


1  

In your actions.php:

在你的actions.php中:

$this->getResponse()->setSlot('someData', 'and its value');

In layout.php:

在layout.php中:

<div id="header">
<?php echo get_slot('someData'); ?>
</div>
<div id="content">
<?php echo $sf_content ; ?>
</div>
<div id="footer">
</div>

#2


0  

Slots work for this. They can either be set in the action as in the first answer above or you can define them in the templates themselves. This is what I've done where I have dynamic data to define for the layout.

插槽适用于此。它们可以像上面的第一个答案一样在动作中设置,也可以在模板本身中定义它们。这就是我在为动态数据定义布局时所做的工作。

In your example:

在你的例子中:

<div id="header">
  <?php include_slot('some slot name')?>
</div>
<div id="content">
<?php echo $sf_content() ?>
</div>
<div id="footer">
</div>

In the templates you would define the following:

在模板中,您将定义以下内容:

<?php slot('some slot name')?>
  //your code goes here
<?php end_slot() ?>

When the layout is then rendered Symfony will place the code between the slot() and end_slot() into the point at which you defined by using include_slot().

然后,当渲染布局时,Symfony会将slot()和end_slot()之间的代码放入使用include_slot()定义的点。

For ease I created a global partial that is included in all templates that defines the various common slots used through out the application. There is more info on slots and their usage here

为了方便起见,我创建了一个全局部分,它包含在所有模板中,用于定义应用程序中使用的各种常见插槽。有关插槽及其使用的更多信息

#1


1  

In your actions.php:

在你的actions.php中:

$this->getResponse()->setSlot('someData', 'and its value');

In layout.php:

在layout.php中:

<div id="header">
<?php echo get_slot('someData'); ?>
</div>
<div id="content">
<?php echo $sf_content ; ?>
</div>
<div id="footer">
</div>

#2


0  

Slots work for this. They can either be set in the action as in the first answer above or you can define them in the templates themselves. This is what I've done where I have dynamic data to define for the layout.

插槽适用于此。它们可以像上面的第一个答案一样在动作中设置,也可以在模板本身中定义它们。这就是我在为动态数据定义布局时所做的工作。

In your example:

在你的例子中:

<div id="header">
  <?php include_slot('some slot name')?>
</div>
<div id="content">
<?php echo $sf_content() ?>
</div>
<div id="footer">
</div>

In the templates you would define the following:

在模板中,您将定义以下内容:

<?php slot('some slot name')?>
  //your code goes here
<?php end_slot() ?>

When the layout is then rendered Symfony will place the code between the slot() and end_slot() into the point at which you defined by using include_slot().

然后,当渲染布局时,Symfony会将slot()和end_slot()之间的代码放入使用include_slot()定义的点。

For ease I created a global partial that is included in all templates that defines the various common slots used through out the application. There is more info on slots and their usage here

为了方便起见,我创建了一个全局部分,它包含在所有模板中,用于定义应用程序中使用的各种常见插槽。有关插槽及其使用的更多信息