Does Django have HTML helpers? - html

Does Django have any template tags to generate common HTML markup? For example, I know that I can get a url using
{% url mapper.views.foo %}
But that only gives me the URL and not the HTML code to create the link. Does Django have anything similar to Rails' link_to helper? I found django-helpers but since this is a common thing I thought Django would have something built-in.

No it doesn't.
James Bennett answered a similar question a while back, regarding Rails' built-in JavaScript helpers.
It's really unlikely that Django will ever have 'helper' functionality built-in. The reason, if I understand correctly, has to do with Django's core philosophy of keeping things loosely coupled. Having that kind of helper functionality built-in leads to coupling Django with a specific JavaScript library or (in your case) html document type.
EG. What happens if/when HTML 5 is finally implemented and Django is generating HTML 4 or XHTML markup?
Having said that, Django's template framework is really flexible, and it wouldn't be terribly difficult to write your own tags/filters that did what you wanted. I'm mostly a designer myself, and I've been able to put together a couple custom tags that worked like a charm.

The purpose of helpers is not, as others here imply, to help developers who don't know how to write HTML. The purpose is to encapsulate common functionality -- so you don't need to write the same thing a thousand times -- and to provide a single place to edit common HTML used throughout your app.
It's the same reason templates and SSI are useful -- not because people don't know how to write the HTML in their headers and footers, but sometimes you want to write it just once.
EG. What happens if/when HTML 5 is
finally implemented and Django is
generating HTML 4 or XHTML markup?
Same thing that happens when HTML 5 is implemented and all your templates are written in repetitive HTML, except a lot easier.
The other posts have already answered the question, linking to the docs on custom template tags; you can use tags and filters to build your own, but no, there aren't any built in.

it doesnt look like they're built in but here's a couple snippets. it looks like it'd be pretty easy to create these helpers:
http://www.djangosnippets.org/snippets/441/

Here is a list of all template tags and filters built into Django. Django core doesn't have as much HTML helpers as Rails, because Django contributors assumed that web developer knows HTML very well. As stated by saturdaypalace, it's very unlikely for AJAX helpers to be added to Django, because it would lead to coupling Django with a specific JavaScript library.
It's very easy to write your own template tags in Django (often you need just to define one function, similiar to Rails). You could reimplement most of Rails helpers in Django during a day or two.

