Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jinja2 Tag Support #268

Merged
merged 2 commits into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions longclaw/basket/jinja2/basket/add_to_basket.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<button id="btn-add-to-basket" class="{{btn_class}}" data-variant-id="{{variant_id}}">
{{btn_text}}
</button>
{{ longclaw_vendors_bundle()|safe }}
{{ longclaw_client_bundle()|safe }}
<script type="text/javascript">
var btn = document.getElementById('btn-add-to-basket');
btn.addEventListener("click", function (e) {
longclawclient.basketList.post({
prefix: "{{ longclaw_api_url_prefix() }}",
data: {
variant_id: e.target.dataset.variantId
}
});
});
</script>
34 changes: 34 additions & 0 deletions longclaw/basket/jinja2tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import jinja2
from jinja2 import nodes
from jinja2.ext import Extension

from django.template.loader import get_template

from .templatetags.basket_tags import get_basket_items
from .utils import get_basket_items


def add_to_basket_btn(variant_id, btn_class="btn btn-default", btn_text="Add To Basket"):
"""Button to add an item to the basket
"""
basket_template = get_template('basket/add_to_basket.html')

return basket_template.render(context={
'btn_class': btn_class,
'variant_id': variant_id,
'btn_text': btn_text
})


class LongClawBasketExtension(Extension):
def __init__(self, environment):
super(LongClawBasketExtension, self).__init__(environment)

self.environment.globals.update({
'basket': jinja2.contextfunction(get_basket_items),
'add_to_basket_btn': add_to_basket_btn,
})


# Nicer import names
basket = LongClawBasketExtension
21 changes: 21 additions & 0 deletions longclaw/checkout/jinja2tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import jinja2
import jinja2.nodes
from jinja2.ext import Extension

from django.template.loader import get_template

from .templatetags.longclawcheckout_tags import gateway_client_js, gateway_token


class LongClawCheckoutExtension(Extension):
def __init__(self, environment):
super(LongClawCheckoutExtension, self).__init__(environment)

self.environment.globals.update({
'gateway_client_js': gateway_client_js,
'gateway_token': gateway_token
})


# Nicer import names
checkout = LongClawCheckoutExtension
1 change: 1 addition & 0 deletions longclaw/core/jinja2/core/longclaw_script.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script type="text/javascript" src="{{ static(path) }}"></script>
39 changes: 39 additions & 0 deletions longclaw/core/jinja2tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import jinja2
import jinja2.nodes
from jinja2.ext import Extension

from django.template.loader import get_template

# to keep namespaces from colliding
from .templatetags import longclawcore_tags as lc_tags


def longclaw_vendors_bundle():
template = get_template('core/longclaw_script.html')

context = lc_tags.longclaw_vendors_bundle()

return template.render(context=context)


def longclaw_client_bundle():
template = get_template('core/longclaw_script.html')

context = lc_tags.longclaw_client_bundle()

return template.render(context=context)


class LongClawCoreExtension(Extension):
def __init__(self, environment):
super(LongClawCoreExtension, self).__init__(environment)

self.environment.globals.update({
'longclaw_api_url_prefix': lc_tags.longclaw_api_url_prefix,
'longclaw_client_bundle': longclaw_client_bundle,
'longclaw_vendors_bundle': longclaw_vendors_bundle,
})


# Nicer import names
core = LongClawCoreExtension