简体   繁体   English

用树枝中的资产加载图像

[英]load image with asset in twig

i want to load an image in my twig but it doesnt show anything only show picture alt 我想在树枝上加载图像,但不显示任何内容,仅显示图片alt

this is my list.html.twig that will load image: 这是我的list.html.twig将加载图像:

<img src="{{ asset('images/2.jpg') }}" alt="Symfony" />

and this is my controller : 这是我的控制器:

<?php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class InheritanceController extends Controller
{
    /**
     * @Route("/inherit")
     */
    public function inheritAction(){
        return new Response($this->render('article/list.html.twig'));
    }
}

my image folder is in root directory 我的图像文件夹在根目录中

仅当图像存在于web/images文件夹中时,才可以使用其默认配置的twig中的asset()函数,然后可以通过{{ asset('images/<image_name>') }}来调用任何图像

通过控制台安装资产: php app/console assets:install --symlink

方法$this->render()已经返回一个Response实例,只需将其更改为return $this->render(...)然后重试即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM