Hello I'm having problems with a macro.
in macros.html.twig
{% macro panel_header(title) %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ title|escape('html') }}
</div>
{% endmacro %}
in profile.show.html.twig
{% import "macros.html.twig" as macros %}
{{ macros.panel_header("hello" {{profile.name}} ) }}
Above example doesn't work because it asks that arguments are separated by a comma
{{ macros.panel_header("hello {{profile.name}}" ) }}
This outputs "hello {{profile.name}} which makes sense ofcourse
So my question is how can i add a string and a variable as a single argument to a macro?
String interpolation (#{expression}) allows any valid expression to appear within a double-quoted string. The result of evaluating that expression is inserted into the string:
{{ "foo #{bar} baz" }}
{{ "foo #{1 + 2} baz" }}
{{ macros.panel_header("hello #{profile.name}") }}
String interpolation was added in Twig 1.5
Found the answer in:
http://twig.sensiolabs.org/doc/templates.html#other-operators
~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} >would return (assuming name is 'John') Hello John!.
So in my example this works:
{{ macros.panel_header("hello" ~ profile.name ) }}
Related
I'm trying to check if a string is empty in a twig template but I don't understand this behaviour. I want to show a textarea element when the condition is met. Here's the code:
{% if item.payload.customizationText|length == 0 %}
<textarea class="customization-text">{{ item.payload.customizationText|trim }}</textarea>
{% endif %}
It always shows the textarea, whether the string has something in it or not. Am I missing something?
It's in shopware 6 by the way and the dump() fuction throws an error because it's undefined.
Also, this expression just outputs the string. Is the length expression not available?
{{ item.payload.customizationText|length }}
I believe there is a mistake in the question. It does not make sense to print the variable, only if it is empty.
If you want to show the text area in case the customizationText has some contents, you should use this:
{% if item.payload.customizationText|trim|length %}
<textarea class="customization-text">{{ item.payload.customizationText|trim }}</textarea>
{% endif %}
https://twigfiddle.com/nmk2kq/4
You can try it.
{% if item.payload.customizationText is defined and item.payload.customizationText is empty %}
<textarea class="customization-text">{{ item.payload.customizationText|trim }}</textarea>
{% endif %}
I have a for-loop within another for-loop and I would like the inner for-loop to go through the array test[INDEX] with [INDEX] being the index of the outer for-loop. I know I can get the loops index with the variable {{ loop.index() }}, however I do not know how to apply that within the head of my inner loop.
I've tried {% for x in test.{{ loop.index0 }} %}, but that throws me the error
Expected name or number.
Is there any way to do this?
In twig you can also use the array notation to get values from a variable
A note here is that the default loop.index is 1 indexed so you might want to use loop.index0 to get the correct offset
{% for f in foo %}
- {{ f }}: {{ bar[loop.index0] }}
{% endfor %}
As an alternative you can also get the key in the {% for key, value ... format
{% for key, value in foo %}
- {{ value }}: {{ bar[key] }}
{% endfor %}
demo
edit
As for you comment, you can succesfuly use this in any inner-loop, here is a more readable example
{% for country in countries %}
{{ country }}:
{% if cities[loop.index0]|default %}
<ul>
{% for city in cities[loop.index0] %}
<li>{{ city }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
demo
I'm trying to use a pagination inside a terms page (e.g. /layouts/tags/terms.html) because I got many tags.
And I also want to display all my tags sorted by popularity. For the last point, I did:
{{ range .Data.Terms.ByCount}}
...
{{ end }}
And it works fine.
I tried:
1)
{{ range .Data.Terms.ByCount}}
{{ range .Paginator.Pages }}
...
{{ end }}
{{ end }}
But I got "can't evaluate field Paginator in type hugolib.OrderedTaxonomyEntry" ERROR.
2)
{{ range .Paginator.Pages }}
{{ range .Data.Terms.ByCount}}
...
{{ end }}
{{ end }}
I got no error but no posts are displayed, only the pagination is there.
3)
{{ $paginator := .Paginate .Data.Terms.ByCount }}
{{ range $paginator.Pages }}
...
{{ end }}
I got: "error calling Paginate: cannot convert type hugolib.OrderedTaxonomy to Pages" ERROR.
I tried many others things but I couldn't figure out how to do display all my posts by popularity with a pagination in my taxonomy terms.html page.
Do you have more idea ?
In your terms.html page put the following code. It should work.
{{ range .Paginator.Pages }}
{{ .Title }}: {{ .RelPermalink }}
{{ end }}
But since you want it sorted, you probably need something like this.
{{ range .Paginator.Pages.ByDate }}
{{ .Title }}: {{ .RelPermalink }}
{{ end }}
You can sort using other parameters. Check the full list here.
You wanted to sort by popularity. I'm not sure what how you're defining popularity. But if popularity is a front matter variable, you can sort by it with the following.
{{ range (.Pages.ByParam "popularity") }}
<!-- ... -->
{{ end }}
I got the answer from somewhere else; it is:
{{ $topaginate := slice }}
{{ range $k, $v := .Data.Terms.ByCount}}
{{ $topaginate = $topaginate | append (site.GetPage (print $.Data.Plural "/" $v.Term )) }}
{{ end }}
{{ range (.Paginate $topaginate).Pages }}
{{ . }}
{{ end }}
The following does not convert the quotes to HTML entities
{% for row in files %}
<tr data-id="{{ row.id }}"><td>{{ row.name }}</td></tr>
{% endfor %}
The following does convert the quotes to HTML entities
{% for row in files %}
<tr{{ row.id?' data-id="'~row.id~'"' }}><td>{{ row.name }}</td></tr>
{% endfor %}
How can I prevent quotes from being converted to HTML entities in a twig ternary operator?
You should try the |raw filter (check out the documentation).
This is because in general, everything that twig prints out will be escaped to avoid things like cross-site-scripting. An exception is made for entirely static values like {{ '<b>static value</b>' }} which will not be escaped.
In your case, the following should work:
{% for row in files %}
<tr{{ (row.id?' data-id="'~row.id~'"')|raw }}><td>{{ row.name }}</td></tr>
{% endfor %}
I want to do something like this:
{% set c=a+b %}
Where a and b are strings.
How can I do it?
The way to do it is:
{% set c = a ~ b %}
Use the "~" operator. This will concatenate your two strings. The "+" operator cannot be used to combine strings.
You would get this:
{% set c=a~b %}
More info:
The "+" operator: Adds two objects together (the operands are casted to numbers).
You can use:
{{ "Hello " ~ name ~ "!" }}
A clearer example for the {% block page %}...{% endblock %}:
{% block page %}
{% set page = page | merge({
"title" : branchName,
"description" : "This description has "~branchName~" as its title"
}) %}
{{ parent() }}
{% endblock %}
A clearer example for the {% block content %}...{% endblock %}:
{% block content %}
This is just a sample string for {{ branchName }} that needs no concatenation
{% endblock %}