I need to run a google images scraper depending on one of the twig variables. How can i execute the following code inside my .twig file.
{$google_image = new get_google_image_class;
$google_image->destination = '.'; // directory to save thse images
$google_image->limit = 1; // limit the number of images, 0 for random
$google_image->display = true; // output images to the page
$google_image->GetImage($manufacturer);
echo '<p>Retrieved images: '.$google_image->retrieved.'</p>';
}
I need to run it with the following variable {{ manufacturer }} :
<img alt="{{ manufacturer }}" src= "www.antonio.ecommercefull.com/catalog/controller/product/{{ manufacturer }}-1.jpg">
You can try:
{% set google_image = new get_google_image_class %}
{% $google_image.destination = '.' %}
{% $google_image.limit = 1 %}
{% $google_image.display = true %}
{% $google_image.GetImage($manufacturer) %}
<p>Retrieved images: {{ $google_image.retrieved }} </p>
But I think you should write code PHP in file Controller. And using:
<p>Retrieved images: {{ $google_image.retrieved }} </p>
Related
I'm using OctoberCMS which uses Twig.
The way I retrieve user permissions is by getting the information in php then pass a twig variable.
Is there a way to do this using just Twig?
Permissions Docs
PHP
if (BackendAuth::check()) {
# Get User Info
$backend_user = BackendAuth::getUser();
# Superuser
$this['is_superuser'] = $backend_user->is_superuser;
# Permissions
$this['permissions_allow_edit'] = $backend_user->hasPermission(
[
'mycomponent.gallery.allow_edit'
]);
}
Twig
{% if is_superuser or permissions_allow_edit %}
//edit button
{% endif %}
Twig (Standalone)
//another way to backendauth check in twig?
if (BackendAuth::check()) {
$this['backend_user'] = BackendAuth::getUser();
}
//////
{% if backend_user.is_superuser or backend_user.permissions.mycomponent.gallery.allow_edit %}
//edit button
{% endif %}
{{ backend_user }} gives the user information array
{"id":2,"first_name":"Matt","last_name":"","login":"matt","email":"user#example.com","permissions":{"mycomponent.gallery.allow_edit":1},"is_activated":false,"activated_at":null,"last_login":"2017-03-11 09:59:48","created_at":"2017-02-11 10:32:14","updated_at":"2017-03-11 10:03:45","is_superuser":0}
{{ backend_user.permissions }} throws an "Array to string conversion" error.
{{ backend_user.permissions.mycomponent.gallery.allow_edit }} shows nothing.
How do I get the values inside the permissions array?
I have not tested this but you should be able to use the hasPermission method to check if backend user has a specific permission:
{% if backend_user.is_superuser or backend_user.hasPermission('mycomponent.gallery.allow_edit') %}
//edit button
{% endif %}
I am trying to add pagination to my current project. I am pretty new to Symfony so I am not sure if there is something out there that can help me build such. My current code looks like this:
Controller class:
class MovieDisplayController extends Controller
{
public function showAction()
{
$movies = $this->getDoctrine()->getEntityManager()->getRepository('AppBundle:Movie')->FindAll();
return $this->render('movies/index.html.twig', array(
'movies' => $movies
));
}
}
Twig template:
{% block body %}
{% if movies|length == 0 %}
There are no movie items available. Add a movie here to get started.
{% elseif movies|length != 0 %}
These are the results: <br />
<ul>
{% for x in movies %}
<li>Title: {{ x.title }} - Price: {{ x.price }} - Edit - Details - Delete</li>
{% endfor %}
</ul>
Add more movie entries
{% endif %}
{% endblock %}
This will return all results within the database. I would like to only show 5 results (rows per page) and add paging buttons below the list and I wonder how/if this is possible?
The findAll() function will not work if you want to set limit.
You can try KnpPaginatorBundle to add pagination in symfony. It will work with fine to add pagination.
https://github.com/KnpLabs/KnpPaginatorBundle
Hi how use this part of code with twig,
$this->assign('title', 'Home');
not
echo $this->assign('title', 'Home');
I tried,
{% set assign = ('title', 'Home') %}
{% set this.assign = ('title', 'Home') %}
{% set assign = {'title', 'Home'} %}
{{ assign('title', 'Home') }}
But still don't work
Thanks you
I don't know about using $this in the context of a template (it would refer to some generated class instance), but you can perform arbitrary operations without printing by using the do statement.
The do tag works exactly like the regular variable expression ({{ ... }}) just that it doesn't print anything:
{% do 1 + 2 %}
To access the view itself when using TwigView, use the _view variable:
{% do _view.assign('title', 'Home') %}
I have the folowing long variable in Twig to read the src attribute of an image in RSS feed:
<img src="{{item.get_item_tags("http://www.w3.org/2005/Atom","link")[0]['child']['http://search.yahoo.com/mrss/']['content'][0]['child']['http://search.yahoo.com/mrss/']['thumbnail'][0]['child']['http://www.w3.org/2005/Atom']['img'][0]['attribs']['']['src']}}"/>
I want to make the code more cleaner and readable, so initially, I defined two variables to act as parameter of get_item_tags() and the other as a path for the array:
{% set param1 = 'http://www.w3.org/2005/Atom' %}
{% set arrayPath = '[0]['child']['http://search.yahoo.com/mrss/']['content'][0]['child']['http://search.yahoo.com/mrss/']['thumbnail'][0]['child']['http://www.w3.org/2005/Atom']['img'][0]['attribs']['']['src']' %}
What I want to get, but it generate error, is:
<img src="{{item.get_item_tags({{param1}}, "link"){{arrayPath}}}}" />
I don't use symfony and Twig version is 1.16.0
When you set a variable in twig and want to use it inside another function either in {{ }} or {% %} you do not need to use {{ }} again for the set variable.
Furthermore, you cannot set a variable as index of another variable then concat them; so you need to change it to:
{% set param1 = 'http://www.w3.org/2005/Atom' %}
{% set output = item.get_item_tags(param1, "link") %}
{% set yk = 'http://search.yahoo.com/mrss/' %}
<img src="{{ output[0]['child'][(yk)]['content'][0]['child'][(yk)]['thumbnail'][0]['child']['http://www.w3.org/2005/Atom']['img'][0]['attribs']['']['src'] }}" />
The following entities exist, Farm, Barn and Animals. A Farm can have many Barns and a Barn many Animals.
When displaying a Farm in a TWIG template the number of Animals should be shown as well.
What is the best way to do this?
I have create a TWIG extension which allows me to easily show the number of barns.
public function totalFieldFilter($data, $getField='getTotal') {
$total = count($data->$getField());
return $total;
}
In my template I would use {{ farm|totalField('getBarns') }}, I could easily extend this to write another custom function like so:
public function totalFieldFilter($farm) {
$total = 0;
foreach($farm->getBarns() AS $barn) {
$total += count($barn->getAniamls());
}
return $total;
}
Although this would work, is there a better way and can it be made more generic? What if I wanted to count Legs on Animals? Or how many Doors a Barn has, I would have to write a custom TWIG extension each time.
Use Entity accessors :
{% for farm in farms %}
{{ farm.name }}
{% set barns = farm.getBarns() %}
Barns count = {{ barns|length }}
{% for barn in barns %}
{% set animals = barn.getAnimals() %}
{{ barn.name }} animals count : {{ animals|length }}
{% endfor %}
{% endfor %}
You are looking for the length filter
When used with an array, length will give you the number of items. So, if your array is farm.barns, you can just use {{ farm.barns|length }}