【问题标题】:Laravel way to write re-usable view snippetsLaravel 编写可重用视图片段的方法
【发布时间】:2015-09-28 18:21:08
【问题描述】:

我的应用程序中有许多模型。每个模型都有一些在无数地方使用的特定标记。例如,我的User 模型有一个profile_link 属性,其定义如下:

class User extends Model {

    /*
     * Returns the anchor tag to the users profile.
     */
    public function getProfileLinkAttribute() {
        // This particular markup is simple. In the actual code, the HTML
        // is more complex, containing user's profile image etc.
        return '<a href="' . url($this->id) . '">' . $this->name . '</a>';
    }

}

对于许多模型有许多这样的访问器,它们返回相似的代码 sn-ps。这种方法的问题在于它让我在模型中编写 HTML 标记。有没有标准的方法来解决这个问题?

【问题讨论】:

  • 您可以探索presenters,这样您就可以拥有BasePresenter。其他选项也是 html 宏。

标签: model-view-controller laravel-5


【解决方案1】:

你在 Laravel 中寻找的是 View Partials。这些是您包含在其他视图中的可重用视图。它们可以包含在控制器级别或视图中。

这里有一些很好的链接可以帮助您入门:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多