I bet if there would be any consent of what is common html, there would be helpers module too, just for completeness (or because others have it). ;)
Other than that, Django template system is made mostly for HTML people, who already know how to write p, img and a tags and do not need any helpers for that. On the other side there are Python developers, who write code and do not care if the variable they put in context is enclosed by div or by span (perfect example of separation of concerns paradigm). If you need to have these two worlds to be joined, you have do to it by yourself (or look for other's code).

This won't answer directly to the question, but why not using foo in template then?

Related

Understanding the Basics behind Handlebars, Express, and Node.js

I've been struggling to understand Handlebars ever since I was introduced to it in class. I've researched different resources and videos (e.g., YouTube, StackOverflow, etc.) to try and learn more about it, but I still feel like I'm not getting it.
Could somebody please either explain to me what Handlebars is in their own terms or send me resources they found helpful when learning it?
Thanks!
handlebars.js is a templating engine which allows dynamic data to be mixed in with your HTML code. Templating engines were created due to complex projects requiring a lot of dynamic HTML manipulation. Previously, software developers created new chunks of HTML code and dynamically inserted them into the DOM using Javascript. This eventually became unwieldy and difficult to maintain. Also, it lead to repetition of code. To solve this issue, templating engines allowed one to create predefined templates to be used in multiple locations without repeating the code. Templates are like “macros”; wherever they are used, the code in them is inserted at that place. They also help to keep your html away from your javascript files, thereby increasing the readability and re-usability of your code. For a more comprehensive explanation see this blog post
The best resource to learn handlebars.js is their documentation
There is a course on Udemy that uses handlebarsjs and Node and Express and goes from pretty basic I think. This is it.

Django - same html, different views

I want to have a block oh html across all my templates (like a sidebar for basic form submissions), which is easily implemented on the html files by using blocks.
However, my doubt is not about the repetitions across templates, but across views. Since the functionality will be the same across all templates, it would be really boring (and bad programming) to define the request handling (that would come from that side bar's submissions) for every view I have! How should I handle this? Should (and can) I make a view dedicated to handling that "all-around" part of the template?
Any advices are welcome,
Thanks in advance
Daniel is refering to an Inclusion Tag
Basically, a custom template tag is used for scenarios like yours (... code reusability amongs many other advantages)
Also this post might be helpful: Django Custom Inclusion Tags
Another approach could be using template inheritance - create a base template, which defines the layout, and override the blocks of code that would change for specific views.
Here is an example of template inheritance: https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance
Use a custom template tag - probably an inclusion tag.

HTML Layout/Templating

I am building a website where i'm looking to create html templates which contain placeholders where i'll be able to inject pages/content, menus, etc.... I really like AngularJS which has good support for this but am thinking that it might be a bit of an overkill for what i'm building. There is a chance some parts of the application will eventually be ripped out and run standalone and so dont want to tie the html/javascript to a library which requires so much framework specific syntax. Are there any other, simpler libraries, that will allow me to do this? thanks in advance
/Eric
KnockoutJS is pretty lightweight and also includes a templating feature.
I wouldn't just dismiss angularjs because it seems like a heavy framework. There are very simple ways to use it without using any of the "advanced" features like routing, creating services, creating directives, etc. You can simply have a controller and use the built in directives to do a lot of powerful things on a single page. Angular is also pretty small and is easily bootstrapped with the ng-app tag anywhere in your DOM.

Is there a convenient way to prototype future structure of View layer of MVC framework (Rails or like) on pure HTML?

My "Rails friends" have a situation when their UI designer is beginning his work on prototyping UI screens on his own - their Rails programmer is busy and is going to join this work later, probably much later.
I know, it is easy to ask UI designer the learn Rails rendering mechanism, so he could begin prototyping on Rails, but I am interested,
is it possible to setup views structure using partials (reusable pieces of HTML views) using just pure HTML, without any ruby code inside?
The only approach I see is to use SSI. Are there any other options?
What is needed is simply a possibility for one html-file could have a string like
<whatever include other.html ...>
which would include the contents of other.html inside its body.
This way, a future project can be divided logically on changeable/nonchangeable parts without any Ruby/Php code for partials.
UPDATE 1
The quoted wiki page about SSI has Client Side Includes section that suggests using object tag which seems to work
<object type="text/html" data="test.html"></object>
One suggestion is to use Sinatra, which is a simple and quick web framework for static sites.
It's easy to start using Sinatra even for a beginner. However, even if the UI man doesn't know and don't want to study any coding, he can still utilize it by planning the partial names and ask the developer to setup it for him. All he need to do later is to fill each blank templates and add JS CSS image assets.
Benefit of Sinatra:
Friendly to static sites
Simple to use
Lots of templating language to use, same as Rails
The partials/templates can be ported to Rails later with zero efforts!!!

HTML - writing vs generating code

I've had the chance to work with very different types of frameworks for web development. Somehow I can see that every framework tries to avoid me writing pure HTML code.
For example Spring has it's own tags, Struts comes with it's own tags also Zend and Codeigniter (PHP) have form helpers.
Does this mean I shouldn't just write HTML code myself or that it's not recommended? SHould I be using them? because they don't seem that intuitive, easy to manage and time saving as they intend.
You need to write your own HTML (and CSS) to define the overall structure and layout of your website design. Don't confuse this with time-saving features that eliminate the need to write repetitive HTML, such as form inputs and navigation menu items.
For example, under ASP.NET MVC you still need to get the bulk of your HTML into a MasterViewPage or Razor Layout, but when it comes to writing all of the form inputs you just need to put <%= Html.TextBoxFor( m => m.FirstName ) and it handles the rest, which saves you the trouble of writing boilerplate HTML for the <input /> element. It also helps avoid bugs (if you forget a name="" or `id=""`` attribute, for example, but in some frameworks it's essential to get reliable round-trip form data working.
I suggest you to manage your code by yourself if you don't have VERY good tools for generate it (for example Razor in ASP.NET MVC) which can do your code cleaner and more intuitive. In your case if You use Zend i suggest you to write your html in normal sites and only use generator for forms with Zenf_form. Becouse it is great.
Generated code services I have used generate ugly code that is hard to sift through at a later date.
writing the code and HTML yourself makes for a better structure and will not effect the way its run if that is what you are worried about and these languages are set to a standard.
Recommendation is to write the code where you can and generate code in areas you dont (or learn how to code it before you attempt it in test sheets).
I think it is a case by case basis.
These frameworks help you get rid of boilerplate code and write cleaner applications. You might also avoid some security problems by using them and you respect MVC.
There might be a performance penalty though so if you write a small application or you have certain requirements in speed you might be better without (some of) them.