Sonata: translations with parameters - symfony

I can't seem to be able to use translations with variables in Sonata as I do in normal symfony controllers.
In my controller:
$this->setSonataFlashSuccess(
$this->get('translator')->trans('flash_create_success', [
'%link%' => 'abcd',
'%id%' => '1234'
])
);
My template:
{{ message|trans|raw }}
My translations file 'SonataAdminBundle.yml':
flash_create_success: Created with success: #%id%
The rendered HTML:
Created with success: #%id%
So the translation worked but not the replacement of the expressions Link and Id. Is there something especial about translations in Sonata? Everything works fine in the rest of the app.

When you write a translations file, you have to name it differently than the original (e.g. admin_messages.[langage].yml), otherwise the original SonataAdminBundle.[locale].yml will be used.
So, you should create a file in your bundle like :
# YourBundle/Resources/translations/admin_messages.en.yml`
flash_create_success: Created with success: #%id%
And specify it in your controller :
$this->get('translator')->trans(
'flash_create_success',
array(
'%link%' => 'abcd',
'%id%' => '1234'
),
'admin_messages', // By default looks for a "messages.[_locale].yml"
);
Note that you don't need to translate the message one more time in your template, as it is already translated in your controller.

Related

How to use an array of objects on a foundation email template?

Im using foundation emails, i can use variables on a template by wrapping them in a raw tag, for example:
<raw><%= myVariable %></raw>
Now, I need to add attachments, and attachmeants come as an array with this form:
attachmentsData: [
{
id: '301e165f-130e-4f89-83da-a49ff43172ce_Screenshotfrom2018-11-1916-43-01.png',
title: 'Screenshotfrom2018-11-1916-43-01.png',
url: 'https://s3.eu-central-1.amazonaws.com/dev-messaging-attachments/301e165f-130e-4f89-83da-a49ff43172ce_Screenshotfrom2018-11-1916-43-01.png',
},
{
id: '301e165f-130e-4f89-83da-a49ff43172ce_Screenshotfrom2018-11-1916-43-02.png',
title: 'Screenshotfrom2018-11-1916-43-02.png',
url: 'https://s3.eu-central-1.amazonaws.com/dev-messaging-attachments/301e165f-130e-4f89-83da-a49ff43172ce_Screenshotfrom2018-11-1916-43-02.png',
},
],
On the documentation it also says that i can loop over arrays that are declared in src/data in yml format.
However in my case i need the array of objects to come from the backend.
but if it comes from the backend it i have to parse it with the raw tags.
But if use the raw tags i cant use the each helper:
https://foundation.zurb.com/emails/docs/panini.html#custom-data
Do you know how to loop over this array?
note that, If i do <raw><%= myArray[0].name %></raw> this works and prints the right value.
Any tips? Thanks
If <raw><%= myArray[0].name %></raw> works, then the following should work too.
<% myArray.forEach(data => { %>
<raw><%= data.name %></raw>
<% }); %>

tinyMCE4 can't get external templates to work

