Symfony2 - Asset function to images - symfony

I know I can use this as an asset function:
<img src="{{ asset('images/logo/logo.png') }}" alt="Logo">
But is there a way to use an asset function using another function? Like this:
<img src="{{ asset('images/logos/{{ dominantItem.generateLogos | lower | replace({' ': '-'}) }}.png') }}" alt="Logos">
Obviously the code above is not working, am I missing something?

You can't nest twig tags like that, but you can use string concatenation to achieve what you want:
<img src="{{ asset('images/logos/' ~ (dominantItem.generateLogos | lower | replace({' ': '-'})) ~ '.png') }}" alt="Logos">

Related

How do I avoid having to use isset() in Laravel Blade templates?

I've got a Blade file like this:
<a href="{{ $url }}" class="{{ $wrapperClass }}">
<p class="{{ $paraClass }}">{{ $title }}</p>
</a>
And call it like this:
<x-mycomponent
url="{{ $book->url }}"
title={{ $book->title }}
wrapper-class=""
para-class = ""
/>
Works great, but whenever I want to add a variable to the component, I need to add this to each file where I call this component.
How can I write it without having to specify all variables and without having to use isset() or {{ $myvar ?? '' }} in Blade? like this:
<x-mycomponent
url="{{ $book->url }}"
title={{ $book->title }}
/>
You can assign default values for your properties like the below's peace of code:
#props([
"product" => null
])
{{ $product }}
and then, you can call your component in both these ways:
<x-mycomponent />
<x-mycomponent product="$product" />
Note: #props is a Blade directive that allows you to pass data from a parent component to a child component.

Use LiipImagineBundle without filters

I'm using LiipImagineBundle in my Symfony2 project and everything is working great. I've defined a thumbnail filter:
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter('thumb_home') }}" alt="Warning" />
And it works perfectly, but some of my images don't need any filter, so I've tried to remove the filter:
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter('') }}" alt="Warning" />
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter() }}" alt="Warning" />
**edited to correct the example as malcolm points out
But it doesn't work, so I need to use Assetic for these images:
{% image '#AppBundle/Resources/public/images/example.jpg' %}
<img src="{{ asset_url }}" alt="Example" />
{% endimage %}
I can't find in the documentation of the bundle any option which allows me to use the Bundle without filters. Is that true? Of course, I can use the LiipImagineBundle way to include images when they need a filter and the Assetic way when they don't, but switching between two different ways of achieving almost the same thing is pretty annoying.
Silly answer to your question - create an original size filter then :). Something like this:
class OriginalSizeFilter implements LoaderInterface
{
public function load(ImageInterface $image, array $options = [])
{
$size = $image->getSize();
$origWidth = $size->getWidth();
$origHeight = $size->getHeight();
$filter = new Thumbnail(new Box($origWidth, $origHeight));
$image = $filter->apply($image);
return $image;
}
}
service:
original_size_filter:
class: ...\OriginalSizeFilter
tags:
- { name: liip_imagine.filter.loader, loader: original_size_filter }
config.yml:
filter_sets:
original:
filters:
original_size_filter: ~
Usage:
<img src="{{ 'bundles/app/images/home/warning.jpg'|imagine_filter('original') }}" alt="Warning" />
But, why not use a asset() function if you don't want any filters to be applied?

How to get the absolute path of a file in a Symfony2 controller?

I want to send an email with an attachement so therefore I need the absolute path to the pdf file.
I tried the flowing code in the controller:
$this->get('templating.helper.assets')->getUrl('MyDoc.pdf', 'doc');
But this returns:
/bundles/MyBundle/doc/MyDoc.pdf?v=1
This is the path without the domain for web access. But I need the real path starting the root of the disk, like example below. How is this possible? Thanks.
/home/MyAccount/public_html/web/src/company/MyBundle/Resources/public/doc/MyDoc.pdf
$this->get('templating.helper.assets') asset helper is about HTTP URI, not directory path.
However, you can easily get the right absolute directory web path like this:
$appPath = $this->container->getParameter('kernel.root_dir');
$webPath = realpath($appPath . '/../web');
$webPath should return something like this:
/home/martins/www/symfony-project/web
A bit of a cheat sheet for anyone else looking for different URL/URI paths for different applications.
Using the Symfony\Component\HttpFoundation\Request component, there are lots of different ways you can call upon this.
Consider this link http://dev.col.com/app_dev.php/my-route?bar=1&foo=bar
$r = $this->getRequest();
$r->getClientIp() 127.0.0.1
$r->getScriptName() /app_dev.php
$r->getPathInfo() /my-route
$r->getBasePath() ''
$r->getBaseUrl() /app_dev.php
$r->getScheme() http
$r->getPort() 80
$r->getHttpHost() dev.col.com
$r->getRequestUri() /app_dev.php/my-route?bar=1&foo=bar
$r->getBaseServerUrl() http://dev.col.com
$r->getUri() http://dev.col.com/app_dev.php/my-route?bar=1&foo=bar
$r->getUriForPath("/other-path") http://dev.col.com/app_dev.php/other-path
$r->getQueryString() bar=1&foo=bar
$r->isSecure() false
$r->getHost() dev.col.com
$r->getMethod() GET
$r->isXmlHttpRequest() false
Or you can do it in Twig as well. For example:
<div>
<a href="{{ job.url }}">
<img src="{{ app.request.scheme ~ '://' ~ app.request.host }}{{ asset(job.webPath) }}" alt="{{ job.company }} logo" />
</a>
</div>
<div>
<a href="{{ job.url }}">
<img src="{{ app.baseServerUrl }}{{ asset(job.webPath) }}"alt="{{ job.company }} logo" />
</a>
</div>
use Symfony\Component\Finder\Finder;
$finder = new Finder();
$finder->files()->in("dir")->name("name.file");
foreach ($finder as $file) {
// Dump the absolute path
var_dump($file->getRealPath());
}
From Docs: http://symfony.com/doc/current/components/finder.html

