symfony2 assign icon to twig variable - variables

Using Symfony2.3.4 with Twig.
I need to assign an icon to a twig variable and then print the variable, can this be done at all??
for example:
{% set var = '<i class="glyphicon-user"></i>' %}
this obviously doesn't work, it's just to make my idea clear, it prints this text:
<i class="glyphicon-user"></i>
I need to print the icon itself.
Tips appreciated, thanks

Your method will work you just need to call the raw filter with it:
{% set var = '<i class="glyphicon-user"></i>' %}
{{ var|raw }}
Without raw twig will escape html entities

Related

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

Assign a liquid object to a variable in Shopify

In Shopify I am declaring a variable like this:
{% assign favourites = hello %}
Instead of the variable being `hello, I want to use a metafield from my product. I can get the metafield like this:
{{ product.metafields.global["other_options"] }}
I can't, however, merge the two together like this
{% assign favourites = {{ product.metafields.global["other_options"] }} %}
I have tried wrapping the liquid object in single and double quotation marks but this doesn't work.
Is it possible to do this?
Remove the internal braces :)
{% assign favourites = product.metafields.global["other_options"] %}

How to define global variables in Liquid?

Right now it seems variables I create can't communicate across files.
For Liquid you can pass a variable in the include
{%- assign global_var = "VALUE" -%}
{%- include 'YOUR_FILE' global_var: global_var -%}
For Shopify liquid you can do the following:
There is a work around this, you can set the global variable in the theme settings as an option config/settings_schema.json
{
"type": "text",
"id": "global_variable",
"label": "global variable",
"default": "Variable value"
},
and you can access it in the liquid files through
settings.global_variable
But the value is depending on what you enter in the theme settings.
If you need more dynamic way, you can set cart attributes through ajax like:
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: { attributes: {'global_variable': "MY_VALUE"} },
dataType: 'json',
success: function(cart) {
location.reload();
}
});
And then access it any where in the theme through
cart.attributes.global_variable
But you have to update it each time the cart is empty
It seems the templates are loaded before the theme, so variables set in your layout/theme file wont be present in templates. Frustrating. However you can set them via a snippet, and include this snippet in your templates, layout, etc
In Liquid, as you want for example to determine the language and reuse this code in multiple occasions, you can create an file in the render folder.
instead of calling this using render, use include. Then it returns the assigned value. For example, create "render/current_language.liquid" :
{%- liquid
assign current_language = 'en'
if request.path contains '/nl/'
assign current_language = 'nl'
endif
%}
In a other file you can use as follows:
{%- liquid
include 'get-language'
if current_language == 'en'
else
endif
%}
As long as you use the
{% assign variable = value %}
you should be able to get the value anywhere in the file, and any file included after it has been assigned.
I believe this is the closest you can get to global variables in it.

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');

Django: user info in template

I have this in my base.html template:
{% if user.is_authenticated %}
<p style="color:#000;">
Welcome
{{ user.first_name }}
|
Logout
</p>
{% endif %}
After I authenticate, and go to this page, my first name does not show up. Can anybody tell me why?
The "Welcome" also does not show up. So, this must be failing on user.is_authenticated.
Thanks. :)
The django.contrib.auth.context_processors.auth middleware is responsible for setting request.user before a request reaches the controller.
In order to access it from a template, you need to pass this variable or a RequestContext. Example:
def something(request):
context_instance = RequestContext(request)
template_name = 'your_template.html'
extra_context = { 'other_variable': 'some value' }
return render_to_response(template_name, extra_context, context_instance)
This way, all the request variables are accessible from the template.