I'm very new to tinyMCE (and to JavaScript), so I'm sorry if the answer to my question is obvious. (I'm also working on code and files that another developer created and that I'm not overly familiar with.)
I need to use an external template file for tinyMCE4, and I can't get it to work. I've looked at the tinyMCE4 documentation, but I don't understand where I'm going wrong.
The tinyMCE init is in an index.cfm file, and the list of templates is in a separate file, template_list.js.
Contents of template_list.js:
var tinyMCETemplateList = [
["Name", "templates/file1.cfm", "Name."],
["Name2", "templates/file2.cfm", "Name2."],
...
];
In index.cfm, I've included "template" in the plugins line.
To pull in the templates to appear as a list in a drop-down so the user can choose a template, I've tried:
template_external_list_url: "tinymce/js/tinymce/template_list.js"
With this, when I run the program and click the Insert Template button I get a "No templates defined" error.
I've also tried:
templates : [{url:"tinymce/js/tinymce/template_list.js"}]
With this, the Insert Template dialog box appears, but the drop-down is empty, and the raw code from template_list.js appears in the text area under the drop-down. I get the same result if I change the code in template_list.js to:
[
{title: "Name", url: "templates/file1.cfm", description: "Name."},
{title: "Name2", url: "templates/file2.cfm", description: "Name2."},
...
]
...and also if I add quotations around "title", "url", and "description".
Again, sorry if the answer is obvious, but as a beginner I appreciate any help.
Per the documentation the TinyMCE configuration object expects you to pass an array containing one object for each template. At a high level it would look like this:
tinymce.init({
selector: "textarea", // change this value according to your HTML
plugins: "template",
menubar: "insert",
toolbar: "template",
templates: [
{title: 'Item 1', description: 'Desc 1', content: 'My content'},
{title: 'Item 2', description: 'Desc 2', url: 'development.html'}
]
});
You will note that the templates configuration option is passed an array of objects - this is what TinyMCE expects so no matter what you have to return an array of objects.
You can insert the template HTML directly (as shown in the first example above) or you can point to a URL that the browser can fetch when TinyMCE is initialized (as shown in the second example above). There is no template_external_list_url configuration option so that is not working because its not valid.
If you want to externalize the templates outside the TinyMCE configuration you can place the data in a file and reference that via a URL. For example:
tinymce.init({
selector: "textarea", // change this value according to your HTML
plugins: "template",
menubar: "insert",
toolbar: "template",
templates: "/path/to/the/file/templates.php"
});
The URL referenced there must return an array of objects as that is ultimately what TinyMCE is expecting. Your example above seems to imply your external file is returning a JavaScript variable named tinyMCETemplateList - but that means nothing to TinyMCE so while the file may be loaded what is "returned" is not an array of JavaScript objects.
I would suggest you start by getting things to work without externalizing the templates (just make sure you get the basics working). Then externalize the content to a separate file and make sure that the file returns an array of objects. I would note that your example using tinyMCETemplateList seems to return an array of arrays which is not what TinyMCE is expecting.
I found this really frustrating and fiddly to get working at all. Eventually what I'm doing is calling a function in another js file that returns an array that I give to the templates parameter.
function GetTemplateArray()
{
return new Array(
{
title: "2 Columns",
url: "templates/template1.html",
description: "Adds a 2 column table"
},
{
title: "3 Columns",
url: "templates/scf/template2.html",
description: "Adds a 3 column table"
}
);
}
Then in the tinymce.init code:
tinymce.init(
{
...
templates: GetTemplateArray(),
...

Symfony 2: Way to store app configuration for templates and controller

I am new to symfony and looking for a way store (and read) some informations, which I want to use in controller and templates.
Basically I want to access this sample structure:
project:
name: "My cool Project"
cdn: "http://www.example.com"
paths:
"images": "/images",
"pdf": "/pdf"
...
I have already tried to add this to my parameters.yml. But is it the correct place and how to access it in template AND controller?
In controller, I can do:
$this->getParameter("project")
Is there a way to directly access project.name? Something like:
$this->getParameter("project.name")
How to access it in template?
Just pass the parameter from the controller to the view:
In the controller class:
return [
'variable' => $this->getContainer()->getParameter('variable');
];
In the twig template, to print it:
{{ variable }}
If you want to pass a parameter to the templates without passing it in every controller, use the twig.globals configuration:
twig:
globals:
variable: %variable%
Then print it the same way as above.
$this->getParameter('project')['name'];
EDIT:
For the view, have a look to global Variables in Twig:
http://symfony.com/doc/current/cookbook/templating/global_variables.html

devise password reset view

I'm trying to style my devise forms. So far I have done so by adding classes and IDs into the forms found in app/views/devise. However, I haven't been able to find where the form for /users/password/new (new_password_path) exists. It renders a button with "send me reset password instructions" on it; I did a search through my app and can't find where that comes from... Would that be in the Devise gem itself?
TL/DR: How can I style the forgot password view?
Thanks in advance.
Similar to the Registrations and Sessions behaviour Devise provides a default Passwords view and controller that isn't automatically generated in your own application folder structure.
To override the Forgotten Password view create new.html.erb in a views/devise/passwords folder. You can see the default views on Devise's GitHub: https://github.com/plataformatec/devise/tree/master/app/views/devise/passwords
To override the PasswordsController create passwords_controller.rb in your app/controllers folder that inherits from the Devise controller and override whatever you need. For example to supply a non-default layout.
class PasswordsController < Devise::PasswordsController
include ApplicationHelper
layout "minimal"
end
You then need to change your routes.rb file to specify the custom controller should be used by devise:
devise_for :users, :controllers => {:registrations => "registrations", :sessions => "sessions", :passwords => "passwords"}
In you development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => your_port_num,
:domain => 'site.com',
:user_name => 'admin#site.com',
:enable_starttls_auto => true,
:password => 'none' }
Your path with be redirected in mail.

How to use YUI compressor in Symfony2 routing/controller

How can I use the YUI compressor with following scenario:
routing.yml
js_route:
pattern: /foo/bar.{_format}
defaults: { _controller: FooBundle:Default:JS }
requirements:
_format: js
DefaultController.php
public function JSAction() {
// ...
// content for JS file is being generated
// ...
return $this->render('FooBundle:Default:bar.js.twig', $returnarray);
// ...
}
I know how to use it in my twig templates (e.g. {% javascripts '#FooBundle/Resources/public/js/*' filter='?yui_js' %}) but unfortunately not for above.
Any hints? Thanks!
I don't actually suggest you do this because the YUI JS compressor will be loaded on every request to the resource. But this is one way to do it anyway.
Note, in order to keep the example simple I've excluded any extra code to properly determine your web root and location of the jar file.
$path = $this->container->getParameter('kernel.root_dir');
$ac = new \Assetic\Asset\AssetCollection(array(
new \Assetic\Asset\FileAsset($path . '/../src/WebBundle/Resources/public/js/jquery.longclick.js')
), array(
new \Assetic\Filter\Yui\JsCompressorFilter($path . '/Resources/java/yuicompressor-2.4.7.jar')
));
$compressJS = $ac->dump();
return new Response($compressJS, 200, array('Content-Type' => 'text/javascript'));
Also note, you're not just limited to FileAsset(). There are other classes available like StringAsset(), etc, so you can build content dynamically.

Resources