maybe it help you:
custom methods:
@register.simple_tag
def category_tree_custom(id=None):
"""
Creates an unnumbered list of the categories.
Example::
<ul class="lev1">
<li>Books
<ul class="sub1">
<li>Science Fiction
<ul class="sub2">
<li>Space stories</li>
<li>Robot stories</li>
</ul>
</li>
<li>Non-fiction</li>
</ul>
</ul>
"""
active_cat = None
if id:
try:
active_cat = Category.objects.active().get(id=id)
except Category.DoesNotExist:
active_cat = None
# We call the category on every page so we will cache
# The actual structure to save db hits
current_site = Site.objects.get_current()
cache_key = "cat-%s" % current_site.id
existing_tree = cache.get(cache_key, None)
if existing_tree is None:
root = Element("ul")
lev_name = 'lev1'
root.set('class',lev_name)
for cats in Category.objects.root_categories():
recurse_for_children(cats, root, active_cat)
existing_tree = root
cache.set(cache_key, existing_tree)
# If we have an active cat, search through and identify it
# This search is less expensive than the multiple db calls
if active_cat:
active_cat_id = "category-%s" % active_cat.id
for li in existing_tree.getiterator("li"):
if li.attrib["id"] == active_cat_id:
link = li.find("a")
link.attrib["class"] = "current"
break
return tostring(existing_tree, 'utf-8')
@register.tag
def categories_for_slugs_custom(parser, token):
"""
Usage: {% categories_for_slugs_sf "slug[,slug...]" as varname %}
Sets the variable *varname* in the context to a list of
categories, given by
the list of slugs.
Useful if you want to specify a custom list of categories and
override the
default category listing from satchmo.
Example usage::
{% categories_for_slug "hats,boots,accessories" as categories
%}
<ul>
{% for child in categories.child.active %}
<li><a
href="{{ child.get_absolute_url }}">{{ child.translated_name }}</a></
li>
{% endfor %}
</ul>
"""
try:
# Splitting by None == splitting by spaces.
tag_name, arg = token.contents.split(None, 1)
except ValueError:
raise TemplateSyntaxError, "%r tag requires arguments" \
% token.contents.split()[0]
m = re.search(r'"([^ "]+)" as (\w+)', arg)
if not m:
raise TemplateSyntaxError, "%r tag had invalid arguments" \
% tag_name
cat_slugs, var = m.groups()
cats=[]
for cat_slug in cat_slugs.split(','):
try:
cat = Category.objects.get(slug__iexact=cat_slug)
except Category.DoesNotExist:
log.warn("No category found for slug: %s", cat_slug)
cat = None
cats.append(cat)
return SetVariableInContextNode(var, cats)
On Jul 16, 1:44 am, lzantal <[email protected]> wrote:
> Hi,
>
> Make sure the templatetags is a package, so it needs a __init__.py,
> Also you don't need a custom category tag if all you want is collapse
> the subcategories.
> You just need to would iterate through the category list with jQuery.
> Could you post the generated html so I can write up a simple example?
> All my category template tags are modified so I don't have a vanilla
> satchmo category template tag.
>
> lzantal
>
> On Jul 15, 11:19 am, RT <[email protected]> wrote:> Where are custom
> templatetags supposed to live? I put it in store/
> > localsite/templatetags/update_category.py (store is the top directory
> > of the store)
> > Then I changed the top of base.html to look like this:
> > {% load i18n update_category satchmo_google satchmo_util
> > satchmo_currency satchmo_discounts app_plugins normalize_decimal %}
>
> > Now when I try to go to the site I get an error saying:
>
> > 'update_category' is not a valid tag library: Template library
> > update_category not found, tried
>
> django.templatetags.update_category,satchmo_store.shop.templatetags.update_
> category,django.contrib.admin.templatetags.update_category,django.contrib.c
> omments.templatetags.update_category,sorl.thumbnail.templatetags.update_cat
> egory,livesettings.templatetags.update_category,satchmo_utils.thumbnail.tem
> platetags.update_category,satchmo_store.contact.templatetags.update_categor
> y,tax.templatetags.update_category,product.templatetags.update_category,pay
> ment.templatetags.update_category,payment.modules.giftcertificate.templatet
> ags.update_category,satchmo_ext.upsell.templatetags.update_category,satchmo
> _ext.productratings.templatetags.update_category,satchmo_utils.templatetags
> .update_category,app_plugins.templatetags.update_category
>
>
>
>
>
> > What am I doing wrong? Do i need to specify where templatetags live?
> > and thanks for all the help so far!
> > RT
>
> > On Jul 15, 9:51 am, Stuart Laughlin <[email protected]> wrote:
>
> > > category_tree is a template tag that outputs the HTML in question.
>
> > > Check "satchmo_store/shop/templatetags/satchmo_category.py" for the
> > > implementation of category_tree.
>
> > > --Stuart
>
> > > On Thu, Jul 15, 2010 at 11:40 AM, RT <[email protected]> wrote:
> > > > Thanks for the help, I am a little bit confused by the way Satchmo
> > > > works with Django (I'm pretty new to both) so if this is the code
> > > > which then generates the sidebar as a <ul>:
>
> > > > <h3>{% trans "Shop Categories" %}</h3>
> > > > {% block sidebar-categories %}
> > > > <div id="menu_container">
> > > > {% if category.id %}
> > > > {% category_tree category.id %}
> > > > {% else %}
> > > > {% if product.get_category %}
> > > > {% category_tree product.get_category.id %}
> > > > {% else %}
> > > > {% category_tree %}
> > > > {% endif %}
> > > > {% endif %}
> > > > </div>
> > > > How exactly do I use jquery with it? Where is the <ul> coming from?
>
> > > > On Jul 14, 10:40 am, lzantal <[email protected]> wrote:
> > > >> Hi,
>
> > > >> On Jul 14, 10:11 am, RT <[email protected]> wrote:
>
> > > >> > I have searched a bit and haven't found anything about this. Is
> > > >> > there
> > > >> > any easy way to make subcategories collapse into their parent
> > > >> > category
> > > >> > and only show when you click the parent (in the left navigation of
> > > >> > the
> > > >> > simple store). Has this been implemented or do I need to implement
> > > >> > it
> > > >> > myself. Thanks!
>
> > > >> I use jquery.toggle() to do that. Just wrap the subcategories in a
> > > >> div
> > > >> and bind toggle event to the parent which will show and hide the
> > > >> subcategories for you.
>
> > > >> lzantal
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "Satchmo users" group.
> > > > To post to this group, send email to [email protected].
> > > > To unsubscribe from this group, send email to
> > > > [email protected].
> > > > For more options, visit this group
> > > > athttp://groups.google.com/group/satchmo-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Satchmo users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/satchmo-users?hl=en.