Re: citations

21 views
Skip to first unread message

Russell Keith-Magee

unread,
Dec 29, 2012, 8:37:17 PM12/29/12
to django...@googlegroups.com

On Sat, Dec 29, 2012 at 10:26 PM, <samt...@gmail.com> wrote:
Hi,

I am creating a site, and trying to implement a referencing/citation system.

I'd like to render something like
"Lorem ipsum dolor sit amet[[ref:foo2012]], consectetur adipisicing elit, sed do eiusmod tempor incididunt[[ref:bar2011]] ut labore et dolore magna aliqua."
stored in a TextField,
"Lorem ipsum dolor sit amet[1], consectetur adipisicing elit, sed do eiusmod tempor incididunt[2] ut labore et dolore magna aliqua."

"[1] foo, 'something something' (2012)
[2] bar, 'something something' (2011)
"
where "foo2012" is the slug of a reference object, that has a title, author, data etc.

I have made a filter that can scan though a string, pick out the [[]] tags and look up the reference. I can use this in  a template like {{ site.description|refs }}. However i need somewhere to store mapping of numbers to references for that page, so that i can filter multiple blocks on the same page without resetting the numbering. I would then use a tag at the end of the page, that rendered the collected list.

The docs on custom tags and filters talks about render_context, but does not show how to use them with filters (only with tags).

Am i going about this the wrong way? should filters be stateless? i realise that parsing relationships out of a text field is not good database style, but i can't think of another way to allow editors to put citations into text. i would also like to use a similar tag for cross links in the text (eg [[site:foo]] -> "/sites/view/foo").

What you're describing is just a markup language -- if you look in django.contrib.markup, you'll find 3 examples of analogous markup languages. So yes, this is an entirely appropriate way to handle user-provided content.

As for implementation; filters are generally stateless -- they're really not much more than a way to access a function in a template. However, that's got more to do with convention than hard rules. There's nothing that *requires* them to be stateless. Writing to the context in a filter isn't prohibited; it's just unusual (which is why the docs don't provide a specific example). Using one tag to extract references, and a second to print those references sounds entirely appropriate. 

The other approach would be to use a template tag. The only advantage here would be that tags often have context-modifying side effects, so it would be marginally less surprising to an outside observer to see a tag modify context. However, that's really just a developer communication issue -- if there's enough documentation (formal or informal) about what the pair of filters are doing, I don't see any reason why you shouldn't use a filter for this purpose.

Yours,
Russ Magee %-)

samt...@gmail.com

unread,
Jan 3, 2013, 12:07:30 PM1/3/13
to django...@googlegroups.com

I have it working using a filter to process the citation markup into numbers (eg [[ref:sam2013foo]] -> to [1]) and a tag to output the reference list (eg "[1] Foo Bar, sam, (2013)"). But as I can't access the render_context from a filter, i am storing the reference numbers in a list in global scope of the templatetags script. This does not seem a very good way to do it, and means that the tag that outputs the reference list, must clear the list, otherwise each time the page is refreshed the list grows.

is there anyway to find the render_context in a filter?

thanks,
Sam

Reply all
Reply to author
Forward
0 new messages