我将多选框的结果传递给页面,以便可以在屏幕上显示选择。 由于它是多选的,因此结果可以是标量或数组引用。 有没有办法找出这个问题? 我在网上找不到任何东西,但我想可能有一个.array或.array_ref标记可用于验证。 我正在使用模板工具包,Perl和舞者。
所以这就是我得到的标量:
<% IF multitext %> Text: <% multitext %> <% END %>我想要的是类似...
<% IF multitext %> <% IF multitext.array_ref %> <!-- whatever works! --> <% FOREACH text IN multitext %> Text: <% text %> <% END %> <% ELSE %> Text: <% multitext %> <% END %> <% END %>I'm passing the results of a multi-select box to a page so that the selections can be shown on screen. As it's multi-select, the result can either be a scalar or an array reference. Is there a way of finding this out? I can't find anything online, but I thought there might be a .array or .array_ref token that could be used for validation. I'm using Template Toolkit, Perl and Dancer.
So here is what I've got for a scalar:
<% IF multitext %> Text: <% multitext %> <% END %>What I want is something like...
<% IF multitext %> <% IF multitext.array_ref %> <!-- whatever works! --> <% FOREACH text IN multitext %> Text: <% text %> <% END %> <% ELSE %> Text: <% multitext %> <% END %> <% END %>最满意答案
如果<%- multitext.0 -%>返回一个非零值,则它是一个arrayref。
如果<%- multitext.keys.size -%>返回非零值,则为hashref。
我通常处理它的方式是强制它成为一个数组,如果它是一个标量,例如:
<%- SET items = multitext.0 ? multitext : [ multitext ]; FOREACH item IN items; ... END; -%>If <%- multitext.0 -%> returns a non-zero value, it's an arrayref.
If <%- multitext.keys.size -%> returns a non-zero value, it's a hashref.
The way I usually handle it is to force it to be an array if it's a scalar, eg:
<%- SET items = multitext.0 ? multitext : [ multitext ]; FOREACH item IN items; ... END; -%>更多推荐
发布评论