so I have a page parameter sent from server.So I set the currentPage to be event if it is event otherwise default. I'm trying to include only when page is default.
The following code works when current page is not event i.e default but for event page it sends error as follows:
{% set currentPage = 'event' if page === 'event' else
'default' %}
{% include 'partials/default-scripts.njk' if currentPage === 'default' %}
Error:
Template render error:
Error: The `name` parameter is not specified:
I'm not sure what is wrong really.
{% set currentPage = ('event' if page === 'event' else 'default') %}
{% if currentPage == 'default' %}
{% include 'partials/default-scripts.njk' %}
{% endif %}
or
{% include ('partials/default-scripts.njk' if currentPage == 'default' else 'dummy') %}
Where dummy is an empty template.
Related
I could find any solution about this problem. Basically I want to be able to get the data from a Nunjucks variables to be correctly rendered inside a shortcode.
{% for foo in bar %}
{% image "{{foo.src}}", "alt", "sizes", "imgClass" %}
{% endfor %}
but this results in an error
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering njk template ./src/index.html (via TemplateContentRenderError)
[11ty] 2. (./src/index.html)
[11ty] Template render error: (...test.html)
[11ty] EleventyShortcodeError: Error with Nunjucks shortcode `image` (via Template render error)
[11ty] 3. ENOENT: no such file or directory, stat '{{foo.src}}.png' (via Template render error)
So it can not get the right variable value in there. But how can I do this?
You can use the variables directly in that case:
{% set images = [
{
src: "src/images/image-file.jpg",
alt: "image description",
imgClass: "center fit"
},
...
]
%}
{% for item in images %}
{% image item.src, item.alt, item.imgClass %}
{% endfor %}
In my page in django application it has multiple forms such that main form has sub forms .
When submitting I check for all the forms to be valid and if any of the form is invalid , it return the form_invalid function containing the main form .
But the problem is if any of the subform is invalid , it doesn't show any error and the fields are reset as well .
Here is the code when form is submit .
if player_form.is_valid() and stats_form.is_valid() and contacts_form.is_valid() and extra_stats_form.is_valid():
player = player_form.save(commit=False)
stats_form = PlayerStatsForm(request.POST, instance=player.stats)
stats = stats_form.save()
contacts_form = PlayerContactsForm(request.POST, instance=player.contacts)
contacts = contacts_form.save()
extra_stats_form = PlayerExtraStatsForm(request.POST, instance=player.extra_stats)
extra_stats = extra_stats_form.save()
return redirect(self.get_success_url())
else:
return self.form_invalid(player_form)`
The get_context_data function which adds sub_forms into the main player form .
context.update({
'object_title_plural': 'Players',
'list_headings': self.list_headings,
'form_panel_title': 'Add Player',
'save_button_text': 'Add Player',
'search_form': self.form_defaults(PlayerSearchForm()),
'sub_forms': (self.form_defaults(self.get_form(PlayerStatsForm)),
self.form_defaults(self.get_form(PlayerContactsForm)),
self.form_defaults(self.get_form(PlayerExtraStatsForm)))
})
Here is the template in which sub forms are rendered .
{% block display_subforms %}
{% if sub_forms %}
{% include "dashboard/includes/sub_form.html" %}
{% endif %}
{% endblock %}
I am currently investigating if we can make a query string var work within a nunjucks if statement
The below hardcoded value works "5" and brings back the expected result:
{% block content %}
{% for client in clients %}
{% if client.id == 5 %}
{{client.client}}
{% endif %}
{% endfor %}
{% endblock %}
I have the current page id from the url stored in a variable but i can't seem to replace "5" with the actual var
Is this possible?
Edit added JQuery
$(document).ready(function () {
$.urlParam = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
return results[1] || 0;
}
var newid = $.urlParam('id');
alert(newid);
});
Thanks a lot of any time spent.
TCP
I need to know on which type of page I am on in a Shopify App. For example
% if template contains 'product' %}
{% assign entity = product %}
{% elsif template contains 'article' %}
{% assign entity = article %}
{% endif %}
This in a liquid page tell me about product and article pages. What are such tags for say category page or home page?
The template variable tells you the name of the template for the current page. For example, your home page is index.liquid so on that page {{ template }} will output index (the name of the template without ".liquid" extension).
Is that what you're looking for?
I have a 4 column table of products in template,each item in table has an anchor with onclick like this:
<div id="tablepro">
<table>
<tr>
{% for product in cat.products.all %}
{% if forloop.counter|divisibleby:4 %}
</tr>
<tr>
<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>
{% else %}
<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>
{% endif %}
{% endfor %}
</table>
</div>
in remove function I have :
function remove(id)
{
var URL='{% url CompanyHub.views.catDetails company.webSite,cat.id %}';
URL+='delpro/'+id+'/';
$.ajax({
url:URL,
type:'POST',
complete:function(){
var str='<table><tr>';
{% for product in cat.products.all %}
{% if forloop.counter|divisibleby:4 %}
str+='</tr><tr>';
str+='<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>';
{% else %}
str+='<td><center>delete</br><img style="width:200px;height:200px;" class="magnify" src="{{product.image.url}}" /></center></td>';
{% endif %}
{% endfor %}
str+='</table>';
$('#tablepro').html(str);
},
error:function(){
alert('Error');
}
});
}
in views.py :
def deleteProduct(request,key,cat_id,pro_id):
try:
company=Company.objects.get(webSite__iexact=key)
except Company.DoesNotExist:
Http404
cat=Category.objects.get(pk=cat_id)
if pro_id:
try:
product=Product.objects.get(pk=pro_id)
product.delete()
except Product.DoesNotExist:
Http404
return render_to_response('CompanyHub/Company/%s/cat_details.html'%(company.theme),{'company':company,'cat':cat}, context_instance=RequestContext(request))
as U see I've returned cat object that now a product object has removed from its list,but I can't get my Div updated in template!that sounds like cat object has not been updated in template tag.
any suggestion is appreciated
Template are compiled on server side and the browser renders the HTML.
To update your div after ajax call, you'd need to update it using javascript code in complete method. Your server side view can return JSON, XML, HTML or any other data type and your complete method has to render that data. Here is an example of how your complete method should look like if your server side returns partial html (i.e. just the table):
complete:function(data) {
$('#tablepro').html(data);
},
Remember that templates are compiled on the server side and the resulting HTML is then passed to the client. This means that the template code you have within your complete function (in the ajax call) will always be the same - in other words, every time you delete an element (and remove is called), you are redisplaying all the original categories again, as the HTML generated within the for loop is created on the server side once - not asynchronously