dsfsdfsdfsdfsdf45454 2009-12-21 19:16
浏览 28
已采纳

从函数或方法返回html代码是一个坏主意吗?

I have this idea:

when someone calls mysite.com/layer1/layer2/something, then I want to start the action called something. That action is simply a file in an actions directory which contains a function. This function takes some context parameters and is supposed to return HTML output. This output, which goes into a variable, will then be brought into an main template, and finally the little framework will throw out the whole html output at once.

Now I don't know if it's good or bad design to return big amounts of html code from a function or method?

  • 写回答

5条回答 默认 最新

  • dousu8456 2009-12-21 19:30
    关注

    There's nothing wrong with returning HTML from a function provided that function lives in the presentation layer and you maintain good seperation of business/controller/presentation logic.

    However, in this particular case, I'd recommend having your "action" files just dump their output via echo, and capturing it with output buffering wherever you're doing your original function call. This way, your action files don't have to worry about concatting a bunch of data onto a buffer variable and making sure it gets returned. Also, because these files are (presumably) largely HTML oriented, you'll be able to use the much nicer and more legiable syntax:

    <div id="Outer">
      <?= $content ?>
      <p class="Inner">
        <?= $more_content ?>
      </p>
    </div>
    

    As opposed to:

    $buffer .= '<div id="Outer">' . $content .
               '<p class="Inner">' . $more_content .
               '</p></div>';
    

    At the very least, the first way allows your IDE/editor to perform proper syntax highlighting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作