Symfony 2.7 : asset component not working with imagine_filter

With the introduction of the asset component in Symfony 2.7, how can I output the relative url of an asset without a version number ?
I am using the following code whcih does not work anymore :
<img src="{{ asset('images/user.png') | imagine_filter('default') }}" alt="Image de profil" class="img-circle whitebg">
The asset function outputs an url with a version number and this is not handled properly by the imagine_filter :
http://mywebsite.com/media/cache/resolve/default/images/user.png%3Fversion=v1.0
My config :
framework:
assets:
version: 'v1.0'
version_format: '%%s?version=%%s'
base_path: ~
packages:
images:
base_path: /images
version_format: ''
Ideally I would be able to make the imagine filter work while keeping this versionning strategy
Otherwise, deactivating the versioning for images could be good enough
Thanks for your help !
Apply the filter to the relative path directly, asset() can be seen as a sort of helper:
<img src="{{ asset('user.png'|imagine_filter('default')) }}">
You can also sett the version (4th parameter) to false:
<img src="{{ asset('user.png'|imagine_filter('default'), null, false, false) }}">
You should explicitly point to the package that you want to use for concrete asset:
<img src="{{ asset('user.png', 'images') | imagine_filter('default') }}" alt="Image de profil" class="img-circle whitebg">

Symfony2 Twig custom filter in asset

I'm nearly new in Symfony2 and I have a little question:
I'm developing an email template, which has txt and html parts (no problem with it)
The only 'problem' I have is with the absolute paths of assets in TWIG.
Inside my email.html.twig file I have something like this:
<img src="{{ asset('images/my-image.png') }}" alt="My image" /> but it writes the route with relative path.
I discovered a little solution to add absolute paths, something like this:
{% set abs = app.request.scheme ~ '://' ~ app.request.host %}
<img src="{{ abs ~ asset('images/my-image.png') }}" alt="My image" />
It works! But I want to improve this solution and also learn to create custom filters (I read the documentation, but I got a bit lost)
I want to create something like this:
<img src="{{ asset('images/my-image.png' | absolute) }}" alt="My image" />
But I don't know how to properly override the assetics extension. Can you help me?
Thanks a lot!!
Well, it is a little hard to copy paste the solution but I can make a short cookbook so you can go step by step and do it yourself:
1) You will have to implement Assetic/Filter/FilterInterface
2) If you look at the FilterInterface class, you will see that you have to to implement two methods: filterLoad and filterDump.
So, you will do something like this:
<?php
namespace You\YourBundle\Assetic\Filter;
use Assetic\Asset\AssetInterface;
use Assetic\Filter\FilterInterface;
class YourAsseticFilter implements FilterInterface
{
public function filterLoad(AssetInterface $asset)
{
// do something
}
public function filterDump(AssetInterface $asset)
{
$content = $asset->getContent();
// do something
$asset->setContent($content);
}
}
And after you to this, you have to something very similar to registering twig extensions in services.yml in YourBundle. Sure, it depens if you use YML, XML... configuration. I use yml so I will type it in yml :)
parameters:
your_bundle.class: You\YourBundle\Assetic\Filter\YourAsseticFilter
services:
your_bundle.assetic.your_assetic_filter:
class: %your_bundle.class%
tags:
- { name: assetic.filter }
- { alias: yourChosenNameForYourNewAsseticFilter }
And then you call it like | yourChosenNameForYourNewAsseticFilter, of course.

Resources