Helper methods like rails or taglibs/tagfiles like java

80 views
Skip to first unread message

Rodrigo Gomes

unread,
Jun 15, 2011, 2:01:53 PM6/15/11
to django...@googlegroups.com
Hi, 

I was looking for some 'html helpers' in django as we have on rails or java (with tagfiles or taglibs), but I saw that it doesn't exist, according to this stackoverflow response:

The guy suggested to write custom template tags, and pointed to this page https://docs.djangoproject.com/en/1.2/howto/custom-template-tags/

But I think that I'm missing something. Is it so hard to write a simple custom select box, for example? I need to write a parser, a render and put in the html in another file? Or there is a easer way to do that?

What I want is just write some custom html components (inputs) with specific behavior.

Any tips?

Thanks,
Rodrigo Gomes

Tom Evans

unread,
Jun 15, 2011, 2:11:59 PM6/15/11
to django...@googlegroups.com

The original advice stands - template inclusion tags are just a few
lines of code + the HTML file.

https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#inclusion-tags

Cheers

Tom

Rodrigo Gomes

unread,
Jun 15, 2011, 5:39:16 PM6/15/11
to django...@googlegroups.com
Hi Tom, thanks for your reply.

Inclusion tag seems to be better (to write) than the other kinds of tags. But I think that it is still too many things to do just to write a tag. 

But it works, and I'm doing this way right now :)

Thanks,
Rodrigo Gomes



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Tim Shaffer

unread,
Jun 15, 2011, 6:48:19 PM6/15/11
to django...@googlegroups.com
Another option, if your use-case doesn't require anything terribly complex, is to just include another template and pass your variables to it:
{% include "link_to.html" with url="google.com" text="check out google" %}

In link_to.html:

<a href="{{ url }}">{{ text }}</a>

https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#include

bruno desthuilliers

unread,
Jun 15, 2011, 8:42:27 PM6/15/11
to Django users
On 15 juin, 20:48, Tim Shaffer <timshaf...@me.com> wrote:
> Another option, if your use-case doesn't require anything terribly complex,
> is to just include another template and pass your variables to it:
>
> {% include "link_to.html" with url="google.com" text="check out google" %}
>
> In link_to.html:
>
> <a href="{{ url }}">{{ text }}</a>


Quite a lot of processing for something as simple as link...

Tim Shaffer

unread,
Jun 16, 2011, 1:23:39 AM6/16/11
to django...@googlegroups.com
The link was just an example. It could be a more complex block of HTML. Point being if the tag just massaging data into HTML, you can accomplish it with another template without creating a tag.

Kenneth Gonsalves

unread,
Jun 16, 2011, 1:29:50 AM6/16/11
to django...@googlegroups.com
On Wed, 2011-06-15 at 11:01 -0300, Rodrigo Gomes wrote:
> But I think that I'm missing something. Is it so hard to write a
> simple
> custom select box, for example? I need to write a parser, a render and
> put
> in the html in another file? Or there is a easer way to do that?
>
>

as mentioned, django philosophy is against generating code.
--
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

Rodrigo Gomes

unread,
Jun 16, 2011, 6:03:36 PM6/16/11
to django...@googlegroups.com
Yes, but the point is not about generate code. Is to follow a DRY principle...If I have a "multiple select list" with a specific behavior,look-and-feel, with some javascript surrounding the code, etc, I don't want to write this everywhere...I just want to create once and use it anywhere...with just a line of code...

And I can do this easily with java through tagfiles, and sometimes taglibs. And now with django I'm using inclusion tag as suggested by Tom. 

The only reason that I'm not enjoying inclusion tags so much, is because I can not pass named parameters to them...only positional. But is works

[]'s
Rodrigo Gomes

--
You received this message because you are subscribed to the Google Groups "Django users" group.

Tom Evans

unread,
Jun 17, 2011, 8:31:30 AM6/17/11
to django...@googlegroups.com
On Thu, Jun 16, 2011 at 7:03 PM, Rodrigo Gomes <rgo...@gmail.com> wrote:
> Yes, but the point is not about generate code. Is to follow a DRY
> principle...If I have a "multiple select list" with a specific
> behavior,look-and-feel, with some javascript surrounding the code, etc, I
> don't want to write this everywhere...I just want to create once and use it
> anywhere...with just a line of code...
> And I can do this easily with java through tagfiles, and sometimes taglibs.
> And now with django I'm using inclusion tag as suggested by Tom.
> The only reason that I'm not enjoying inclusion tags so much, is because I
> can not pass named parameters to them...only positional. But is works
> []'s
> Rodrigo Gomes
>

I don't normally pass any parameters; you can instead specify that it
should have the template context available to it instead, and then
pull named parameters out of there.

If the parameters that the inclusion template is expecting have
different names to the ones in the context, then I just alias them
using the {% with %} tag.

Cheers

Tom

Tom Evans

unread,
Jun 17, 2011, 3:46:35 PM6/17/11
to django...@googlegroups.com
On Fri, Jun 17, 2011 at 4:32 PM, Rodrigo Gomes <rgo...@gmail.com> wrote:
> Hi,
> This sounds really better! But I tried it and got an error.
> What I tried to do, is to pass a string value that is not on the context,
> like this:
> {% my_tag with param_str="a string here" %}

{% with some_name_my_tag_wants=some_name tag_var2=var2 %}
{% mytag %}
{% endwith %}

NB {% with %} has different syntax in <1.3

Thinking about it, this could be made easier if there was a
standardized wrapper for parsing arguments. Less boilerplate in the
template, less boilerplate in the tag code.

Cheers

Tom

Rodrigo Gomes

unread,
Jun 17, 2011, 3:32:08 PM6/17/11
to django...@googlegroups.com
Hi, 

This sounds really better! But I tried it and got an error.

What I tried to do, is to pass a string value that is not on the context, like this:

{% my_tag with param_str="a string here" %}

and 

@register.inclusion_tag('my_tag.html',takes_context=True)
def my_tag(context):
    
    param_str = context['param_str']
    ...

But this give me the error:

TemplateSyntaxError at /my/url/

my_tag takes 0 arguments

Am I using the 'with' tag wrongly?

thanks,
Rodrigo Gomes

Reply all
Reply to author
Forward
0 new messages