I want to realize the following thing: in my parameters.yml I have uploads_url: http://test.site.com. It's a URL to Docker container with my images. Next, I have an entity with names of this pictures.
I want to generate URL in twig like this:
background-image: url({{ 'URL from parameters' + 'name of picture from `entity`' }})
At present time I have the second part of the previous expression (name of the picture). How can I generate the 1st part?
app/config/config.yml
# Twig Configuration
twig:
globals:
url: "%url%"
app/config/parameters.yml
parameters:
url: 'your_url'
official doc How to Inject Variables into all Templates (i.e. global Variables)
twig concatenate How to concatenate strings in twig
Related
I was wondering if it is possible with Symfony 3.5 to use multiple translation files for a single language when using yml files.
Currently I have something like this:
AppBundle/Resources/translations/messages.en.yml
AppBundle/Resources/translations/messages.de.yml
which contains all my translations in either language. However I was wondering if it was possible to change this to the following structure:
AppBundle/Resources/translations/en/products.yml
AppBundle/Resources/translations/en/invoices.yml
AppBundle/Resources/translations/de/products.yml
AppBundle/Resources/translations/de/invoices.yml
I have been looking but I have been unable to find some kind of solution for this. I got it working for splitting up my routes.
AppBundle/Resources/config/routing.yml
appbundle_routes:
resource: '#AppBundle/Resources/config/routing'
type: directory
Inside that folder I got all my routes split like:
AppBundle/Resources/config/routing/products.yml
AppBundle/Resources/config/routing/users.yml
AppBundle/Resources/config/routing/invoices.yml
I was wondering if it was possible to achieve the same thing with translations?
Symfony's Translator requires files to by named in format domain.locale.loader. In case you have messages.en.yml:
messages is the default name of domain, you can also specify eg. invoices
en is the locale
yml is specifying YAML loader will be used
So your proposed use is not possible to achieve with standard set of configs and functionality. However, you can split your translations to different domain files. So paths would be:
AppBundle/Resources/translations/products.en.yml
AppBundle/Resources/translations/invoices.en.yml
And when you are using translator you specify the domain in which the translation should be looked for:
$translator->trans('translated.key', [], 'invoices');
Or in Twig:
{{ 'translated.key'|trans({},'invoices') }}
I've seen a few answers on how to pass a parameter using iron:router as the third parameter, for example: href="{{pathFor 'article' _id=this._id }}"
What I would like to do is pass a parameter as the second variable (where 'article' is in the example above).
I have a collection of posts that contain a title, a body, and a type. There are three types of articles in the project I'm working on, and each type needs to be routed to a different template because the formatting is significantly different.
What I would like to be able to do is route to a particular url based on the type. An example that captures my intent but doesn't work would be this:
link
One of the "types" is "inquiry", and the route looks like this:
#route "inquiryPost",
path: "inquiry/:_id"
data: ->
Posts.findOne #params._id
waitOn: ->
[
Meteor.subscribe "posts"
]
I've tried using a helper to pass the value into the pathFor, but that didn't work either:
path: ->
type = #type
"pathFor '#{type}'"
...with this on the front end:
link
I can think of a few work arounds, but it seems to me like there should be an easier way to pass in parameters...
So, is it possible to pass in parameters into the second value of a pathFor?
Try with
<template name="example">
link
</template>
Where type is the helper
Template.example.examples({
type:function(){
return type;
}
})
Also you can try with this for example
link with many parameters
Here you where accessing for example to the route
/myRoute/type/rewr8671qew for example
I defined some values in the define.yml in the app/config folder.
And I can access then in the PHP code.
Now I want to use the validate.yml file to check weather users give out too many data, could I use the configed valus in the config file(such as %maxInputLength%) rather than write it one more time in the
validate.yml?
for a example:
I defind a variable in define.yml:
maxLength: 10
So we can use it in the controller.
But if I want to use it in the validate.yml in my own bundle,
such as : myBandle/config/validation.yml
Now I just use the file like that:
myBundle\Entity\UserDesc:
properties:
content:
- NotBlank: ~
- MaxLength: 10
So it seems that the same value was defined at two place.In fact the 10 in the second file is just the value defined in the first file . But I don't know how to get the value from the first file.Is there any way to make that?
AFAIK you want to set value to variable in one config file and then reuse it in other .yml config file.
So you cant define some vars in e.g variables.yml file
parameters:
test_variable: "blabla"
Then in second .yml config file you can import file with defined variables:
imports:
- { resource: variables.yml }
Then you can use imported variable in this file:
property:
some_value: %test_variable%
How to use date locale properly in symfony2 Twig so that:
for GB browser I display format 27/10/2013
for US browser I display format 10/27/2013
First I had to enable Intl twig extension which is by default included in symfony2 framework (not enabled by default)
Enable it by adding this in your config.yml (example src/Project/TestBundle/Resources/config/):
services:
twig.extension.intl:
class: Twig_Extensions_Extension_Intl
tags:
- { name: twig.extension }
Then you use like this:
{{ 'now' | localizeddate('short', 'none') }}
// outputs dd/mm/yyyy or mm/dd/yyyy depending on where browser is coming from
If this does not work automaticaly then you need to force to use Prefered Language by the browser like this (in your controller)
$this->getRequest()->setLocale($this->getRequest()->getPreferredLanguage());
This will use valid date format depending on browser's visitor settings (like Accept-Language header with value en_GB or en_US)
You can force to use certain locale by adding third parameter in localizeddate (example "de-DE"). To get current locale use {{ app.request.locale }}
Make note that twig documentation is a little bit out of date so its better to use what I did above
I have in my template something like that
<h1>Workers {{ 'views.index.list'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</h1>
And I want to translate this but I don't know how. I guess have to create a file with translations but where save it ,how name a file and what write inside?
In app/config/config.yml I have 'translator: { fallback: "%locale%" }'
In 'symfony/app/config/parameters.yml' defined locale parameter "locale:pl"
First look at the bundle's Resources / translations folder. It looks loke this:
JordiLlonchCrudGeneratorBundle.ca.yml
JordiLlonchCrudGeneratorBundle.en.yml
JordiLlonchCrudGeneratorBundle.es.yml
Then create a file JordiLlonchCrudGeneratorBundle.pl.yml (if you want to translate to the pl local) and put it to the app/Resources/JordiLlonchCrudGeneratorBundle/translations directory.
Use one of the default translation files as a pattern for your translation, translate all text you need and save it to the pl translation file you've just created.
See also: http://symfony.com/doc/2.1/cookbook/bundles/inheritance.html#overriding-resources-templates-routing-validation-etc