Should I use a large if statement inside a view or create a different view entirely?

I’m using the MVC architecture Laravel 4.2 to create a relatively complex web application. This application consists of a website which is very heavily based on whether an event has occurred or will occur. i.e. if an event is in the future, I will be displaying a countdown, whereas if it is in the past, I will be displaying information about it.

Overall, about 50% of the entire “page” markup may be unique depending on whether it has occurred or will occur.

My question is, should I be using a large if statement inside the view to display what I want based on its future/past occurrence (Yes! Otherwise you will be violating DRY with duplicated markup) or should I create a view which contains the markup for future events and another view which contains the markup for past events and get my controller to route to the applicable view (Yes! Views should be dumb and display & routing logic should be left to the controller).

What would be the correct choice in this situation, or more appropriately what questions could I ask myself to help me determine the best approach to take to this in the future?

2

While views shouldn’t contain business logic, they still can contain basic conditional logic, loops, etc.

Since your website is “very heavily based on whether an event has occurred or will occur”, this is definitively a case where you’ll have two views, not one.

What questions could I ask myself to help me determine the best approach to take to this in the future?

What you should ask yourself is whether each view corresponds to a page, as it would be defined by a person who don’t know anything about the internal workings of a website.

  • For example, a list of products and the details of a product are logically two pages, and so you’ll find two views.

    Internally, it may correspond to a single AJAX request which retrieves all info about all the products. Or in order to display the details of a product, you may be doing a dozen AJAX requests to different APIs. What matters is that non-technical persons will see it as a page which shows a list of products and a page which shows the details.

  • On the other hand, a list of products ordered by a price and a list of products ordered by a name feel more like a single page, and will probably correspond to a single view.

Since the notion of pages is itself not very precise, you may also be guided by how much change is there between two views:

  • If two views are sharing no more than a header and a footer, there are chances they should remain separate.

  • If two views are practically a copy-paste with only a tiny part being different, they should be merged in a single view with an if/else statement.

  • In between, the choice remains rather subjective.


Note that having two different views doesn’t necessarily lead to code duplication (or rather markup duplication in this case). Most view/template engines have:

  • Master views, which enable for a given template to inherit from a master template which contains the surrounding markup,

  • Partial views, which enable for a given template to embed markup from another template, markup which can this way be shared between several views.

I’ve never used Laravel, so I’m not 100% sure this will work for you, but one method I’ve used in the past is to include either a list or an optional value in my viewmodel that contains the name of another model to render and the data to put there. The view then iterates over the list/checks for presence of the value and includes the appropriate view definition.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật