I would like to display the following array of data in twig:
array:7
0 => []
1 => "Q4(2017)"
2 => "Q1(2018)"
3 => "Q2(2018)"
4 => "Q3(2018)"
5 => "Q4(2018)"
6 => "Q1(2019)"
]
I tried to display the above array of data in Symfony Twig as following but it returned error. Could you please advise?
{% for key, value in arrayList %}
<br>{{ value }}<br>
{% endfor %}
I expect the results as following but it returned errors
Q4(2017)
Q1(2018)
Q2(2018)
Q3(2018)
Q4(2018)
Q1(2019)
You don't need to pass key and value as parameters.
{% for value in arrayList %}
<br>{{ value }}<br>
{% endfor %}
Related
In Twig, I pass an array params to a macro. Within the macro I check if all required elements (here: required_element) of params are set. If not, I want to set that to a default value.
This code is working, but I think there must be a better way:
{% macro my_macro(params) %}
{% if not params.required_element is defined %}
{% set params = params|merge({'required_element: 'value'}) %}
{% endif %}
I thought of something shorter like {% params.required_element ?: value %}, but that does not work.
For various reasons I do not want pass them one by one as parameters to the macro, so this is not an alternative:
{% macro my_macro(required_element, another_element, yet_another, puh_another, next_element) %}
Nope, that is the only (vanilla) way. If you want to make it shorter u'd need to extend twig with something like this:
<?php
$twig = new \Twig\Environment($loader);
$function = new \Twig\TwigFunction('set_array_value', function (&$context, $array_name, $index, $value) {
if (!isset($context[$array_name])) return;
/**
- If value is array just do arr[index] = value
- Otherwise test if object
-> test if method $index exists
-> test if there is a setter for the property $index
**/
if (is_array($context[$array_name])) $context[$array_name][$index] = $value;
elseif(is_object($context[$array_name])) {
if (method_exists($context[$array_name], $index)) $context[$array_name]->{$index}($value);
elseif(method_exists($context[$array_name], 'set'.$index)) $context[$array_name]->{'set'.$index}($value);
}
}, ['needs_context' => true, ]);
$twig->addFunction($function);
{% macro foo(my_array) %}
{% if not array.foo is defined %}{% do set_array_value('my_array', 'foo', 'bar') %}{% endif %}
{{ my_array.foo }}
{% endmacro %}
{% import _self as sandbox %}
{{ sandbox.foo({}) }} #output: bar
{{ sandbox.foo({'foo': 42,}) }} #output: 42
I have an object which, in JSON, would look like this:
{
'class': ['nav-link', 'dropdown-toggle'],
'data-toggle': ['dropdown']
}
I need to then be able to append another class to the class array inside the object.
This code doesn't seem to work; it just overwrites the class array.
{% set link_attribs = { 'class' : ['nav-link', 'dropdown-toggle'], 'data-toggle':'dropdown'} %}
{% set link_attribs = link_attribs|merge({'class': ['highlighted']}) %}
Really I want to do something like this, but it just throws a punctuation error.
{% set link_attribs.class = link_attribs.class|merge(['highlighted']) %}
Any ideas?
Using Twig, you can't set object properties directly, so "set (...).class" will never work. But instead, you can create a new variable that will inherit from both default and options values (just like in most JavaScript codes).
For example:
{%
set options = link_attribs | merge({
'class': link_attribs.class | merge(['highlighted'])
})
%}
{% for class in options.class %}
{{ class }}
{% endfor %}
Will display:
nav-link
dropdown-toggle
highlighted
See fiddle.
This looks like it works:
{% set c = link_attribs.class %}
{% set c = c|merge(['highlighted']) %}
{% set link_attribs = link_attribs|merge({'class': c}) %}
Not sure if its the most elegant way though.
I am totally new to Symfony and i d like to know how I manage to display multiple data sets from my db in a twig...
untill now my attempt is the following:
class ProjController extends Controller
{
/**
* #Route("/", name="_projekte")
* #Template("FHBingenBundle:projekte:list.html.twig")
*
*/
public function indexAction()
{
$projekte = $this->getDoctrine()
->getRepository('FHBingenBundle:Projekt')->findAll();
return $projekte;
}
}
to get all the data sets. Here is where my Problem starts... how do I extract the data from the array? (the entity has multiple columns but i only want two of them, name and description)
{% extends "FHBingenBundle::layout.html.twig" %}
{% block content %}
<table>
<?php foreach ($liste as $projekt ?>
<tr><p>{{ $projekt->getName() }}</p></tr>
<?php endforeach;?>
</table>
{% endblock %}
thats how I tried to do it but apperently I am not allowed to use $ inside of {{}}? atleast thats what the error says
You should consider reading the cookbook.
Since you are using twig, think about using twig templating system.
{% for item in navigation %} // equivalent to foreach($navigation as $item) {
{{ item.name }} // equivalent to $item->name or $item->getName() or $item->hasName()
{% endfor %} // equivalent to }
EDIT : I don't really remember but it seems that you have to return an array for the twig templating system. return array('projects' => $projects);
I would like to return the field values of a SearchDocument's object. For example, I have generated a SearchResult object using:
class SearchResult(Handler):
def get(self):
index = search.Index("INDEX_NAME")
results = index.search("Brian")
self.render('search-result.html', results = results)
The results object looks something like this:
search.SearchResults(results=[
search.ScoredDocument(
doc_id=u'6122080743456768',
fields=[search.TextField(name=u'full_name', value=u"Brian Jones"),
language=u'en',
rank=106509239L),
search.ScoredDocument(
doc_id=u'4714705859903488',
fields=[search.TextField(name=u'full_name', value=u"Brian Lara"),
language=u'en',
rank=106427057L)],
number_found=2L)
Inside search-result.html, how can I return the values of the fields?
...
<body>
{{ field_values }} #return the field values "Brian Lara" and "Brian Jones"
<body>
...
Start here:
{% for result in results %}
{{ result.fields[0].value }}
{% endfor %}
(You have a '[' mismatch in your fields. Fix that, then check the syntax)
I have a custom theme in Big Cartel and I need to create an if statement using BC's own tags that looks at the page URL and then displays a tag.
For example, if url is homepage show this tag, if it is product page a, show this tag and so on...
Anyone have any idea how I can write that?
At the moment the theme has this:
<title>{{ page.name | remove: '-footer-' | remove: '-hide-' | remove: '-sidebar-' | remove: '-subnav-' | remove: '-f1-' | remove: '-f2-' | remove: '-f3-' }} | {{ store.name }}</title>
There are a number of different conditions you could use for this, like checking the page name, page permalink, the full URL, or others. A full variable reference is available here: http://help.bigcartel.com/customer/portal/articles/772750-variables#page
Here's a couple examples:
{% if page.name == 'Home' %}code here {% endif %}
{% if page.permalink == 'home' %}
code here
{% elsif page.permalink == 'product' %}
more code here
{% endif %}