How to find a rendered file in Symfony2 - symfony

i got the job to edit a page. And now i can see the rendered and displayed page, but i canĀ“t find the template to work on it.
The controller which is rendering the page:
return $this->render('FEBundle:Public\Impressum:Impressum.html.twig',array());
So how do i get the path from a rendered file?

Probably in FEBundle/Resources/views/Public/Impressum/Impressum.html.twig

Starting from Symfony 2.7, you can use the Twig profiler: http://symfony.com/blog/new-in-symfony-2-7-twig-profiler

Referencing Templates in a Bundle
Referencing Templates in a Bundle - overriding-bundle-templates
templates should be in:
app/Resources/FEBundle/views/Public/Impressum/Impressum.html.twig
or in:
FEBundle/Resources/views/Public/Impressum/Impressum.html.twig
EDIT:
if you want server path from controller:
$parser = $this->container->get('templating.name_parser');
$locator = $this->container->get('templating.locator');
$path = $locator->locate($parser->parse('FEBundle:Public\Impressum:Impressum.html.twig'));

Related

Displaying an image uploaded outside of the public directory in a Symfony API

I am working with Symfony as an API and a React frontend divided in two separate projects.
I store user uploaded images inside a medias/ folder at the root of the Symfony project because some images are confidential (this is a business app) so they can't be simply put inside the public/ folder.
The upload part is fine but I can't figure out how to display the file in the React app even though I get the full link from the API using VichUploader's $this->storage->resolveUri(...) as stated in the API-Platform documentation.
I understand why it doesn't work, as it gives me something like www.domain.com/medias/images/123.png while the medias/ folder sits outside of the public/ directory.
I have no idea how to display those files. Every single documentation (Symfony, Vich...), guides and forum posts I've found are about displaying those images inside Twig or a webpacked javascript that is inside the Symfony project, while my javascript is in another different project.
Can anybody give me some pointers as to where to look for a solution?
As a reference, this is my VichUploader's configuration file:
vich_uploader:
db_driver: orm
mappings:
machine_image:
uri_prefix: /medias/images/machine
upload_destination: '%kernel.project_dir%/medias/images/machine'
namer: Vich\UploaderBundle\Naming\OrignameNamer
You'll have to create a controller method that gets the image from the filesystem and create a response with that image.
In that method you can also add some checks if the user is allowed to see the image, but that depends on your situation. In some cases the image url isn't 'guessable' and that might be enough security.
Something like this:
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
final class ImageController extends Controller
{
#[Route(path: 'image/{img}', name: 'image', methods: ['GET'])]
public function image(string $img) : Response
{
// .. add some security checks here
$filepath = $this->storage->resolveUri($img);
$response = new Response(file_get_contents($filepath));
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $img);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'image/png');
return $response;
}
}
(not tested, feel free to ask questions if it's not working)
While I'm not a big fan of 'there's a library/app/bundle for this'-answers, I'd like to recommend LiipImagineBundle if you want to add some other features. You can create a thumbnail, add a watermark, add a webp support, etc.

Custom Index Swashbuckle/Swagger

this is my first time using Swagger, I'm currently using Swashbuckle.AspNetCore v2.4.0 and trying to implement a custom index page. In this version they have removed the possibility of injecting javascript, and I'm essentially stuck with the only option of making a custom index.html page just to edit some links (most specifically the navigation icon link). I'm trying to get my custom index page to work but I'm having trouble getting it to load.
app.UseSwaggerUI(c =>
{
c.IndexStream = () => GetType().GetTypeInfo().Assembly
.GetManifestResourceStream("customSwaggerIndex.html"); // requires file to be added as an embedded resource
});
I have the following in my code with the customSwaggerIndex.html saved on the wwwroot folder and its Build Action set as Embedded resource, but I keep running into the error System.ArgumentNullException: Value cannot be null. This is most likely due to me not properly writing the GetManifestResourceStream("string"). Any help?
Figured out why it wasn't working. First thing is that it needs the name of the solution, and instead of \ its .
So for example a file located in my wwwroot folder would look like this.
c.IndexStream = () => GetType().GetTypeInfo().Assembly
.GetManifestResourceStream("MySolution.wwwroot.customSwaggerIndex.html");

How do I route a static page in sylius

I am having tourble creating and routing static pages from within the Sylius CMS.
I have had this working once but now just can not get it to work at all. Creating the content for the page works fine but when I try to save a route all I get is errors.
An exception has been thrown during the rendering of a template
("Parameter "id" for route "sylius_backend_static_content_update" must
match ".+" ("" given) to generate a corresponding URL.") in
SyliusWebBundle:Backend/Content/StaticContent:update.html.twig at line
20.
Image of the page I am trying to save
Any help would be appreciated as there doesn't seem to be any documentation on this part of sylius.
May be you can have a look into the link below. I also had some problems with the routes while creating the pages. I had solved it by adding some info into the database. Hope this link helps you.
https://github.com/Sylius/Sylius-Standard/issues/78
Happy Syliusing ^^

Meteor+Blade template variables catch 22

I'm trying to use a variable in my Blade template, but I always get
ReferenceError: files is not defined
My understanding is that the proper way to pass a variable to a template is something like this (client/ceres.js):
Meteor.startup(function() {
Files = new Meteor.Collection('files');
Template['files'].files = function() {
return Files.find();
}
});
(Copying from the "todos" example)
And then I should be able to use it in my template, views/files.blade:
ul
foreach files as file
li= file.filename
But I guess the variable is passed to the template too late? But if I take my JS out of Meteor.js then Template isn't defined.
So I don't get it. Either my template doesn't exist, or the variable doesn't exist, and it always crashes. How do I pass a simple variable along?
Same error with this:
ul
- for(var i=0; i<files.length; ++i)
li= files[i].filename
This is a known issue with Meteor that is actively being worked on.
The problem is that Meteor prevents smart packages from specifying the load order of files. See issue here.
Because of this issue, it is possible that your client-side JavaScript will run before the templates are loaded. (There is a hack in Meteor that ensures Handlebars templates load before your custom code) For example, Template.foo.helperName = function() { ... } will fail if Template.foo has not yet been defined.
Check the generated HTML (view source) for the initial page load to see if your client-side JavaScript code is loading before the template is defined. If so, you may get an Error like:
TypeError: Cannot set property 'helperName' of undefined`
To workaround this issue, try putting your client-side code in a folder with a different name. I believe that Meteor currently sorts files alphabetically when determining the load order. See the troubleshooting section on this page for more information.
A similar workaround is to utilize Meteor.startup when adding view helpers to your views. That is, you can wrap your Template.foo.helperName = ... stuff in a Meteor.startup call. If you are using a body.blade template, though, you can end up with the opposite problem (i.e. the "catch 22") in which your body.blade template starts rendering before view helpers get setup. In this case, you can get errors since those helpers/variables are not yet defined. The solution here is to avoid using body.blade templates and only render the initial template once all view helpers have been loaded (i.e. at the end of your Meteor.startup routine).
At any rate, all of these workarounds are rather lame. :( But, alas! These issues should be fixed soon.
As soon as Meteor fixes the issue described above, I will modify the Blade smart package to enforce the load order of compiled templates. My apologies for the confusion.
Turns out you can't include files that use Template variables either. i.e., you can't use the include directive in Blade at all if you want to use variables in your template that haven't been initialized by Meteor yet -- you have to insert your template via jQuery/JS after the DOM has loaded. Example:
views/body.blade:
.container
h1 Page Title
#content
views/files.blade:
ul
foreach files as file
- console.log(file);
li= file.filename
client/main.js:
Files = new Meteor.Collection('files');
Template.files.files = function() {
return Files.find();
};
$(function() {
$('#content').html(Meteor.render(Template.files));
});

Symfony2 can't find template file

I am having issues using twig templates for email in SF2.
First, I created a template file located at:
MainBundle/Resources/views/Email/InviteNewUsers.twig
Next, I rendered the view as the body of my email: $this->get('mailer')->send($this->renderView('MainBundle:Email:InviteNewUsers.twig', array('code' => $invite->getCode())));
Unfortunately, this triggers an error: Unable to find template "MainBundle:Email:InviteNewUsers.twig"
is there something wrong with my file placement or my render command?
It was suggest that I might need a type in the template name, so I tested with InviteNewUsers.txt.twig and received the same error.
I believe the name of twig template needs to be InviteNewUsers.{_format}.twig. so it must be something like InviteNewUsers.html.twig, InviteNewUsers.xml.twig, or InviteNewUsers.text.twig etc
EDIT:
Also if your namespace includes first directory something like {Company}\BaseBundle\..., then your template path needs to be {Company}MainBundle:Email:InviteNewUsers.text.twig

Resources