How to put liquid code inside a php string - php

I want to put Liquid code in a metafield with value type string. This is the Liquid code:
{% unless shop.metafields.cmld == blank %}
{%- assign cmld = shop.metafields.cmld -%}
<div class="slider">
{%- for field in cmld -%}
<div>
<img src="{{ field | last }}" />
</div>
{% endfor %}
</div>
{% endunless %}
The Liquid code works fine when I try it on shopify.
The problem here is this error on the 1st row:
{%'(T_CONSTANT_ENCAPSED_STRING), expecting ')' in .....
Any suggestions? It looks like this:
$add_metafield= array(
"metafield" => array(
"namespace"=> $metafield_namespace,
"key"=> "something",
"value" => "{% unless shop.metafields.".$metafield_namespace." == blank %}{%- assign ".$metafield_namespace." = shop.metafields.".$metafield_namespace." -%}<div class="slider">{%- for field in ".$metafield_namespace." -%}<div><img src="{{ field | last }}" /></div>{% endfor %}</div>{% endunless %}",
"value_type" => "string"
)
);

Change from double to single quotes in <div class="slider"> and <img src="{{ field | last }}" />
changing it to -> <div class='slider'> and <img src='{{ field | last }}' />

Related

Twig - print array without empty values

I have array constructed like this
add_to_context('custom', [
[
'title' => 'My title',
'link' => 'My link'
],
[
'title' => 'My title 1',
'link' => 'My link 1'
]
]);
and in view I have simple loop
{% for item in custom %}
<li>
<h1>{{ item.title }}
<img src="{{ item.link|e }}" target="_blank">
</li>
{% endfor %}
And everything works fine. But I want to print elements which have both keys with value. For example, if I have
[
'title' => '',
'link' => 'mylink'
]
I don't want to print this. If link will be empty I don't want it too. If both are empty - the same. I want to print it only if both keys have values. So, how can I do this?
You could do something like this, maybe.
Twig even has a little built in functionality for this:
<ul>
{% for item in custom if item.title and item.link %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
I haven't tested it, but I assume the and in the if statement should work.
You can just add a simple test in your template :
{% for item in custom %}
{% if item.title|length %}
<li>
<h1>{{ item.title}}
<img src="{{ item.link|e }}" target="_blank">
</li>
{% endif %}
{% endfor %}
Generally speaking , "0"|trim expression will evaluates to false.

Special character issue in twig

I have try following code for custom block. it is working fine when I does add content from admin. but, just issue for special character.
admin\view\template\extension\module\theme_module.twig
<div class="tab-content">
{% for language in languages %}
<div id="tab8-language-{{ language.language_id }}" class="tab-pane">
<div class="form-group">
<div class="col-sm-10">
<textarea name="custom_block[{{ language.language_id }}][description]" data-toggle="summernote" data-lang="{{ summernote }}" class="form-control" id="input-description8{{ language.language_id }}">{{ custom_block[language.language_id].description ? custom_block[language.language_id].description }}</textarea>
</div>
</div>
</div>
{% endfor %}
</div>
admin\controller\extension\module\theme_module.php
if (isset($this->request->post['custom_block'])) {
$data['custom_block'] = $this->request->post['custom_block'];
} else {
$data['custom_block'] = $this->config->get('custom_block');
}
catalog\controller\common\header.php
$data['config_language_id'] = $this->config->get('config_language_id');
$data['custom_block'] = $this->config->get('custom_block');
catalog\view\theme\default\template\common\header.twig
{% set lang = config_language_id %}
{% if custom_block[lang]['description'] %}
{{ custom_block[lang]['description'] | convert_encoding('UTF-8', 'HTML-ENTITIES') }}
{% endif %}
when I does add content something like from admin: ľščťžýáíé
So, does Output: ľšÄťžýáíé
The correct way is to do this in the controller file.
For example in:
catalog\controller\product\category.php
Create your variable:
$data['my_var'] = html_entity_decode($data['my_var'][$this->config->get(‌​'config_language_id'‌​)]['description'], ENT_QUOTES, 'UTF-8');
And in catalog\view\theme\default\template\product\category.twig, echo it:
{{ my_var }}
Output:
ľščťžýáíé
I think you should do something like this
{{custom_block.lang.description | convert_encoding('UTF-8', 'HTML-ENTITIES')}}
You can also try
#Digicart suggestion

Twig Comparison Operator not behaving as it should

Am trying to add a certain class to an anchor element if a given condition is met using symfony's twig templating engine, The following piece of code is being used in an attempt to achieve this:
{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
<a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ (colorData[keys] is defined and colorData[keys]['code'] == c.hexcode) ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}
The above code adds the selected class rightfully, to just the first anchor element even when I expect to have 3 anchor elements assigned this class:
{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
<a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ (colorData[keys] is defined and colorData[keys]['code'] in colors | keys) ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}
The second code fragment adds the selected class to 3 anchor elements because however you wish to look at it, colorData[keys]['code']has keys that exist in the colors array even if the class isn't being added to the right anchor elements. My question is this; if the comparison operator (==) returns true for matched variable values why isn't the first code fragment working? and why is the second code fragment adding this class to the wrong anchor elements?
A snapshot of the colorData array is shown below:
That of the colors array is thus:
About the first example you're comparing:
"#FFFFFF" == "#FFFFFF"
"#222222" == "#795548"
"#01579b" == "#3e2723"
and after this comparsion it finishes 3 times in condition colorData[keys] is defined. What you can do here is to add one more loop.
{% if colors is defined and colors is not empty %}
{% for keys, c in colors %}
<li>
{% set isColorInColorData = false %}
{% for exactColor in colorData %}
{% if exactColor.code == c.hexcode %}
{% set isColorInColorData = true %}
{% endif %}
{% endfor %}
<a id="{{ keys }}" data-rel="tooltip" data-placement="top" title="{{ c.color|capitalize }}" class="picker-btn{{ isColorInColorData ? ' selected':'' }}" style="background: {{ c.hexcode }}" data-color-id="{{ c.id }}" data-color-text="{{ c.color }}" data-color-code="{{ c.hexcode }}"></a>
</li>
{% endfor %}
{% endif %}
Looking to the second example you're comparing a string with an integer.
"#FFFFFF" in [0,1,2]
"#222222" in [0,1,2]
"#01579b" in [0,1,2]
Note that "#FFFFFF" is "equal"(==) to 0 in php!!
So you have to really concentrate on what are you comparing. In twig there is no === operator.
With the first code, you're saying
colorData[keys] is defined and colorData[keys]['code'] == c.hexcode
As you loop over an array of 3 items, keys is going to be 0, 1, 2 (or maybe 1, 2, 3 given it's Twig). So you're saying
colorData[0]['code'] == c.hexcode
i.e. comparing colorData[0] with colors[0], so does #FFFFFF == #FFFFFF, which it does.
Second iteration, you're then comparing colorData[1] with colors[1], so does #795548 == #222222, and so on.

How to say "if this translation is empty"

I want to have this image not showing if the translation is empty or non existing
<img class="partners-logo" src="{{ 'page.image.path' | trans | raw }}">
So can I wrap a logic loop around it as in the following code?
{% if {{ 'page.image.path' | trans }} is not null %}
<img class="partners-logo" src="{{ 'page.image.path' | trans | raw }}">
{% endif %}
Obviously not right? Then how should it be?
You can do something like that :
{% if "page.image.path"|trans != "page.image.path" %}
This will check if the result of the translation is different from the translation key : if a translation key has no translation, filter trans returns the translation key.

how to display blob image stored in mysql database in symfony 2.7.3

I am having trouble displaying blob image on my web site, am using symfony 2.7.3...
i tried to encode the blob data in base 64, am able to display the base 64 value but when i embed it in nothing happens bellow is my code.
pls what am in doing wrong ??
controller
$badges = $this->getDoctrine()
->getRepository('AppBundle:News')->findAll();
$images = array();
foreach ($badges as $key => $badge) {
$images[$key] = base64_encode(stream_get_contents($badge->getImage()));
}
return $this->render('default/index.html.twig', array(
'badges' => $badges,
'images' => $images,
));
}
view
{% if badges %}
{% block badge %}
{% for key,badge in badges %}
<div style = "margin:4px;height:100px" class="thumbnail">
<div class="pull-left">
<a href=""><img alt="embeded image"src="data:image/png;base64,{{ images[key] }}" />
</div>
<div style="width:auto" class = "pull-right" >
{{ badge.title }}
</div>
</a>
</div>
{% endfor %}
{% endblock %}
{% endif %}

Categories