Add template in Twig-exel bundle - excel

Some have some example of using template in twig-exel bundle.
Here I give you an example of how I have used it but I can't make it work.
{% xlsdocument {'template': './template_advanced.xlsx'} %}
{% xlssheet %}
{% xlsrow %}
{% xlscell %}Hello2{% endxlscell %}
{% endxlsrow %}
{% xlsrow %}
{% xlscell 1 %}Bar2{% endxlscell %}
{% endxlsrow %}
{% endxlssheet %}
{% endxlsdocument %}
I have changed the address of the file but it don't load.
If I skip the code {'template': '#./template_advanced.xlsx'}, the exel is generated.
Thanks

Related

How to break “for loop” in Django template

My code is:
{% for key, value in section.items %}
{% for key_t, value_t in title.items %}
{% if value_t.section_id == key|add:"0" %}
<li class="nav-item-header"><div class="text-uppercase font-size-xs line-height-xs">
{{value.title}}</div> <i class="icon-menu" title="Tables"></i></li>
{% endif %}
{% endfor %}
{% endfor %}
I want to break the for loop when if the condition is true. like as
{% for key, value in section.items %}
{% for key_t, value_t in title.items %}
{% if value_t.section_id == key|add:"0" %}
<li class="nav-item-header"><div class="text-uppercase font-size-xs line-height-xs">
{{value.title}}</div> <i class="icon-menu" title="Tables"></i></li>
{{break}}
{% endif %}
{% endfor %}
{% endfor %}
How is it possible? please help me...
There is no way to break out of a for loop in Django Template. However, you can achieve this by setting a variable and adding an if statement on the top like this.
{% set isBreak = False %}
{% for number in numbers %}
{% if 99 == number %}
{% set isBreak = true %}
{% endif %}
{% if isBreak %}
{# this is a comment. Do nothing. #}
{% else %}
<div>{{number}}</div>
{% endif %}
{% endfor %}
for some additional help check out this link
https://dev.to/anuragrana/for-loops-in-django-2jdi
or check this answer on stack overflow
How to break "for loop" in Django template

Twig embed and variable scope

I'm running into some problems when nesting twig embeds, more particularly when both embeds have the same variable defined.
An example: a simple code excerpt from a form layout with a fieldset, some rows and input elements (removed some variables for clarity):
{% embed 'components/frmGroup' with {'id':'myFieldset'} %}
{% block main %}
{% embed 'components/frmRow' with {'id':'mySpecialFormRow'} %}
{% block main %}
{% include 'components/inpText' %}
...
{% endblock %}
{% endembed %}
{% embed 'components/frmRow' %}
{% block main %}
{% include 'components/inpText' %}
...
{% endblock %}
{% endembed %}
{% endblock %}
{% endembed %}
As you can see, both frmGroup (fieldset) and frmRow (row) components can take a variable id. The first frmRow embed is fine since it defines its own id, the problem arises with the second frmRow which doesn't need a specific id (and so doesn't define one). But when I look at the outputted html code, I see the formrow carries #myFieldset, the id set on the frmGroup embed. Not what I wanted to accomplish :)
I've tried toying with the "only" keyword but that gave me some very weird results. I could use different variable names (frmGroupId and frmRowId) but that feels lame (and creates clutter), I could also explicitly define and empty id on the second frmRow, but then I have to remember what variables to define on what nested embeds, which isn't pretty either.
So how do I solve this (and can it be solved in Twig)?
I tried your example and I have perfectly normal results:
{% set id = "1" %}
{# id is 1 #}
{% embed "_test.html.twig" with { id: "2"} only %}
{# id is 2 for the scope #}
{% block main %}
{% embed "_test.html.twig" with { id: "3"} %}
{# id is 3 for the scope #}
{% endembed %}
{% embed "_test.html.twig" %}
{# id value is still 2 #}
{% endembed %}
{% endblock %}
{% endembed %}
{% embed "#_test.html.twig" %}
{# id value is 1 #}
{% endembed %}
With _test.html.twig:
{% block main %}
{{ id|default('null') }}
{% endblock %}
It returns
3 2 1
The only think i can think of it for you to use temporary variable:
{% embed "#InddigoMain/_test.html.twig" with { id: "2"} %}
{% block main %}
{% embed "#InddigoMain/_test.html.twig" with { id: "3"} %}
{% endembed %}
{% set temp = id %}
{% set id = null %}
{% embed "#InddigoMain/_test.html.twig" %}
{% endembed %}
{% set id = temp %}
{% endblock %}
{% endembed %}
{% embed "#InddigoMain/_test.html.twig" %}
{% endembed %}
It returns
3 null 1
I have go throught the twig bundle and I don't think it exist.

Pass include path from variable with Twig?

I need to include child-1.twig and child-2.twig in component.twig, and include component.twig in page.twig.
In my page.twig:
{% set items = [
'{% include "child-1.twig" %}',
'{% include "child-2.twig" %}'
] %}
{% include "component.twig" with items %}
In component.twig:
<div class="component">
{% for item in items %}
{{ item }}
{% endfor %}
</div>
The complexity comes from the fact that I cant modify component.twig, only page.twig. My code above would work if {% include "child-1.twig" %} and {% include "child-2.twig" %} were rendered but instead they are printed onto the page as a string of text.
Can I do something similar to my approach but make the child include actually run?
Can I suggest you add an empty block in that file (component.twig)
{% block includes %}{% endblock %}
Then you will be able to do this:
{% embed "component.twig" with items %}
{% block includes %}
{% include "child-1.twig" %}
{% include "child-2.twig" %}
{% endblock %}
{% endembed %}

Navigation link works on mobile but not on desktop

i have this website http://sds-test.nowcommu.myhostpoint.ch/de (please use the "de" at the end) and the last link of the navigation "RECHENZENTRUM" does not works on desktop but works on mobile. How can i solve it? This is the twig code:
{% extends 'partials/base.html.twig' %}
{% set show_onpage_menu = header.onpage_menu == true or header.onpage_menu is null %}
{% macro pageLinkName(text) %}{{ text|lower|replace({' ':'_'}) }}{% endmacro %}
{% block javascripts %}
{% if show_onpage_menu %}
{% do assets.add('theme://js/singlePageNav.min.js') %}
{% endif %}
{{ parent() }}
{% endblock %}
{% block bottom %}
{{ parent() }}
{% if show_onpage_menu %}
<script>
// singlePageNav initialization & configuration
$('#navbar').singlePageNav({
offset: $('#header').outerHeight(),
filter: ':not(.external)',
updateHash: true,
currentClass: 'active'
});
</script>
{% endif %}
{% endblock %}
{% block header_navigation %}
{% if show_onpage_menu %}
<ul class="navigation">
{% for module in page.collection() %}
{% set current_module = (module.active or module.activeChild) ? 'active' : '' %}
<li class="{{ current_module }}">{{ module.menu }}</li>
{% endfor %}
{% set datacenter_page = page.find('/services/datacenter') %}
<li>{{ datacenter_page.menu() }}</li>
</ul>
{% else %}
{{ parent() }}
{% endif %}
{% endblock %}
{% block content %}
{{ page.content }}
{% for module in page.collection() %}
<div id="{{ _self.pageLinkName(module.menu) }}"></div>
{{ module.content }}
{% endfor %}
{% endblock %}
The 'rechenzentrum' link is rendered correctly, and copying the link location via right click gives a correct URL that opens a seemingly correct page. So the template rendering is fine.
The link has two onclick handlers attached to it, though. Probably one of them fails and thus prevents navigation. (The JS is minified so I did not try to debug it.)

Is it possible to turn on spaceless mode only in certain situations in twig?

I want to do something like this:
{% if compress %}{% spaceless %}{% endif %}
...
{% if compress %}{% endspaceless %}{% endif %}
I'm trying to pass ['compress' => true] to the template from PHP to turn on spaceless mode. But it causes an error; template tags need to be nested properly.
Is there any technique that would let me turn spaceless on/off from PHP?
You would have to restructure your template to do something like this instead.
{% import _self as example %}
{% macro stuff(obj) %}
output stuff with {{ obj.name }}, etc...
{% endmacro %}
{% if compress %}
{% spaceless %}
{{ example.stuff(bla) }}
{% endspaceless %}
{% else %}
{{ example.stuff(bla) }}
{% endif %}
Using macros avoids you have to duplicate the content. The import statement at the top is important, so don't forget it.
page.twig:
{% block page %}
page content
{% endblock %}
index.twig:
{% extends 'page.twig' %}
{% block page %}
{% if compress %}
{% spaceless %}
{{ parent() }}
{% endspaceless %}
{% else %}
{{ parent() }}
{% endif %}
{% endblock %}

Resources