Does anybody know how to get the current URI in a Twig template?
I’ve read through the documentation and I’m unable to find the Twig function to do this.
{{ app.request.uri }}
If you want to read it into a view variable:
{% set uri = app.request.uri %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.
Related
Sometime i need to check if an user can comment from a voter, obviously at this point of the code the comment does not yet exist, but still i need to check if the user is allowed to comment or not.
From the controller i would usually do $this->isGranted('create', new Comment()) but how i'm supposed to do the same thing in twig? I can't find a clear explaination, and i obviously can't do something like {% if is_granted('create', new comment()) %}.
Is there a way to do this without hacking an object from the controller such as rendering a new Comment() from the controller? Doing so will allow me to avoid a lot of spaghetti code in my voters.
you can extend twig with Symfony Twig Extensions
Look at this documentation link:
Symfony Twig Extensions
Inside your twig you can use is_granted like this:
{{ is_granted(role, object = null, field = null) }}
I need a little help.
Imagine that in database for every user have stored color of background.
Everytime when user login, first for that user in some folder is generated css file with name of id of user and included in html template.
I need help to understand how to generate css file ?
Thanks.
I Symfony with FOSUserBundle, you can use app.user to get the user in your twig files.
If you open a MySQL console and enter describe fos_user; then you will see all the fields you can use in your Twig file. So in your table you could store for each of the FOS users a color
Then in Twig you could use something like this:
{% if app.user %}
{% set backColor = app.user.color %}
{% else %}
{% set backColor = 'none' %}
{% endif %}
...
<body style="background-color:{{ backColor }};">
You get the idea...
You might want to figure out a way to store the color preference for the FOS user using a Symfony form. And then store preference in database. You can raise a separate question for that if you have difficulties - it should be easy.
Quite simple...
You need to generate a twig file for every user in your desired folder, and place "inline" CSS in it.
{% block css %}
<style>
.class {
parameter: {{ entity.value }};
}
</style>
{% endblock css %}
To generate the file, create a service, which you will call when a page is loaded.
It will have to first check if the file exist, if it does, it will read from it (no query to the DB), if not, it will generate the file, and read need data from DB.
Look at answer from me and Gopal on this page to know how to make a service How to use session in Symfony.
You sould be able to use it for your need... ;)
Also look at the Symfony Filesystem Component
file_put_contents() should help you write content of the file... ;)
I want to be able to pass into twig some html in a variable and render it as
{{ data.content | raw }}
But within that content variable I would like to be able to have content like this - pulled from a database:
<div>
<p>Text with a Twig Extension function call eg: {{ doSomething('112233') }}</p>
</div>
Is there any way to get that twig extension to fire having come from the Twig variable itself?
Thank you.
There is one possible solution. Look at template_from_string twig function. It says:
New in version 1.11: The template_from_string function was added in Twig 1.11.
The template_from_string function loads a template from a string:
{{ include(template_from_string(data.content)) }}
But doc also says:
The template_from_string function is not available by default. You must add the Twig_Extension_StringLoader extension explicitly when creating your Twig environment.
So, probably it won't work out of box.
I am trying to create a basic CMS in Symfony2. Is it possible to "translate" twig code from mysql to controller somehow?
For example let say in mysql there is
{% include 'FOO::FOO.html.twig' %}
When I get it from orm how to make it load the template instead of just echo
{% include 'FOO::FOO.html.twig' %}
Thanks a lot.
You can use the internal Lexer, parser and compiler of twig to get a php object representation of your templates:
http://twig.sensiolabs.org/doc/internals.html
I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.