I'm using Symfony 2.1 dev and looking for easiest way to get parameter from app/config/parameters.yml (ini).
Simple example:
I have record in parameters.yml
parameters:
url: "http://domain.com"
Then i want to use it somehow in static js file
var url = "{{ app.url }}"; // trying to avoid hardcode
This token should be replaced by actual value from coonfig after
app/console assetic:dump
So final js will have
var url = "http://domain.com";
Currently i'm thinking about writing my own console command but firstly i want to ensure there is no any standard way of doing such things in Symfony2 or maybe some bundle that can halp me?
UPDATE: i'd like to do this with AsseticBundle, like YUI and LESS
assetic:
debug: %kernel.debug%
use_controller: false
write_to: %kernel.root_dir%/../web
filters:
cssrewrite: ~
lessphp: ~
yui_js:
jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar"
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar
to add another one filter which will replace token {{ app.url }} in js file to actual "http://domain.com"
A simple solution would be to reference your parameters in the twig globals:
parameters:
url: "http://domain.com"
an_array:
twig: "is cool"
and: "symfony2 to"
twig:
globals:
app_parameters:
url: %url%
an_array: %an_array%
Then in your template:
<script>
window.parameters = {{ app_parameters|json_encode|raw }};
</script>
would render something like:
<script>
window.parameters = {"url":"http://domain.com","an_array":{"twig":"is cool","and":"symfony2 to"}};
</script>
Related
I'm searching how i can get the "data base name" on my twig view on silex and symfony2 or 3.
I found that i can go on "app.db" and there is "_params" and "db name" witch are twig object and protected so i can access to it.
I have tried :
app.db._params.dbname
app.db.dbname
app.db.get('dbname')
app.db.get('_params')
app.db.get('params')
There is an other solution with out set the variable in the controller??
Thank in advance
You can inject global variables into templates like described in the docs
http://symfony.com/doc/3.1/cookbook/templating/global_variables.html
# app/config/parameters.yml
parameters:
database_name: name
# app/config/config.yml
twig:
globals:
app_database_name: '%database_name%'
{# default/index.html.twig #}
{{ app_database_name }}
I have a problem with generating relative URL in symfony 2.7 project
My project is hosted behind a proxy that redirect URL to the target VM, like this - example.com/oblounge/bo/ to vmhostname/
All my URL are generated in relative
Here is my problem
For example when I'm on this page
http://example.com/oblounge/bo/admin/articles/actualite?section=127
All generated likns are malformated, the "/admin/articles" is duplicated
http://example.com/oblounge/bo/admin/articles/admin/articles/actualite?section=127
Note: The problem is occurred only for rendered controllers
The render action
{{ render(controller('AppBundle:Back/Article/Article:sectionsArticle', { 'rubric': 'actualite' })) }}
The rendered twig
{% for section in sections %}
<li><i class="fa fa-circle-o"></i>{{ section.name }}</li>
{% endfor %}
My routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
The prefix of my admin controller
/**
* #Route("/admin")
*/
class ArticleController extends BaseArticleController
{
I would suggest you give a look at your routing.yml file that is referenced within your bundle.
If you've already defined a prefix for the route in the routing.yml of you app/config there is no need to repeat it in the routing of your bundle , otherwise there will be a duplication of prefixes as in your case.
Example:
This is the routing.yml in the App\config folder
tutoLexikTestBundle_parents:
resource: "#tutoLexikTestBundle/Resources/config/routing/parents.yml"
prefix: /parents
And this is the one of the bundle
parents_show:
path: /{id}/show
defaults: { _controller: "tutoLexikTestBundle:Parents:show" }
I don't need to repeat the prefix 'Parents' in the path
I think there is a bug in the bundle LiipImagineBundle. I explain:
Here is my new config of the bundle:
liip_imagine:
resolvers:
default:
web_path:
web_root: %kernel.root_dir%/../web/img
# %kernel.root_dir%/../web/img is the folder where filtered images will be created!
cache_prefix: media/cache
# media/cache the prefix of folder where the cached images will be created
filter_sets:
cache: ~
my_thumb:
quality: 75
filters:
thumbnail: { size: [120, 90], mode: outbound }
This is the twig part for displaying the image:
{# This way the filtered image will not be created!#}
<img src="{{ 'img/test.jpg' | imagine_filter('my_thumb') }}" />
{# That way, the filted images will be created. asset() must be used. #}
<img src="{{ asset('img/test.jpg' | imagine_filter('my_thumb')) }}" />
The generated link of the image is not correct! In fact, the obtained link is:
http://localhost/media/cache/my_thumb/img/test.jpg
The expected correct link is:
http://localhost/tuto/web/img/media/cache/my_thumb/img/test.jpg
There is a missing part in the url: tuto/web/img . Is this a bug?
To avoid that problem, I did this:
<img src="{{ asset('img/test.jpg' | imagine_filter('my_thumb'))|replace({'media':'tuto/web/img/media'}) }}" />
I guess that playing with twig is not a good solution.
It is a bug in LiipImagineBundle? If not, please give the correct config for that bundle!
Thanks!
I found finally the solution on github.
I am just trying to get working LiipImagineBundle.
Ok, all I got so far:
Installed using composer
$ php composer.phar require "liip/imagine-bundle:dev-master"
Enabled bundle in AppKernel.php
new Liip\ImagineBundle\LiipImagineBundle(),
Added to routing.yml
_imagine path
Added liip_imagine filter in config.yml
Checked using php app/console router:debug and path _imagine_my_thumb exist.
But after using:
<img src="{{ '/relative/path/to/image.jpg' | imagine_filter('my_thumb') }}" />
image is not rendered, path is simply not found error.
prod.log says that Route _imagine_my_thumb does not exist, although it exist, because it's displayed using router:debug for both environments.
You know that you should replace '/relative/path/to/image.jpg' with your image path?
Make sure your file exists.
A working example
config:
liip_imagine:
driver: gd
web_root: %kernel.root_dir%/../web
data_root: %kernel.root_dir%/../app
cache_mkdir_mode: 0777
cache_prefix: /media/cache
cache: web_path
cache_clearer: true
data_loader: filesystem
controller_action: liip_imagine.controller:filterAction
formats: []
filter_sets:
avatar:
filters:
thumbnail: { size: [40, 40], mode: outbound }
profile:
filters:
relative_resize: { widen: 500 }
html:
<img src="{{ 'uploads/images/filename.jpg' | imagine_filter('avatar') }}" alt="image">
enter code here
routing.yml:
_imagine:
resource: .
type: imagine
Remark: My source folder is in the app folder (see: data_root)
I had similar problem and after enable of php_fileinfo extension in php.ini render start to work.
I try to retrieve the global param that i define in parameters.yml and config.yml
in FOSUserBundle.en.yml under registration, in message i try to pass %myparam% and in my email.txt i try pass %myparam%:param like this
{{ 'registration.email.message'|trans({'%username%': user.username, '%confirmationUrl%': confirmationUrl,'%myparam%':myparam}, 'FOSUserBundle') }}
but it dosen't works.
and can i insert html inside yml and new line?
thanks
In your config.yml under fosuserbundle configuration.
confirmation:
enabled: true
template: MYMailBundle:Registration:email.html.twig
By doing this you are creating a twig template where you can render your variables and format your html how you want.
If you want to create a newline in your yml file I think you could do it by adding a \n.