symfony 3 - url image from database - php

I wanted to display the url of an image from bdd in my file twig I receive an error of the image like the:
this is my bdd :
and my view from twig :
{% extends '#App/layout.html.twig' %}
{% block title %} Illustration {% endblock %}
{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
<h2> {{ imageFormation.image.nom }}</h2>
<hr>
<img src=" {{ imageFormation.image.url |raw }} "/><br><br>
</div>
{% endblock %}
Can you help me solve my problem please?
i dont know why i cant get the real image.
edit :
/**
* #Route("/formation/{formation}" , name="image")
* #param $image
* #return \Symfony\Component\HttpFoundation\Response
*/
public function imageFormationAction($formation){
$doctrine = $this->container->get('doctrine');
$em = $doctrine->getManager();
$imageRepository = $em->getRepository('AppBundle:Formation');
$imageFormation = $imageRepository->findOneByid($formation);
return $this->render('#App/formation/formationImage.html.twig', array(
'imageFormation' => $imageFormation
));
}

Your image url must be relative to web directory, like this fichier\Mention.jpg, not the image's path in your file system C:\xampp\htdocs\Symfony\MonProjetCv\web\fichier\Mention.jpg.
Save a relative path to your image file in the database (url equal fichier\Mention.jpg), and use asset function in your twig file:
{% extends '#App/layout.html.twig' %}
{% block title %} Illustration {% endblock %}
{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
<h2> {{ imageFormation.image.nom }}</h2>
<hr>
<img src=" {{ asset(imageFormation.image.url); }} "/><br><br>
</div>
{% endblock %}

Can you please try this way?
<img src='{{ asset("IMAGE_DIRECTORY_PATH/"~ image.url)}}' />

Related

Twig : how to check if there's a request ? (looking for symfony 5 : app.request)

I'm trying to check through Twig if there is a request in order to show the results of my searchBar, in Twig, ONLY if there is a request.
I found old symfony docs on app.request, but it doesn't seem to work for my 5.2 Symfony Project (here is my twig template) :
{% if app.request %}
<h3 class="main-header_h3">Résultats de la recherche</h3>
<hr>
<div class="grid">
{% for product in products %}
{% include 'product/_card.html.twig' with {product: product} only %}
{% endfor %}
</div>
<hr>
{%endif %}
Problem here is that my {% if app.request %} isn't being interpreted, so I get my whole product database coming on my view.
If you need any more code, let me know (Controller/Repository...) but the request definitely works, it's just my rendering in Twig that gives me trouble using that if.
Thanks to #bechir, the solution to checking if my URL has a 'search' parameter in it using TWIG is :
{% if app.request.query.get('search', null) %} where search is the
url parameter. (from : bechir)
So this is what it now looks like, and I do have my partial product/_card.html.twig loaded only when there's a URL parameter search:
{% if app.request.query.get('search', null) %}
<h3 class="main-header_h3">Résultats de la recherche :</h3>
<hr>
<div class="grid">
{% for product in products %}
{% include 'product/_card.html.twig' with {product: product} only %}
{% endfor %}
</div>
<hr>
{% endif %}

How to get my gallery to slider in twig file?

I have rendered all my pictures from gallery, but need to get them to slider in my twig file.. is that possible without using any plugin, please???
My code, php:
<?php
$context = Timber::get_context();
$context["post"] = Timber::get_post();
Timber::render( 'pages/single/single-one-collection.twig', $context, CACHE_TIME);
My twig:
{% for picture in post.get_field('oc_gallery') %}
<div class="section" id="section{{ loop.index }}">
<img src="{{ TimberImage(picture).src | resize(400, 266)}}" alt="" />
</div>
{% endfor %}

Twig loop for nested comment in a Symfony-Doctrine blog

I am trying to implement a comment section on a blog with Symfony and Doctrine, using Twig templates.
I am having some trouble trying to implement a response system for my comments. I would like to have something like this:
<div class="comment">
<p>This is comment number 1</p>
<div class="response">
<p>This is a response to comment number 1</p>
<div class="response">
<p>This is a response to response just above</p>
</div>
</div>
</div>
So I would like to have sort of a nested comment system.
I have a Comment entity with a $response property having a OneToOne Relation with another Comment entity:
/**
* #ORM\OneToOne(targetEntity="Comment")
*/
protected $response;
In my controller, I just get my comments like this:
$comments = $this->getDoctrine()
->getManager()
->getRepository('AppBundle:Comment')
->findByArticle($article);
Now I am trying to create the HTML (Twig) but I don't know how to loop throught all comment and associated response so I can create the HTML as I wrote just above...
Anyone who can help me with that?
Thanks.
All you need is recurrence. You have a few options to choose from, one of them is using a macro.
Create twig file with your macro:
{# src/AppBundle/Resources/views/Default/_macro.html.twig #}
{% macro print_comments_recursively(comment) %}
<div class="response">
<p>{{ comment }}</p> {# implement __toString on your Comment class or print appropriate property #}
{% if comment.response is not null %}
{{ _self.print_comments_recursively(comment.response) }}
{% endif %}
</div>
{% endmacro %}
Import macro in your view and use it:
{% import 'AppBundle:Default:_macro.html.twig' as macros %}
<div class="comment">
<p>{{ comment }}</p>
{% if comment.response %}
{{ macros.print_comments_recursively(comment.response) }}
{% endif %}
</div>
Here you have similar problem, already solved, with other solutions: How to render a tree in Twig

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 %}

How to implement rel=prev and rel=next to Symfony2.3 pagination?

I am using Symfony2.3 for my project and for pagination I am using KNP paginator Bundle.
I want to know that How I can implement rel=prev and rel=next in our page ?
My Controller :
<?php
namespace XXX\ABCBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\ORM\EntityManager;
/**
* author Siddharth Singh (siddharth.find#gmail.com)
*/
class ArcController extends Controller {
/**
* #Route("/arch", name="_arch")
* #Template()
*/
public function indexAction() {
$em = $this->getDoctrine()->getEntityManager();
$AArtEntity = $em->getRepository('XXXABCBundle:Arc')->findAll(array('updated'=>'DESC'));
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$AArtEntity, $this->get('request')->query->get('page', 1)/* page number */, 10/* limit per page */
);
return array('article' => $pagination);
}
}
Twig File :-
{% extends 'XXXABCBundle::layout.html.twig' %}
{% block body %}
{% for artic in article %}
<div class="txt">
<p>{{artic.description|striptags|titletruncate(250)}}</p>
</div>
{% endfor %}
<div class="arcive_Paging">
<ul class="ul_paging">
<span class="acitve">{{ knp_pagination_render(article) }}</span>
</ul>
</div>
{% endblock %}
Thanks !
Accepted answer does not work because rel=prev and rel=next must be inside head tag.
I use this macros:
{% macro pagination_meta(pagination) %}
{% set total = pagination.totalItemCount %}
{% set items_per_age = pagination.itemNumberPerPage %}
{% set current = pagination.currentPageNumber %}
{% set last = (total / items_per_age) | round(0, 'ceil') %}
{% set page_parameter = pagination.paginatorOption('pageParameterName') %}
{% if current != 1 %}
<link rel="prev" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params') | merge({ (page_parameter): (current - 1) })) }}" />
{% endif %}
{% if current != last %}
<link rel="next" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params') | merge({ (page_parameter): (current + 1) })) }}" />
{% endif %}
{% endmacro %}
Inside a template usage looks like this:
{% import 'AppBundle::macros.html.twig' as macros %}
{% block head %}
{{ parent() }}
{{ macros.pagination_meta(entities) }}
{% endblock %}
You can do it by overriding default pagination template. Check Templates part of the official documentation:
https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/doc/templates.md
for example:
Tell KnpPaginatorBundle to load your template for rendering pagination, add these lines of code to config.yml:
knp_paginator:
template:
pagination: YourBundle::pagination.html.twig
And copy default pagination template code to your template in src/YourBundle/Resources/views/pagination.html.twig. You can get the code here:
https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/views/Pagination/sliding.html.twig

Categories