如何在smarty模板中插入换行符?

时间:2013-01-11 06:15:43

标签: php smarty

我有一个聪明的模板代码,如下所示,我想逐行将智能模板数据写入文本文件。为此,我要在每条记录的末尾插入换行符(即在值$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} 

2 个答案:

答案 0 :(得分:4)

应该有一个带有智能标签的新行字符(“\ n”),它位于值$user_test_result.test_result_data.total_obtained_marks之后,就像这个{“\ n”}一样。你会得到理想的结果。

答案 1 :(得分:2)

除非我错了,否则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}