phalcon extends volt with variable, Any other way to make that work - phalcon

{% extends "../../../views/"{{variable}}"/templates/one-column.volt" %}
or
{% extends "../../../views/"~variable~"/templates/one-column.volt" %}
or any other way to make that work.
how to make it work like above. anyone can help me?
framework phalcon

If you are using variable inside the extend or include you have to omit the template extension. In your case it is .volt.
Try like this:
{% extends "../../../views/"~ variable ~"/templates/one-column" %}
Another example:
// When using variable inside the include
{% set folderVar = 'partials' %}
{% include "_layouts/"~ folderVar ~"/header" %}
// Without using variable
{% include "_layouts/partials/header.phtml" %}
Explanation of this behaviour: https://docs.phalconphp.com/en/latest/reference/volt.html#include

Related

Check query field value in Django template

I am passing a query to the context of a normal Django template. Within the template I want to do this:
{% for item in sample %}
{% if item.status != "Close" %}
Show something
{% endif %}
{% endfor %}
However, the if condition is never evaluated to True.
I can perfectly access the field for each item in the template like {{item.status}} but the if part is not working.
Is there a special way to write the if statement so the template expands item.status to its value before evaluating the comparison?

how to define variable in phalcon php volt blade and assign data to it?

i have a variable in my blade that its set to the blade in controller
$this->view->->setVar("formData", $formData);
my formData is an array from my submitted form
now i want to define a new variable in? my volt blade and assign my formData to it.
how should i do that?
i read phalcon(https://docs.phalcon.io/3.4/en/volt) volt document but i cant find how should i do that.
In your controller setVar() and setVars() can be used on the view object to set your variables and then use them in your Volt file:
$this->view->setVar('myData', $data);
and in the template
{{ myData }}
If $data is an array and you want elements from it:
{{ myData['element1'] }}
If $data is an object you can call methods on it
{{ myData.myMethod() }}
If you want to perform comparisons and assign variables in the template:
{% if myData['element'] == 'yes' %}
{% assign reply = true %}
{% else %}
{% assign reply = false %}
{% endif %}
References:
https://docs.phalcon.io/4.0/en/volt#variables
https://docs.phalcon.io/4.0/en/volt#assignments
https://docs.phalcon.io/4.0/en/volt#if

how to parse this python code django template language

for com in applications:
for number in range(len(appliers)):
if connector[number] == com.id:
print(appliers[number].name)
I am having problem with parsing this python code to django template language... keep getting error saying that it can't parse. please can anyone parse it for me...
You can create a filter for getting a range in template:
#register.filter(name='times')
def times(number):
return range(number)
And then in template you can do:
{% for number in appliers|length|times %}
{% if connector.number == commission.id %}
{{appliers.number.name}}
{% endif %}
{% endfor %}
Alternatively you can pass range(len(appliers)) as a context variable to your template from a view.

check variable type inside Jinja2 in Flask

The template file i created contains this:
{% if type({'a':1,'b':2}) is dict %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
Then Jinja2 responds by saying:
TemplateAssertionError: no test named 'dict'
I am completely new to Jinja2 and Flask
You are looking for the mapping test:
{% if {'a': 1, 'b': 2} is mapping %}
"Oh Yes!"
{% else %}
"Oh No!"
{% endif %}
Jinja is not Python though, so you don't have access to all the builtins (type and print do not exist, for example, unless you add them to the context. In Flask, you do this with the context_processor decorator).
You don't actually need print at all. By default everything is output (unless you are in a child template that extends a parent, in which case you can do interesting things like the NULL Master fallback because only blocks with names available in the master template are output).
How about:
{% if {'a':1,'b':2} is mapping %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
see List of Builtin Tests for reference.
In case you want to get a custom type you can access field name like in this example:
{% if 'RelationField' in field.__class__.__name__ %}
<div class="col-md-1">
Manage object
</div>
{% endif %}

How can I access template variable in TWIG macro?

I'm not able to access template variable in TWIG macro.
Here is a simplified example:
{% set myname = "Ligio" %}
{{ _self.pagedurl(1) }}
{% macro pagedurl(page) %}
Hi {{ _self.myname }}! This is Page Num {{ page }}
{% endmacro %}
How can I access the variable myname without passing it to the macro?
You can not.
As stated in the documentation:
As PHP functions, macros don't have access to the current template
variables.
Your only solution is to pass the parameter to the macro:
{% import _self as flow %}
{{ flow.pagedurl(1, "Ligio") }}
{% macro pagedurl(page, myname) %}
Hi {{ myname }}! This is Page Num {{ page }}
{% endmacro %}
IMPORTANT NOTE:
You may have noticed in my example, I call {% import _self as flow %}.
This is something you MUST do:
When you define a macro in the template where you are going to use it,
you might be tempted to call the macro directly via _self.input()
instead of importing it; even if seems to work, this is just a
side-effect of the current implementation and it won't work anymore in
Twig 2.x.
http://twig.sensiolabs.org/doc/tags/macro.html
If you need to pass more than one global variable into the macro, you might find the _context variable useful:
{% macro mymacro(globalvars) %}
Value of the global variable pi is {{ globalvars.pi }}
{% endmacro %}
{% set pi = 3.14159 %}
{{ _self.mymacro(_context) }}
Ref: this or this answer.
You can set a global variable and access it anywhere in the template
$loader = new \Twig_Loader_Filesystem('path/to/templates');
$twig = new \Twig_Environment($loader);
$twig->addGlobal('V_Name', 'V_Value');