2009-09-29 17 views
15

के साथ Django समावेशन टैग मैंने एक समावेश टैग बनाया है, हालांकि मैं वैकल्पिक रूप से कॉन्फ़िगर करने योग्य टेम्पलेट बनाने में सक्षम होना चाहता हूं। बॉक्स के बाहर इस के लिए समर्थन प्रतीत नहीं होता है, इसलिए मैं देखना चाहता हूं कि लोगों ने यह कैसे किया - शायद एक विधि टेम्पलेट निर्देशिका को किसी विशिष्ट टेम्पलेट नाम के लिए खोजती है और फिर डिफ़ॉल्ट टेम्पलेट पर वापस आती है।कॉन्फ़िगर करने योग्य टेम्पलेट

@register.inclusion_tag('foo.html', takes_context=True) 
+0

आप '{% my_template%} को शामिल करने के लिए क्या कर रहे हैं 'नहीं कर सकते हैं? –

+0

मुझे कस्टम टैग के साथ संदर्भ में कुछ चर जोड़ने की जरूरत है। ऐसा लगता है कि मुझे टैग को लंबा रास्ता लिखना होगा। धन्यवाद। – meppum

उत्तर

4

inclusion_tag डेकोरेटर सिर्फ एक शॉर्टकट है - यह एक विशिष्ट संदर्भ के साथ एक विशिष्ट टेम्पलेट प्रतिपादन के एक आसान तरीका के रूप में मतलब है। जैसे ही आप इसके बाहर जाना चाहते हैं, यह अब आपकी मदद नहीं कर सकता है। लेकिन इसका मतलब है कि आपको टैग को लंबे समय तक लिखना होगा, जैसा कि प्रलेखन में बताया गया है, और पैरामीटर के रूप में इच्छित टेम्पलेट को पास करना होगा।

3

मुझे इस परियोजना के लिए ऐसा कुछ करना पड़ा और चूंकि हमें इस तरह के एक से अधिक शामिल टैग की आवश्यकता थी क्योंकि मैंने django inclusion_tag सजावट के आधार पर एक सजावटी बनाया था। यह कोड है:

# -*- coding: utf-8 -*- 
from django import template 
from inspect import getargspec 
from django.template.context import Context 
from django.template import Node, generic_tag_compiler, Variable 
from django.utils.functional import curry 


def inclusion_tag(register, context_class=Context, takes_context=False): 
    def dec(func): 
     params, xx, xxx, defaults = getargspec(func) 
     if takes_context: 
      if params[0] == 'context': 
       params = params[1:] 
      else: 
       raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'") 

     class InclusionNode(Node): 
      def __init__(self, vars_to_resolve): 
       self.vars_to_resolve = map(Variable, vars_to_resolve) 

      def render(self, context): 
       resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] 
       if takes_context: 
        args = [context] + resolved_vars 
       else: 
        args = resolved_vars 

       file_name, extra_context = func(*args) 

       from django.template.loader import get_template, select_template 
       if not isinstance(file_name, basestring) and is_iterable(file_name): 
        t = select_template(file_name) 
       else: 
        t = get_template(file_name) 
       self.nodelist = t.nodelist 
       new_context = context_class(extra_context, autoescape=context.autoescape) 
       # Copy across the CSRF token, if present, because inclusion 
       # tags are often used for forms, and we need instructions 
       # for using CSRF protection to be as simple as possible. 
       csrf_token = context.get('csrf_token', None) 
       if csrf_token is not None: 
        new_context['csrf_token'] = csrf_token 
       return self.nodelist.render(new_context) 

     compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode) 
     compile_func.__doc__ = func.__doc__ 
     register.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 
     return func 
    return dec 

आपको टेम्पलेट (या टेम्पलेट सूची) और संदर्भ निर्देश के साथ एक टुपल वापस करना होगा। ध्यान दें कि आप रजिस्टर (लाइब्रेरी उदाहरण) डेकोरेटर कॉल में पारित करने के लिए है:

from somewhere import inclusion_tag 
@inclusion_tag(register) 
def display_formset(formset): 
    template_name = FORMSET_TEMPLATES.get(formset.model, 
     'includes/inline_formset.html') 
    return (template_name, {'formset': formset}) 

आशा इस मदद करता है

+0

किसी भी विचार को django 1.4 के लिए इसे कैसे अपडेट करें? Generic_tag_compiler को अब टोकन और पार्सर तर्कों की आवश्यकता है – Gattster

28

मैं simple_tag का उपयोग जब मैं कि क्या करने की जरूरत:

from django.template import Library, loader, Context 

@register.simple_tag(takes_context=True) 
def my_tag(context, template_name): 

    var1 = ... 

    t = loader.get_template(template_name) 
    return t.render(Context({ 
     'var1': var1, 
     ... 
    })) 
7

इस पोस्ट http://djangosnippets.org/snippets/1329/

कुंजी "डमी टेम्पलेट" में जोड़ने के लिए है::

मेरे जीवन को बचाने के
+0

यह एक महान युक्ति है! इससे भी बेहतर, '@ register.inclusion_tag'' टेम्पलेट 'उदाहरण (पथ के अतिरिक्त) ले सकता है, ताकि आप कुछ ऐसा कर सकें जैसे' डमी = टेम्पलेट ("" "{% टेम्पलेट%} बढ़ाता है") " फिर '@ register.inclusion_tag (डमी, take_context = True)'। '' ' – Emil

0

एक समाधान नियमित inclusion_tag हो सकता है जो context पर गतिशील टेम्पलेट नाम पास करता है।

इस तरह

:

# templatetags/tags.py 

@register.inclusion_tag('include_tag.html', takes_context=True) 
def tag_manager(context): 
    context.update({ 
     'dynamic_template': resolve_template(context), 
    }) 
    return context 

और टेम्पलेट:

<!-- include_tag.html --> 

{% include dynamic_template %} 

चाल यहाँ है, जब मैं {% tag_manager %} कहते हैं, यह जो बारी में (resolve_template() द्वारा लौटाए गए टेम्पलेट शामिल नहीं शामिल शामिल संक्षिप्तता के लिए)।

उम्मीद है कि यह मदद करता है ...

संबंधित मुद्दे

 संबंधित मुद्दे