twig dynamic variable call - php

I passed data in 3 languages to the twig template and display this data in this way:
{% set lang=app.request.get("lang")%}
{% for item in contests%}
{% if lang=="fa"%}
{{item.titlefa}}
{% elseif lang=="en"%}
{{item.titleen}}
{% elseif lang=="ar"%}
{{item.titlear}}
{% endif%}
{% endfor%}
It is wirking but I must create 3 if condition for each object in "contests"
How can i show data in this logic:
{% set lang=app.request.get("lang")%}
{{item.title~lang}}
{% endfor%}
that can call proper method in contest

You can use the attribute TWIG function for call at runtime a method name, as example:
{% set lang=app.request.get("lang")%}
{% methodname = 'title'~lang %}
{% for item in contests%}
{{ attribute(item, methodname) }}
{% endfor%}
Hope this help

Related

converting a php line to twig [duplicate]

I'm working on a template and I need to check if something is an array. How do I do that in Twig?
I've tried
{% if my_var is iterable %}
{% for v in my_var %}
...
{% endfor %}
{% else %}
{{ my_var }}
{% endif %}
but it always prints my_var, even when my_var is really an array, as evidenced when it prints out
Array
Array
myusername
../data/table.sqlite3
Another way :
{% if my_var.count()>1 %}
If you don't want to create a custom filter use iterable, as per the docs :
iterable checks if a variable is an array or a traversable object
{% if myVar is iterable %} ... {% endif %}
Just add a custom filter:
$twig->addFilter('is_array', new \Twig_Filter_Function('is_array'));
Then use it like this:
{% if my_var|is_array %}

Custom dynamic Twig template that extends Twig_Template

TL;DR: I am currently struggling with the idea how to elegantly generate CRUDs/GRID using Twig.
Long story:
I have a Phalcon app with AngularJs, templating system is Twig.
PHP is suppose to prepare a template for CRUD that contains:
List od entities, table that contains proper headers, columns, row items, action buttons
Simple and advanced search
Mass actions block
Main buttons - Add/Import/Export etc.
AngularJs is responsible for:
Paginating results in 2 modes: Ajax Loaded entities, All entities at once
Changing number of entities per page
Displaying various forms in modals - Add/Edit/Import
Filtering entities - simple and advanced search
Everything is working right now but I am not particularly fond of how it works.
For example a simple entity with few fields needs a following template:
{% extends 'index.twig' %}
{% block containerAttr %}ng-controller="InventoryController as ctrl"{% endblock %}
{% set ctrlName = 'ctrl' %}
{% set columnWidth = 'col-xs-12' %}
{% set tableClass = 'table table-bordered table-striped table-hover' %}
{% block header %}
{% include '#crud/crud/header.twig' %}
{% endblock %}
{% block content %}
{% embed '#crud/crud/main-box.twig' %}
{% block tableHeader %}
{% autoescape false %}
{{ crud.tableHeader('Id', 'id') }}
{{ crud.tableHeader('Nazwa', 'name') }}
<th class="col-xs-1">Actions</th>
{% endautoescape %}
{% endblock %}
{% block tableRow %}
<td ng-bind="e.id"></td>
<td ng-bind="e.name"></td>
{% endblock %}
{% endembed %}
{% embed '#crud/crud/form.twig' %}
{% block modalBody %}
{% autoescape false %}
<div class="row">
{{ crud.input('ctrl.entity.name', 'Name', 12) }}
</div>
{% endautoescape %}
{% endblock %}
{% endembed %}
{% embed '#crud/crud/upload.twig' %}{% endembed %}
{% embed '#crud/crud/advancedSearch.twig' %}{% endembed %}
<script>
window.xdata = {{ xdata | json_encode | raw }};
</script>
{% endblock %}
crud in this template is responsible for generating form fields compatible with AngularJs.
Idea is: To create new CRUDs/Grids with the least amount of work required. While still giving a room for some flexibility.
Right now to make a CRUD/Grid inside my application there is a really little work required:
PHP Controller needs to contain only one line:
class SubCategoryController extends CrudBase
{
protected $entityClass = InventoryCategory::class;
}
Thanks to extending it is really easy to customize every part of it.
Model needs to have just a few fields:
class InventoryCategory extends ModelBase
{
/** #var integer */
public $id;
/** #var string */
public $name;
public function initialize()
{
$this->setSource('inventory_subcategory');
$this::setup(['castOnHydrate' => true]);
}
}
Even the AngularJs controller contains only URLs and one line:
class InventoryCategoryController extends CrudBase {
constructor($injector, $scope) {
super($injector, $scope);
this.urlSave = '/inventory/category/save';
this.urlImport = '/inventory/category/import';
this.urlExport = '/inventory/category/export';
this.urlDelete = '/inventory/category/delete';
this.init({
advancedSearch: false
});
}
}
angular.module('App').controller('InventoryCategoryController', InventoryCategoryController);
Yes I use ES6, application is not compatible with older browsers anyways and it is admin panel so I do not care about older browsers.
But now only templates remains a tedious work to copy it over and change it. Not to mention they do look pretty ugly.
So my idea was to use Annotation Reader (Phalcon has one) to read fields from Model, then transform it into a base configuration, check if there is custom configuration in json and merge it. Then based on configuration, generate this whole template on the fly.
So the grand question is:
I was wondering if I can create some class that extends Twig_Template to generate this template on the fly?
I have seen Twig cache and it should be pretty easy but then how can i use it? How can i render it? Will extend still work?

