Sense user-agent to determine HTML – Keep HTML and Logic separate?

I am of-the-understanding that you should try to keep HTML out of your logic, and keep logic out of your HTML. E.G.: You don’t write HTML in (C# strings or Javascript Services) and pass them into the View or DOM. Likewise, you don’t write classic ASP anymore (and I personally limit/nill Razor). HTML is for HTML, Services are for Logic. My question is based on this best-practice. Of course there are small rare one-off’s. Counter E.G.: Razor fans use the HTML Extensions class. But Razor is its own beast.

With that “best-practice” in mind, is it bad to sense the User-Agent in the headers and send keys or HTML to the View for that specific media? E.G.: I sense an iPad user-agent, so I either send iPad specific <style...> declaration, or a flag on the ViewBag so my View decides which <style...> declarations to use.

I ask because it doesn’t seem like enough to use the css media declarations based on media width. A desktop can (and is frequently) be set to the same 768 as an iPad.

But on a Desktop, I want my styles to be one way; and on an iPad, I want them to be another.

So is it breaking best-practices to sense the User-Agent server-side for making design choices client-side?

Note: This is not a question of “How do you alter CSS for iPad.” As far as I’m concerned, your only options are css @media declaration, or sensing server-side. I just want to know the impact on best-practices.

Your absolutely correct with your thinking, yes you absolutely need to keep them separate.

However, it doesn’t need to be as hard as you might think it is, in order to get your layout working correctly across multiple devices.

Realistically, you have 3 approaches that are considered modern practice in this arena.

1) Create a satellite site for each platform you wish to support, then in a single simple index controller, sniff the user agent and redirect to the appropriate satellite site.

2) Use media queries. Media Query support is now (and has been for some time) good enough and well supported enough to use in full production sites.

3) Use a UI toolkit such as KnockoutJS or Angular and modify class names and attributes on your layouts in real time, responding to user agent changes.

In more detail

If you decide to go with option 1, then the first thing you need to be aware of is the extra work.

Essentially, you’ll be maintaining two sites.

Your back-end however should likely still stay the same. If you use a classic nTier design with a SOLID Composition Root, then your business layer, data layer etc can all be shared, so maintaining separate sub sites does become much easier.

Option 2 on the other hand, means that you still only maintain one site, but the disadvantage you have here is one of complexity.

To use media queries successfully, you need to design your site for one platform, usually desktop, then you need to decide what rules you wish to override and for which screen widths.

While this is not strictly modifying your site based on user agent, you are still adapting your layout to a given screen width, which means that on narrower screens (such as a mobile device) you are still maintaining a readable output, while only having to change the minimum amount needed to maintain that readability.

This tends to be the route that many of the CMS’s take, especially wordpress designs, because it requires very little code, and everything can be changed directly in the CSS.

You are however limited to changing only what can be changed using CSS, and since there is still a fair bit of instability in the CSS3 spec, you may find that you still need to limit some of the changes you’d like to make.

This brings me to Option 3 (This is my preferred option) and the way I work for most of what I do.

Simply design your layout as a universal kind of layout, one which while not pretty, works generically on most devices.

Then, wire in your favorite UI toolkit. (For me this is KnockoutJS mixed with Twitter Bootstrap).

Use your UI toolkit to fill in the dynamic parts of your layout, based on a rest endpoint sniffing the UA from the request.

For example. in my CSS I might have:

.rule_for_desktop
{
  color: blah;
  width: blah;
}

.rule_for_iphone
{
  color: blah;
  width: blah;
}

.rule_for_android
{
  color: blah;
  width: blah;
}

and in my view model for knockout i might have:

var cssRule = ko.observable("rule_for_desktop");
// More code for the rest of the VM here

and in my HTML (This is based on knockout) I might have

<div id="myElement" data-bind="css: cssRule">... some content</div>

What then happens is my layout defaults to a desktop layout (or whatever I set as the default in my viewmodel.

If I then fire off an AJAX request, or perform some UA sniffing in the JS code, beacuse my ‘cssRule’ property is an observable the css rule used in my mark up instantly changes, and takes effect.

9

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