3

我有一个 smarty 模板代码如下,我想将 smarty 模板数据逐行写入文本文件。为此,我必须在每条记录的末尾插入一个换行符(即在 value 之后$user_test_result.test_result_data.total_obtained_marks)。这应该如何实现?请帮我解决这个问题。提前致谢。

{if $all_user_test_result}{assign var='sr_no' value=1}{foreach from=$all_user_test_result item=user_test_result key=key}{if $key!='sort_user_name' && $key!='sort_total_obtained_marks' && $key!='sort_no_correct_answers' && $key!='sort_no_wrong_answers' && $key!='sort_no_skipped_questions'}{$sr_no};{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_user_data.user_name};{else}{$user_test_result.test_user_data.user_name};{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_user_data.test_user_time_used};{else}--{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_correct_answers};{else}-{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_wrong_answers};{else}-{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_skipped_questions};{else}-{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.total_obtained_marks};{else}-{/if}{/if}{assign var='sr_no' value=$sr_no+1}{/foreach}{else}Result not found{/if} 
4

2 回答 2

6

应该有一个换行符(“\n”),它周围有聪明的标签,放在$user_test_result.test_result_data.total_obtained_marks像 {“\n”} 这样的值之后。你会得到想要的结果。

于 2013-01-11T06:49:36.707 回答
2

除非我弄错了,否则 Smarty 会将模板中的所有空格直接输出到文件中。因此,与其将所有 smarty 标签放在一行上,不如在适当的位置插入换行符。

{if $all_user_test_result}{assign var='sr_no' value=1}{foreach from=$all_user_test_result item=user_test_result key=key}{if $key!='sort_user_name' && $key!='sort_total_obtained_marks' && $key!='sort_no_correct_answers' && $key!='sort_no_wrong_answers' && $key!='sort_no_skipped_questions'}{$sr_no};{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_user_data.user_name};{else}{$user_test_result.test_user_data.user_name};{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_user_data.test_user_time_used};{else}--{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_correct_answers};{else}-{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_wrong_answers};{else}-{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_skipped_questions};{else}-{/if}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.total_obtained_marks};{else}-{/if}
{/if}{assign var='sr_no' value=$sr_no+1}{/foreach}{else}Result not found{/if}

为了便于阅读,您还可以在模板中包含不输出的换行符,方法是将它们放入{* comments *}

{if $all_user_test_result}{*
    *}{assign var='sr_no' value=1}{*
    *}{foreach from=$all_user_test_result item=user_test_result key=key}{*
        *}{if $key!='sort_user_name' && $key!='sort_total_obtained_marks' && $key!='sort_no_correct_answers' && $key!='sort_no_wrong_answers' && $key!='sort_no_skipped_questions'}{*
            *}{$sr_no};{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_user_data.user_name};{else}{$user_test_result.test_user_data.user_name};{/if}{*
            *}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_user_data.test_user_time_used};{else}--{/if}{*
            *}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_correct_answers};{else}-{/if}{*
            *}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_wrong_answers};{else}-{/if}{*
            *}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.no_skipped_questions};{else}-{/if}{*
            *}{if $user_test_result.test_user_data.user_status=='Attempted'}{$user_test_result.test_result_data.total_obtained_marks};{else}-{/if}{*
        *}{* Add newline at end of record *}
        {/if}{*
        *}{assign var='sr_no' value=$sr_no+1}{*
    *}{/foreach}{*
*}{else}Result not found{*
*}{/if}
于 2013-01-13T20:45:31.370 回答