Symfony3 Variable does not exist in including template

I get the following error: Variable "file" does not exist.
The included template Contractor:file.html.twig looks like this:
{{ dump(file) }}
The main template form.html.twig looks like this:
{% extends '::base.html.twig' %}
{% form_theme form 'NaSoftEdmBundle:Form:fields.html.twig' %}
...
{% block files %}
{% for file in form.files %}
{{ include('NaSoftEdmBundle:Contractor:file.html.twig', {'file': file}) }}
{% endfor %}
{% endblock %}
Controller:
public function editAction(Request $request, Contractor $contractor)
{
...
return $this->render('NaSoftEdmBundle:Contractor:form.html.twig', array(
'form' => $form->createView(),
));
}
But when I try to display a variable inside the main template file (form.html.twig), the variable becomes available:
{% for file in form.files %}
{{ dump(file) }} {# it work! #}
{% endfor %}
ContractorFile {#499 ▼
-id: 154
-uploadedFile: null
-name: "57c6d217d9a92.jpg"
-origName: "1471590585502.jpg"
-path: "/contractor/docs/files/2/335"
-size: 153536
-mimeType: "image/jpeg"
-updateDate: DateTime {#496 ▶}
-contractor: Contractor {#370 ▶}
}
command php app/console cache:clear did not help
The problem was that I was before starting the cycle "for" included the same template for attribute data-prototype:
data-prototype="{% filter escape %}
{% include 'NaSoftEdmBundle: Contractor: file.html.twig' with { '_form':form.files.vars.prototype} %}
{% endfilter %}"
I've created for the attribute "data-prototype" new template and it worked.
data-prototype="{% filter escape %}
{% include 'NaSoftEdmBundle: Contractor: filePrototype.html.twig' with { 'file':_form.files.vars.prototype} %}
{% endfilter %}"
This has been implemented to customize the template collection
The correct way to pass variables to a Twig include command is this:
{% include 'NaSoftEdmBundle:Contractor:file.html.twig' with {'file': file} %}
As #walther pointed out in comments, it is not actually required to pass variables since an included template get access to variables of the active context, i.e. you can simply write:
{% include 'NaSoftEdmBundle:Contractor:file.html.twig' %}
and the file variable will be used.
You still have to use the with syntax if you want to assign a new variable, e.g.
{% include 'NaSoftEdmBundle:Contractor:files.html.twig' with {'files': form.files} %}
See http://twig.sensiolabs.org/doc/tags/include.html

Multidimensional Array on TWIG loop

I'm trying to save some values into a multiarray on Twig, but for some reason, it's not working.
I want to save in an array a list of users and some values of them. I am using merge function to create the multi array.
{% for result in results %}
{% set users = users|merge({ ('name'~loop.index):result.name,('age'~loop.index):result.age,('credits'~loop.index):result.credits}) %}
{% endfor %}
Try this out :
{% set users = [] %}
{% for result in results %}
{% set users = users|merge([{ 'name' : result.name, 'age' : result.age, 'credits' : result.credits }]) %}
{% endfor %}

Loop till the value of variable is not zero

I have been stuck at this for a while now. I am new to twig and i am trying to iterate a code untill my variable becomes zero. I have tried this:
{% set total = 5%}
{% set i=1 %}
{% for total %}
{{i}}
{%set i=i+1%}
{% set total = total -1%}
{% endfor %}
and this
{% set i=1 %}
{% for total > 1%}
{{i}}
{%set i=i+1%}
{% set total = total - 1%}
{% endfor %}
but none are working.. What am i doing wrong?
Twig fors are more akin to PHP's foreachs (they are for iterating over a traversable). To achieve what you are describing you would do:
{% set nums = range(1, 5) %}
{% for num in nums|reverse %}
{{ num }}
{% endfor %}
In practice, you could set nums from your controller logic. Also note from the Twig manual:
Unlike in PHP, it's not possible to break or continue in a loop.
You can, however, skip elements with if. Manual example:
{% for user in users if user.active %}

Categories