Custom tag in Django How to get the value of a variable parameter

Source: Internet
Author: User
This two days to learn to write a Django tag, this tag is called "Post_detail_tag", the calling code like this:

{% Post_detail_tag post.id current_post_id%}
The last two are the passed variables.

As a result, I found that in the tag code, when the first resolution passed the token of the past, you can get only "post.id", "current_post_id" the variable name, without obtaining its variable value.

Of course, think about it indeed, because this time there is no context. In the tag is only "Post_detail_tag post.id current_post_id" This string, but also in the low-level tokenize stage.

It is therefore natural to think that the actual variable value acquisition must be obtained in the Render method of the corresponding Node class of the tag, because this time the parameter will be passed in the context.

Oddly enough, a common need to get variable values is not shown in the documentation, and is not found in the relevant sample code. Maybe I didn't read it carefully.

Look at d:/python24/lib/site-packages/django-0.95.1-py2.4.egg/django/template/__init__.py's source code to find resolve_variable this function. In addition to FilterExpression also have a resolve function, but this internal or call resolve_variable. So the import function tried it and found that the value of the variable could be taken.

Code:
From Django Import Template
From django.template import context, template, loader, resolve_variable

Register = template. Library ()

Class Postdetailnode (template. Node):
def __init__ (self, ID, current_post_id):
Self.id = ID
self.current_post_id = current_post_id
Pass

def render (self, context):
current_post_id = Int (resolve_variable (self.current_post_id, context))
context[' current_post_id '] = current_post_id
t = loader.get_template ("forum/templatetags/post_detail.html")
return T.render (context)

# @register. Tag (name= ' Post_detail_tag ')
def do_post_detail_tag (parser, token):
Try:
# tag_name, args = Token.contents.split (None, 1)
# id, current_post_id = args.split (None, 1)
Tag_name, id, current_post_id = token.split_contents ()
Print ID, current_post_id
Except ValueError:
Raise template. Templatesyntaxerror, "%s tag requires argument"% tag_name

return Postdetailnode (ID, current_post_id)

Register.tag (' Post_detail_tag ', Do_post_detail_tag)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.