I have a custom tag in Django project:
class ExampleNode(template.Node):
def __init__(self,
nodelist,
header_copy='',
paragraph_copy='',
):
self.nodelist = nodelist
self.header_copy = header_copy
self.paragraph_copy = paragraph_copy
def render(self, context):
template = 'example.html'
context = {
'elements': self.nodelist.render(context),
"header_copy": self.header_copy,
"paragraph_copy": self.paragraph_copy,
}
return render_to_string(template, context)
@register.tag
def example_component(parser, token):
try:
tag_name, header_copy, paragraph_copy = token.split_contents()
except ValueError:
raise TemplateSyntaxError("%r takes two arguments" % token.contents.split()[0])
nodelist = parser.parse(('endexample_component',))
parser.delete_first_token()
return ExampleNode(nodelist, header_copy, paragraph_copy)
And the view of the component is:
<div>
<h1>{{ header_copy }}</h1>
<p>{{ paragraph_copy }}</p>
</div>
<div>{{ elements }}</div>
Usage:
{% with header_copy='Lorem ipsum dolor sit amet' paragraph_copy='Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.' %}
{% example_component header_copy paragraph_copy %}
<div>ex1</div>
<div>ex2</div>
{% endexample_component %}
{% endwith %}
And the issue is that what I am receiving in view is header_copy and paragraph_copy instead of Lorem ipsum. Only passing actual primitive value works.
Maybe there is a better way to receive those args and parse them?