PHPTAL 动态表生成
我发现自己经常为表格数据创建各种表,并且希望创建一个宏,可以根据调用模板(而不是 PHP 代码)中定义的数据结构动态创建表。这是一个简单的示例:
<!-- Define the macro -->
<tal:block metal:define-macro="table">
<table>
<tr tal:repeat="row data">
<td tal:repeat="col row" tal:content="col" />
</tr>
</table>
</tal:block>
<!-- Use the macro -->
<tal:block tal:define="data ???" metal:use-macro="table" />
我正在寻找的是如何从 PHPTAL 本身定义 data
(数组结构)。我无法在 PHP 中将其定义为模板变量(例如 $tpl->data = array(...)
)的原因是因为数据的顺序和布局属于模板。因此,举例来说,如果我想翻转表格的 X 轴和 Y 轴,我只需修改模板,而不是 PHP 代码。
编辑:
举个例子,假设我有任意模板变量foo
、bar
和baz
。我可以在模板中使用这些变量,如下所示:
<span tal:content="foo" /><br />
<span tal:content="bar" /><br />
<span tal:content="baz" />
如何将这些变量构建为行和列的二维数据结构,然后将其输入到表生成宏中?像这样的东西(注意:这实际上不起作用):
<tal:block tal:define="data [foo, bar; baz]" metal:use-macro="table" />
宏所需的输出将是:
<table>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td>baz</td>
</tr>
</table>
稍后,如果我想交换 foo 和 bar 的位置/code>,我只需要修改模板并将data
的定义更改为data [bar, foo;巴兹]
。
I find myself creating various tables for tabular data quite a bit, and would like to create a macro that can dynamically create tables based on a data structure defined in the calling template (not in the PHP code). Here's a simplistic example:
<!-- Define the macro -->
<tal:block metal:define-macro="table">
<table>
<tr tal:repeat="row data">
<td tal:repeat="col row" tal:content="col" />
</tr>
</table>
</tal:block>
<!-- Use the macro -->
<tal:block tal:define="data ???" metal:use-macro="table" />
What I'm looking for is how to define data
(an array structure) from within PHPTAL itself. The reason I can't define this as a template variable in PHP (ex. $tpl->data = array(...)
) is because the order and layout of the data belongs in the template. So, for example, if I wanted to flip the X and Y axes of the table, I should only have to modify the template, not the PHP code.
Edit:
To give an example, say I have arbitrary template variables foo
, bar
, and baz
. I can use these in the templates like so:
<span tal:content="foo" /><br />
<span tal:content="bar" /><br />
<span tal:content="baz" />
How can I construct these variables into a two-dimensional data structure of rows and columns which I can then feed into a table-generating macro? Something like this (note: this doesn't actually work):
<tal:block tal:define="data [foo, bar; baz]" metal:use-macro="table" />
Where the desired output from the macro would be:
<table>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td>baz</td>
</tr>
</table>
And later on, if I wanted to swap the positions of foo
and bar
, I'd only need to modify the template and change the definition of data
to data [bar, foo; baz]
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该使用辅助方法,例如
php:transpose_table(input_data)
或将其包装在 TALES 函数中:PHPTAL 本身的转置或排序会不必要地复杂(PHPTAL 不是 XSLT :)
编辑答案:)
如果您想将多个变量组合成数组,那么
如果您想拥有一定数量的列,请使用:
array_chunk()
函数可能会很有用。如果您喜欢自定义语法,请编写
phptal_tales_...
函数,将您的[...]
语法转换为 PHP 代码。You should probably use helper methods, e.g. either
php:transpose_table(input_data)
or wrap it in a TALES function:Transposition or sorting in PHPTAL itself would be unnecessarily complicated (PHPTAL is not XSLT :)
Answer to edit :)
If you want to combine multiple variables into array, then use:
array_chunk()
function may be useful if you want to have certain number of columns.and if you like custom syntax, then write
phptal_tales_…
function that translates your[…]
syntax to PHP code.对于通用表生成:PHPTAL:重复列标题和值
For a Generic Table Generation: PHPTAL: Repeat Column headers and values