is it possible to make the |safe template filter conditional on content

2013-10-02 Thread Mike Dewhirst
I made a custom template filter (ref_href) which converts numbered 
references (like [1], [2] etc) into footnote hyperlinks. It works but 
requires the |safe filter which is dangerous.


To be actually safe I want to *only* use the |safe filter when the data 
contains a numbered reference. Which I cannot do if it is permanently in 
the template. In other words, I want to remove the |safe filter from the 
template and incorporate it into my custom filter which checks that we 
have an integer between the square brackets before doing its work.


Just thinking about it now, I suppose I could put a conditional in the 
template ...


{% if "[" in value %}
  {{ value | ref_href | safe }}
{% else %}
  {{ value }}
{% endif %}

... but that's a lot of typing over dozens of templates. And it isn't 
good enough.


Thanks for any secrets

Mike

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/524D14D9.5090605%40dewhirst.com.au.
For more options, visit https://groups.google.com/groups/opt_out.


Re: is it possible to make the |safe template filter conditional on content

2013-10-03 Thread Bill Freeman
You can certainly write a custom filter.  And if you do that, you can divvy
up your value into footnote links and other, and escape the other parts
yourself, returning the result as a safe string.


On Thu, Oct 3, 2013 at 2:55 AM, Mike Dewhirst  wrote:

> I made a custom template filter (ref_href) which converts numbered
> references (like [1], [2] etc) into footnote hyperlinks. It works but
> requires the |safe filter which is dangerous.
>
> To be actually safe I want to *only* use the |safe filter when the data
> contains a numbered reference. Which I cannot do if it is permanently in
> the template. In other words, I want to remove the |safe filter from the
> template and incorporate it into my custom filter which checks that we have
> an integer between the square brackets before doing its work.
>
> Just thinking about it now, I suppose I could put a conditional in the
> template ...
>
> {% if "[" in value %}
>   {{ value | ref_href | safe }}
> {% else %}
>   {{ value }}
> {% endif %}
>
> ... but that's a lot of typing over dozens of templates. And it isn't good
> enough.
>
> Thanks for any secrets
>
> Mike
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to 
> django-users+unsubscribe@**googlegroups.com
> .
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at 
> http://groups.google.com/**group/django-users
> .
> To view this discussion on the web visit https://groups.google.com/d/**
> msgid/django-users/524D14D9.**5090605%40dewhirst.com.au
> .
> For more options, visit 
> https://groups.google.com/**groups/opt_out
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAB%2BAj0uMUVFmHGY%3DYckq_e03ve1WhKG29ruCUWRU2zaVuFsN2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: is it possible to make the |safe template filter conditional on content

2013-10-03 Thread Mike Dewhirst

On 4/10/2013 12:21am, Bill Freeman wrote:

You can certainly write a custom filter.  And if you do that, you can
divvy up your value into footnote links and other, and escape the other
parts yourself, returning the result as a safe string.


So you mean I detect any non-footnote html in the custom filter myself 
and convert it to Klingon but add my own html for footnotes and flag all 
such fields as safe?


Hadn't thought of that. Might investigate calling the Django safe filter 
from within the custom filter ...


Great idea, thanks

Mike




On Thu, Oct 3, 2013 at 2:55 AM, Mike Dewhirst mailto:mi...@dewhirst.com.au>> wrote:

I made a custom template filter (ref_href) which converts numbered
references (like [1], [2] etc) into footnote hyperlinks. It works
but requires the |safe filter which is dangerous.

To be actually safe I want to *only* use the |safe filter when the
data contains a numbered reference. Which I cannot do if it is
permanently in the template. In other words, I want to remove the
|safe filter from the template and incorporate it into my custom
filter which checks that we have an integer between the square
brackets before doing its work.

Just thinking about it now, I suppose I could put a conditional in
the template ...

{% if "[" in value %}
   {{ value | ref_href | safe }}
{% else %}
   {{ value }}
{% endif %}

... but that's a lot of typing over dozens of templates. And it
isn't good enough.

Thanks for any secrets

Mike

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to django-users+unsubscribe@__googlegroups.com
.
To post to this group, send email to django-users@googlegroups.com
.
Visit this group at http://groups.google.com/__group/django-users
.
To view this discussion on the web visit

https://groups.google.com/d/__msgid/django-users/524D14D9.__5090605%40dewhirst.com.au

.
For more options, visit https://groups.google.com/__groups/opt_out
.


--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAB%2BAj0uMUVFmHGY%3DYckq_e03ve1WhKG29ruCUWRU2zaVuFsN2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/524E0163.7010406%40dewhirst.com.au.
For more options, visit https://groups.google.com/groups/opt_out.


Re: is it possible to make the |safe template filter conditional on content

2013-10-03 Thread Mike Dewhirst

On 4/10/2013 9:44am, Mike Dewhirst wrote:

On 4/10/2013 12:21am, Bill Freeman wrote:

You can certainly write a custom filter.  And if you do that, you can
divvy up your value into footnote links and other, and escape the other
parts yourself, returning the result as a safe string.


So you mean I detect any non-footnote html in the custom filter myself
and convert it to Klingon but add my own html for footnotes and flag all
such fields as safe?

Hadn't thought of that. Might investigate calling the Django safe filter
from within the custom filter ...

Great idea, thanks


And Django is fantastic too :)

All I need to do is ...

from django.utils.safestring import mark_safe

and return mark_safe(value_with_footnote_href) within my custom filter.

... which means I don't have to use the |safe filter and which makes me 
much happier.


Cheers

Mike






Mike




On Thu, Oct 3, 2013 at 2:55 AM, Mike Dewhirst mailto:mi...@dewhirst.com.au>> wrote:

I made a custom template filter (ref_href) which converts numbered
references (like [1], [2] etc) into footnote hyperlinks. It works
but requires the |safe filter which is dangerous.

To be actually safe I want to *only* use the |safe filter when the
data contains a numbered reference. Which I cannot do if it is
permanently in the template. In other words, I want to remove the
|safe filter from the template and incorporate it into my custom
filter which checks that we have an integer between the square
brackets before doing its work.

Just thinking about it now, I suppose I could put a conditional in
the template ...

{% if "[" in value %}
   {{ value | ref_href | safe }}
{% else %}
   {{ value }}
{% endif %}

... but that's a lot of typing over dozens of templates. And it
isn't good enough.

Thanks for any secrets

Mike

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to django-users+unsubscribe@__googlegroups.com
.
To post to this group, send email to django-users@googlegroups.com
.
Visit this group at http://groups.google.com/__group/django-users
.
To view this discussion on the web visit

https://groups.google.com/d/__msgid/django-users/524D14D9.__5090605%40dewhirst.com.au


.

For more options, visit https://groups.google.com/__groups/opt_out
.


--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAB%2BAj0uMUVFmHGY%3DYckq_e03ve1WhKG29ruCUWRU2zaVuFsN2Q%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.




--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/524E66AD.3070104%40dewhirst.com.au.
For more options, visit https://groups.google.com/groups/opt_out.