lundi 28 septembre 2015

Laravel way to write re-usable view snippets

I have many models in my application. Each model has some specific markup that is used in countless locations. For instance, my User model has a profile_link attribute which is defined as follows:

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>';
    }

}

There are many such accessors for many models, that return similar code snippets. The problem with this approach is that it makes me write HTML markup within the model. Is there any standard way to approach this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire