How to use multidimensional array in twig below we share input field - twig

How I used this HTML tag with php code in twig.
--name="product_option['option_row'][product_option_value]['option_value_row'][pod]['pod_row'][customer_group_ids][]" --
--<?php if (in_array($customer_group['customer_group_id'], $pod['customer_group_ids'])) { ?> checked="checked"<?php } ?> />--

Use the in-operator for this:
<input type="checkbox" name="chk_foo"{% if customer_group['customer_group_id'] in pod['customer_group_ids'] %} checked="checked"{% endif %} />
fiddle

Related

How can I convert this TPL or PHP condition to a valid condition?

I would like to add a condition to my twig file, but I can only find code examples for TPL or PHP. All my attempts to translate this into twig syntax have so far been unsuccessful. This may also be due to the fact that these examples are no longer up-to-date. Unfortunately, there is little current information about the open source vanilla forum. But as long as I don't even know if my twig syntax should actually work, I can't say that.
Can someone please help me and tell me if my twig syntax is correct?
Thank you for help!
This is what the relevant part of my twig looks like (I only want to display the main panel for Signed In users):
<div class="Frame-details">
{% if not isHomepage %}
<div class="Frame-row">
<nav class="BreadcrumbsBox" aria-label={{ t("Breadcrumb") }}>
{{ renderBreadcrumbs() }}
</nav>
</div>
{% endif %}
<div class="Frame-row">
<!-- Main Content -->
<section class="Content MainContent">
{{ renderControllerAsset("Content") }}
</section>
<!-- Main Content END -->
<!-- Main Panel -->
{% if UserSignedIn %}
<div class="Panel Panel-main">
{{ renderControllerAsset("Panel") }}
</div>
{% endif %}
<!-- Main Panel END -->
Here are the examples I've found so far:
For TPL is the following example:
{if $User.SignedIn}
Do some coding like show the Panel Asset
{/if}
For PHP is the following example:
$Session = Gdn::Session();
if ($Session->IsValid())
{
Show the Panel Asset
} else
{
Show only the Guest Box asset

OctoberCMS set param in configuration section of template

I'm trying to render a conditional form to a twig template in OctoberCMS...
[renderForm] formCode = "contact-form"
==
{% if data.show_form == true %}
<div class="container">
<div class="row">
<div class="col unnamed-character-style-86">
This will be a form {{ data.form_to_show }}!!!
{% component 'renderForm' %}
</div>
</div>
</div>
{% endif %}
I need to set the formCode variable to the template data data.form_to_show you can see in the twig section.
I'm really new to October and am a bit stuck on this but it sounds like it should be simple enough.
All help welcome.
I was being an idiot and worked it out myself.
[renderForm] formCode = {{ data.form_to_show }}

not able to POST required data in django templates

I want to POST a form from my html to views.py in django, but I am not able do it.
This is my html. This form should post the url of the downloaded image to the views.py function.
{% for x in photo %}
<a class="down" href="{{x.image.url}}" onclick="myfunc()" download="none">get</a>
<form action="/image_info/" method="POST" id='dform'>
{% csrf_token %}
<input name='d_button' type="hidden" value="{{x.image.url}}">
</form>
{% endfor %}
This is my javascript function to submit form whenever the get link is pressed
<script type="text/javascript">
function myfunc()
{
document.getElementById("dform").submit();
console.log('hello');
}
</script>
this is my views.py. This should take the image url from the form and display it in the new page.
def show_image():
image_url = request.POST.get('d_button', None)
return render(request, 'show_image.html', {'image': image_url)
but my problem is that the form is not returning the url of the image that is clicked instead it is returning the url of first link. for example
link1
link2
link3
link4
if I click on link3, it is downloading the image in link3 but POSTING the url of link1.
This is a bit tricky to explain but this is the best I can.
Thanks in advance.
HTML ids are supposed to be unique. You are looping while generating these forms and hence generate a bunch of duplicate ids, so when you write document.getElementById("dform") the first matching element is selected.
One solution would be to use forloop.counter to generate unique ids and use them. We would set these ids as an attribute on the anchor and pass the anchor element to the onclick function:
{% for x in photo %}
<a class="down" href="{{x.image.url}}" onclick="myfunc(this)" data-target="dform-{{ forloop.counter }}" download="none">get</a>
<form action="/image_info/" method="POST" id='dform-{{ forloop.counter }}'>
{% csrf_token %}
<input name='d_button' type="hidden" value="{{x.image.url}}">
</form>
{% endfor %}
Now in your javascript:
<script type="text/javascript">
function myfunc(elem)
{
document.getElementById(elem.getAttribute("data-target")).submit();
console.log('hello');
}
</script>

Inline form template/theme rendering where declared

I have this simple form where I need a custom template for a field to render something right next to the <input> tag. Since I won't be needing this anywhere else, I thought I'd put it right in the same template as the form like suggested here:
{% form_theme form _self %}
{% block text_widget %}
{{ block('form_widget_simple') }}
something
{% endblock %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
That's pretty much the whole template (to be used with ajax, hence no surrounding markup).
The issue now is, "something" gets rendered right at the beginning of the output where the block text_widget is declared, as would any other block. Its rendered fine in the form next to the <input>:
something
<form name="form" method="post" action="">
<table id="form"><tr>
<td> <label for="form_Search" class="required">Search</label></td>
<td> <input type="text" id="form_Search" name="form[Search]" required="required" autofocus="autofocus" />
something
</td>
</tr><tr style="display: none">
<td colspan="2"><input type="hidden" id="form__token" name="form[_token]" value="dUwdoiz9vo1TJTRjvyUcz9Rwd-D7pTvqUH-R0zCtg28" /></td>
</tr></table>
</form>
This obviously makes inline theming completely unusable, so I think I might be doing something wrong...
How do I get rid of that extra "something" at the beginning?
Having the question already written up and also solved the problem, I might as well answer:
The solution is to derive the template from a dummy base template to swallow any output that's outside of blocks defined in the base template:
{# empty.html.twig #}
{% block content %}
{% endblock %}
And for the actually needed template:
{% extends 'empty.html.twig' %}
{% form_theme form _self %}
{% block text_widget %}
{{ block('form_widget_simple') }}
something
{% endblock %}
{% block content %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
One probably wouldn't think twice about it when customizing a field in a regular template that already uses inheritance, but this way it feels like a hack...

Cancelling parent template if variable is null in Twig

I'm new to Twig and I'm trying to figure out how to achieve the following.
I have an object with a set of properties that I want to render with different Twig templates depending on which type of property it is (text, images, date, etc).
I want to render them as follows:
<div class="row">
<div class="title">Title of property</div>
<div class="propertycontent">
//Specific property content depending on property type
</div>
</div>
My problem is that I can't figure out to skip the complete output if the property is not defined. I want to be able to use parent templates to take care of the "wrapping" of the rendered property content. Is it possible to use parent templates that returns nothing if the property value is undefined? Is there another good solution that does not rely on include "begin"/"end" for wrapping each template?
Thanks in advance.
Solution: Call parent with value(might be null) and title
Parent (propery_wrapper.twig):
{% if value %}
<div class="row">
<div class="title">{{title}}</div>
<div class="propertyContent">
{% block content %}{% endblock %}
</div>
</div>
{% endif %}
child (for height property):
{% extends 'property_wrapper.twig' %}
{% block content %}
{{ value.value|number_format(2, ',') }} m
{% endblock %}

Resources