I have a few values that I would like to pass into a filter and get a URL out of it.
In my template I have:
{% if names %}
{% for name in names %}
<a href='{{name|slugify|add_args:"custid=name.id, sortid=2"}}'>{{name}}</a>
{%if not forloop.last %} | {% endif %}
{% endfor %}
{% endif %}
In my templatetags I have:
@register.filter
def add_args(value, args):
argz = value.strip() + '-' + 'ARGS'
arglist = args.split(',')
for arg in arglist:
keyval = arg.split('=')
argz.join(keyval[0] + 'ZZ' + keyval[1])
argz.join('QQ')
return argz
The output URL should look like:
http://foo.org/john-smith-ARGScustidZZ11QQsortidZZ2
Where ARGS is the start of the arguments, ZZ is '=' and QQ is an '&' equivalent.
First of all: This would work, but I get the custid=name.id coming in the add_args(), where I want to have custid=11 to come in. How pass in the id as an id and not text.
Also, is there a way to just send in an array of key=>value like in PHP. In PHP I would build an array, let say:
arglist = array('custid' => $nameid, 'sortid' => $sortid );
Then I would pass the arglist as an argument to add_args()
and in add_args()
I would do
foreach( arglist as $key => $value)
$argstr .= $key . 'ZZ' . $value . 'QQ'.
Does anyone have a better way of making this work?
Note: if I have to pass all arguments as a string and split them up in the filter I don't mind. I just don't know how to pass the name.id as its value ...